Smtp sends an email with an attachment (Save the string type result as an attachment), smtpstring

Source: Internet
Author: User
Tags mailmessage smtpclient

Smtp sends an email with an attachment (Save the string type result as an attachment), smtpstring

This method can be directly saved as an HTML file or a text file. Other formats do not work well.
MailMessage mmsg = new MailMessage ();
Mmsg. Subject = "mail title ";
Mmsg. Body = "email content ";
Mmsg. To. Add ("accept@qq.com"); // receive mailbox
Byte [] bytes = System. Text. Encoding. Default. GetBytes
(@ "<Table> <tr> <td width = 150> 1234567891234567
</Td> <td width = 80> 12345678 </td> </tr> </table> ");
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 the email account and password
SC. Credentials =
New System. Net. NetworkCredential ("account", "password ");
// Send email
Mmsg. From = new MailAddress ("account@qq.com ");
SC. Send (mmsg );
// Release stream Resources
Ms. Close ();
Ms. Dispose ();
 

Related Article

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.