Transferred from: HTTP://HI.BAIDU.COM/BLUESKY_CN/ITEM/8BB060ACE834C53F020A4DF2
The e-mail account and password used below are not real and need to be tested and replaced by your email account.
References are required:
1 using System.Net.Mail; 2 using System.Text; 3 using System.Net;
Program code:
1MailMessage MyMail =NewMailMessage ();//Create a message instance object2Mymail.from =NewMailAddress ("");//sender, to the mail server verification information corresponding, can not be arbitrarily changed3 4MYMAIL.TO.ADD (NewMailAddress (""));//Recipient5Mymail.subject ="C # Send email";//message Header6mymail.subjectencoding = Encoding.UTF8;//Title Encoding7Mymail.body ="This is a test email!";//Message Content8mymail.bodyencoding = Encoding.UTF8;//Message content Encoding9mymail.isbodyhtml =true;//whether the message content supports HTMLTenSmtpClient SMTP =NewSmtpClient ();//To create an SMTP instance object OneSmtp. Host ="mail.sina.com";//Mail Server SMTP ASmtp. Port = -;//Mail server Port -Smtp. Credentials =NewNetworkCredential ("","123456");//Mail server Authentication information -Smtp. Send (MyMail);//Send mail
Example of sending a message using a Gmail mailbox
1MailMessage MyMail =NewMailMessage ();2Mymail.from =NewMailAddress ("");3MYMAIL.TO.ADD (NewMailAddress (""));4Mymail.subject ="C # Send email";5Mymail.subjectencoding =Encoding.UTF8;6Mymail.body ="This was a test email from gmail!<a href= ' http://www.sina.com.cn ' >sina</a>";7Mymail.bodyencoding =Encoding.UTF8;8mymail.isbodyhtml =true;9SmtpClient SMTP =Newsmtpclient ();TenSmtp. Host ="smtp.gmail.com"; OneSmtp. Port =587;//Gmail's SMTP port ASmtp. Credentials =NewNetworkCredential ("","123456"); -Smtp. Enablessl =true;//gmail requires SSL connection -Smtp. Deliverymethod = Smtpdeliverymethod.network;//the way Gmail is sent is through the network, you need to specify theSmtp. Send (MyMail);
Example of sending mail using QQ mailbox
1MailMessage MyMail =NewMailMessage ();2Mymail.from =NewMailAddress ("");3MYMAIL.TO.ADD (NewMailAddress (""));4Mymail.subject ="C # Send email";5Mymail.subjectencoding =Encoding.UTF8;6Mymail.body ="This was a test email from qq!";7Mymail.bodyencoding =Encoding.UTF8;8mymail.isbodyhtml =true;9SmtpClient SMTP =Newsmtpclient ();TenSmtp. Host ="smtp.qq.com"; OneSmtp. Credentials =NewNetworkCredential ("","123456"); ASmtp. Send (MyMail);
"Go" C # send an email message