Smtp sending email code with attachments _ practical tips-PHP source code

Source: Internet
Author: User
Tags email account mailmessage smtpclient
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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.