This article will share with you a piece of smtp code for sending an email with attachments. the code is very simple and easy to understand. if you need it, refer to it. This article will share with you a piece of smtp code for sending an email with attachments. the code is very simple and easy to understand. if you need it, refer to it.
This method can be directly saved as an HTML file or a text file. Other formats do not work well.
The code is as follows:
MailMessage mmsg = new MailMessage (); mmsg. subject = "Mail title"; mmsg. body = "email content"; mmsg. to. add ("accept@qq.com"); // receive email byte [] bytes = System. text. encoding. default. getBytes (@"
1234567891234567 |
12345678 |
"); MemoryStream MS = new MemoryStream (bytes); ContentType ct = new ContentType (); // attachment file type ct. mediaType = MediaTypeNames. text. html; // attachment name, which can be another suffix ct. name = "attachment Name" + DateTime. now. toString () + ". html "; mmsg. attachments. add (new Attachment (MS, ct); // SMTP simple mail protocol System. net. mail. smtpClient SC = new System. net. mail. smtpClient (); SC. host = "127.0.0.1"; // Host address SC. port = 25; // Port // send email account and password SC. credentials = new System. net. networkCredential ("account", "password"); // send the email message. from = new MailAddress ("account@qq.com"); SC. send (mmsg); // release stream resource ms. close (); ms. dispose ();
Attached to an instance where. net uses smtp to send an email with an attachment
The code is as follows:
Public static void sendEmail (string toAddress, string emailbody) {var fromAddress = ConfigurationManager. deleetask[ "EmailAddress"]; string fromPassword = ConfigurationManager. deleetask[ "EmailPassword"]. toString (); const string subject = "Job Recommendation"; var smtp = new SmtpClient {Host = ConfigurationManager. deleetask[ "SmtpServer"]. toString (), Port = int. parse (ConfigurationManager. deleetask[ "SmtpPort"]), EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod. network, usedefacrecredentials = false, Credentials = new NetworkCredential (fromAddress, fromPassword)}; using (var message = new MailMessage (fromAddress, toAddress, subject, HttpUtility. htmlEncode (emailbody) {smtp. send (message) ;}// Email Address // Emial PWD <-- with attachment version-> 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, usedefacrecredentials = 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 );