Use System.Net.Mail.MailMessage to send e-mail

Source: Internet
Author: User
Tags mailmessage smtpclient

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

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.