C # send an Email (for example, QQ or Gmail)

Source: Internet
Author: User
Tags email account mailmessage smtpclient ssl connection

The email account and password used below are not authentic. If you need to test the password, replace it with your own email account.

To be referenced:
Using System. Net. Mail;
Using System. Text;
Using System. Net;

Program code:

MailMessage myMail = new MailMessage (); // create an email Instance Object

MyMail. From = new MailAddress ("bluesky@sina.com"); // sender, which corresponds to the verification information of the mail server and cannot be changed at will

MyMail. To. Add (new MailAddress ("848594@qq.com"); // recipient

MyMail. Subject = "C # Send Email"; // mail title
MyMail. SubjectEncoding = Encoding. UTF8; // Title Encoding

MyMail. Body = "this is a test email! "; // Email content
MyMail. BodyEncoding = Encoding. UTF8; // email Content Encoding
MyMail. IsBodyHtml = true; // whether the email content supports html

SmtpClient smtp = new SmtpClient (); // create an smtp Instance Object
Smtp. Host = "mail.sina.com"; // Mail Server SMTP
Smtp. Port = 25; // mail server Port
Smtp. Credentials = new NetworkCredential ("bluesky@sina.com", "123456"); // Mail Server verification information

Smtp. Send (myMail); // Send an email

Example of sending emails using Gmail

MailMessage myMail = new MailMessage ();

MyMail. From = new MailAddress ("bluesky@gmail.com ");
MyMail. To. Add (new MailAddress ("444354@qq.com "));

MyMail. Subject = "C # Send Email ";
MyMail. SubjectEncoding = Encoding. UTF8;

MyMail. Body = "this is a test email from gmail! <A href = 'HTTP: // www.sina.com.cn '> sina </a> ";
MyMail. BodyEncoding = Encoding. UTF8;
MyMail. IsBodyHtml = true;

SmtpClient smtp = new SmtpClient ();
Smtp. Host = "smtp.gmail.com ";
Smtp. Port = 587; // smtp Port of Gmail
Smtp. Credentials = new NetworkCredential ("bluesky@gmail.com", "123456 ");
Smtp. EnableSsl = true; // Gmail requires SSL connection

Smtp. DeliveryMethod = SmtpDeliveryMethod. Network; // The sending method of Gmail is through the Network. You must specify

Smtp. Send (myMail );

Example of sending an email using QQ mail

MailMessage myMail = new MailMessage ();

MyMail. From = new MailAddress ("4443233@qq.com ");
MyMail. To. Add (new MailAddress ("bluesky@gmail.com "));

MyMail. Subject = "C # Send Email ";
MyMail. SubjectEncoding = Encoding. UTF8;

MyMail. Body = "this is a test email from QQ! ";
MyMail. BodyEncoding = Encoding. UTF8;
MyMail. IsBodyHtml = true;

SmtpClient smtp = new SmtpClient ();
Smtp. Host = "smtp.qq.com ";
Smtp. Credentials = new NetworkCredential ("4443233@qq.com", "123456 ");

Smtp. Send (myMail );

 

Reference from: http://hi.baidu.com/%CA%D8%CD%FB%C0%B6%CC%EC/blog/item/6e5e4d60c9d6776c0d33fa3d.html

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.