Starting with. NET 2.0, a new class was introduced, System.Net.Mail.MailMessage. This class is used to replace the System.Web.Mail.MailMessage class of the. NET 1.1 era.
The System.Net.Mail.MailMessage class is used to specify one message, and the other class System.Net.Mail.SmtpClient to set up SMTP and then send the message. Because SMTP now requires authentication and some require SSL (such as Gmail), the properties are slightly more set. The code snippet is as follows:
Using System.Net.Mail;
...
MailMessage MAILMSG = new MailMessage ();
Mailmsg.from = new MailAddress ("Your email address");
MAILMSG.TO.ADD ("The email address of the receiver 1");
MAILMSG.TO.ADD ("The email address of the receiver 2");
Mailmsg.subject = "Mail Subject";
Mailmsg.body = "message subject content";
mailmsg.bodyencoding = Encoding.UTF8;
Mailmsg.isbodyhtml = false;
mailmsg.priority = Mailpriority.high;
SmtpClient smtp = new SmtpClient ();
The user name and password that provide authentication
NetEase Mail user may be: username password
Gmail users may be: username@gmail.com password
Smtp. Credentials = new NetworkCredential ("username", "password");
Smtp. Port = 25; Gmail uses 465 and 587 ports
Smtp. Host = "SMTP server address"; such as smtp.163.com, smtp.gmail.com
Smtp. Enablessl = false; If you use Gmail, you need to set to True
Smtp. sendcompleted + = new Sendcompletedeventhandler (sendmailcompleted);
Try
{
Smtp. SendAsync (MAILMSG, MAILMSG);
}
catch (Smtpexception ex)
{
Console.WriteLine (ex. ToString ());
}
...
void Sendmailcompleted (object sender, AsyncCompletedEventArgs e)
{
MailMessage MAILMSG = (mailmessage) e.userstate;
string subject = Mailmsg.subject;
if (e.cancelled)//mail is canceled
{
Console.WriteLine (subject +) was canceled. ");
}
if (e.error!= null)
{
Console.WriteLine ("Error:" + e.error.tostring ());
}
Else
{
Console.WriteLine ("Send complete.") ");
}
}
Reference:
MailMessage class
Http://msdn.microsoft.com/zh-cn/library/system.net.mail.mailmessage (vs.80). aspx