Copy codeThe Code is as follows:
Public static void sendEmail (string toAddress, string emailbody)
{
Var fromAddress = ConfigurationManager. receivettings ["EmailAddress"];
String fromPassword = ConfigurationManager. receivetask[ "EmailPassword"]. ToString ();
Const string subject = "Job Recommendation ";
Var smtp = new SmtpClient
{
Host = ConfigurationManager. receivettings ["SmtpServer"]. ToString (),
Port = int. Parse (ConfigurationManager. receivettings ["SmtpPort"]),
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod. Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential (fromAddress, fromPassword)
};
Using (var message = new MailMessage (fromAddress, toAddress, subject, HttpUtility. HtmlEncode (emailbody )))
{
Smtp. Send (message );
}
}
<Add key = "EmailAddress" value = "*********** @ gmail.com"/> // Email Address
<Add key = "EmailPassword" value = "**********"/> // Emial PWD
<Add key = "SmtpServer" value = "smtp.gmail.com"/>
<Add key = "SmtpPort" value = "587"/>
<-- Version with attachment->
Var fromAddress = "allenyinj@gmail.com ";
String fromPassword = "yj1989120 ";
Const string subject = "CV ";
Var smtp = new SmtpClient
{
Host = "smtp.gmail.com ",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod. Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential (fromAddress, fromPassword)
};
MailMessage email = new MailMessage (fromAddress, "allen.yin.jun@gmail.com ");
Email. Subject = "INLINE attachment TEST ";
Email. IsBodyHtml = true;
String attachmentPath = "C: \ 3.jpeg ";
Attachment inline = new Attachment (attachmentPath );
Inline. ContentDisposition. Inline = true;
Inline. ContentDisposition. DispositionType = DispositionTypeNames. Inline;
// Inline. ContentId = "1 ";
// Inline. ContentType. MediaType = "image/png ";
Inline. ContentType. Name = Path. GetFileName (attachmentPath );
Email. Attachments. Add (inline );
Email. Body = "test ";
Smtp. Send (email );
Email. Dispose ();
// If there is no path, use Stream
Attachment letter = new Attachment (FileUploadLetter. FileContent, FileUploadLetter. PostedFile. ContentType );
Letter. ContentDisposition. Inline = true;
Letter. ContentDisposition. DispositionType = DispositionTypeNames. Inline;
// Inline. ContentId = "1 ";
Letter. ContentType. MediaType = FileUploadLetter. PostedFile. ContentType;
Letter. ContentType. Name = Path. GetFileName (FileUploadLetter. PostedFile. FileName );
Letter. Name = Path. GetFileName (FileUploadLetter. PostedFile. FileName );
// If you want to send an anonymous email
Var smtp = new SmtpClient
{
Host = "serverName ",
Port = 25, // anonymous sending Port
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod. Network,
UseDefaultCredentials = false,
// Credentials = new NetworkCredential (fromAddress, fromPassword)
};