C # Three ways to send mail (LOCALHOST,SMTP,SSL-SMTP)

Source: Internet
Author: User
Tags mailmessage smtpclient

Recently due to a r&i project needs, user requirements in the purchase of products or shipments and other links, need to send an email alert or every Monday to let the system automatically collect data sent an e-mail, so I also find relevant information, wrote a demo to share to everyone, we learn together.
The "System.Net.Mail" provided under the. Net FrameWork 2.0 can be easily implemented, and this article lists 3 ways to send:
1. through localhost;
2. through ordinary SMTP;
3. SMTP via SSL;
For one of the following:
[HTML]

public void Sendmaillocalhost ()
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage ();
Msg. To.add ("[email protected]");
Msg. To.add ("[email protected]");
/* Msg. To.add ("[email protected]");
* Msg. To.add ("[email protected]");
* Msg. To.add ("[email protected]"); can be sent to multiple people
*/
Msg. Cc. ADD ([email protected]);
/*
* Msg. Cc. ADD ("[email protected]");
* Msg. Cc. ADD ("[email protected]"), can be copied to many people
*/
Msg. from = new MailAddress ("[email protected]"," Alphawu ", System.Text.Encoding.UTF8);
/* Above 3 parameters are sender address (can be written casually), sender name, code */
Msg. Subject = "This is a test message";//message header
Msg. subjectencoding = system.text.encoding.utf8;//message header encoding
Msg. Body = "Mail content";//message content
Msg. bodyencoding = system.text.encoding.utf8;//message content encoding
Msg. isbodyhtml = false;//is an HTML message
Msg. Priority = mailpriority.high;//Message Precedence

SmtpClient client = new SmtpClient ();
Client. Host = "localhost";
Object userState = msg;
Try
{
Client. SendAsync (msg, userState);
Simply a little bit can be client. Send (msg);
MessageBox.Show ("sent successfully");
}
catch (System.Net.Mail.SmtpException ex)
{
MessageBox.Show (ex. Message, "Error sending message");
}
}
public void Sendmaillocalhost ()
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage ();
Msg. To.add ("[email protected]");
Msg. To.add ("[email protected]");
/* Msg. To.add ("[email protected]");
* Msg. To.add ("[email protected]");
* Msg. To.add ("[email protected]"); can be sent to multiple people
*/
Msg. Cc. ADD ([email protected]);
/*
* Msg. Cc. ADD ("[email protected]");
* Msg. Cc. ADD ("[email protected]"), can be copied to many people
*/
Msg. from = new MailAddress ([email protected], "Dulei", System.Text.Encoding.UTF8);
/* Above 3 parameters are sender address (can be written casually), sender name, code */
Msg. Subject = "This is a test message";//message header
Msg. subjectencoding = system.text.encoding.utf8;//message header encoding
Msg. Body = "Mail content";//message content
Msg. bodyencoding = system.text.encoding.utf8;//message content encoding
Msg. isbodyhtml = false;//is an HTML message
Msg. Priority = mailpriority.high;//Message Precedence
SmtpClient client = new SmtpClient ();
Client. Host = "localhost";
Object userState = msg;
Try
{
Client. SendAsync (msg, userState);
Simply a little bit can be client. Send (msg);
MessageBox.Show ("sent successfully");
}
catch (System.Net.Mail.SmtpException ex)
{
MessageBox.Show (ex. Message, "Error sending message");
}
}


2. Pass the normal SMTP C # code as follows
[HTML]
public void Sendmailusezj ()
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage ();
Msg. To.add ([email protected]);
Msg. To.add ([email protected]);
/*
* Msg. To.add ("[email protected]");
* Msg. To.add ("[email protected]");
* Msg. To.add ("[email protected]"); can be sent to multiple people
*/
Msg. Cc. ADD ("[email protected]");
/*
* Msg. Cc. ADD ("[email protected]");
* Msg. Cc. ADD ("[email protected]"), can be copied to many people
*/
Msg. from = new MailAddress ("[email protected]"," Dulei ", System.Text.Encoding.UTF8);
/* Above 3 parameters are sender address (can be written casually), sender name, code */
Msg. Subject = "This is a test message";//message header
Msg. subjectencoding = system.text.encoding.utf8;//message header encoding
Msg. Body = "Mail content";//message content
Msg. bodyencoding = system.text.encoding.utf8;//message content encoding
Msg. isbodyhtml = false;//is an HTML message
Msg. Priority = mailpriority.high;//Message Precedence

SmtpClient client = new SmtpClient ();
Client. Credentials = new System.Net.NetworkCredential ("[email protected]"," Userpass ");
e-mail and password registered in 71info.com
Client. Host = "smtp.71info.com";
Object userState = msg;
Try
{
Client. SendAsync (msg, userState);
Simply a little bit can be client. Send (msg);
MessageBox.Show ("sent successfully");
}
catch (System.Net.Mail.SmtpException ex)
{
MessageBox.Show (ex. Message, "Error sending message");
}
}


3. SMTP over SSL
[HTML]
public void Sendmailusegmail ()
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage ();
Msg. To.add ([email protected]);
Msg. To.add ([email protected]);
/*
Msg. To.add ("[email protected]");
* Msg. To.add ("[email protected]");
* Msg. To.add ("[email protected]"); can be sent to multiple people
*/
Msg. Cc. ADD ([email protected]);
/*
* Msg. Cc. ADD ("[email protected]");
* Msg. Cc. ADD ("[email protected]"), can be copied to many people
*/
Msg. from = new MailAddress ("boys90.com", "Dulei", System.Text.Encoding.UTF8);
/* Above 3 parameters are sender address (can be written casually), sender name, code */
Msg. Subject = "This is a test message";//message header
Msg. subjectencoding = system.text.encoding.utf8;//message header encoding
Msg. Body = "Mail content";//message content
Msg. bodyencoding = system.text.encoding.utf8;//message content encoding
Msg. isbodyhtml = false;//is an HTML message
Msg. Priority = mailpriority.high;//Message Precedence
SmtpClient client = new SmtpClient ();
Client. Credentials = new System.Net.NetworkCredential ("[email protected]"," password ");
Write your Gmail email and password above
Client. Port = Ports Used by 587;//gmail
Client. Host = "smtp.gmail.com";
Client. Enablessl = true;//SSL-Encrypted
Object userState = msg;
Try
{
Client. SendAsync (msg, userState);
Simply a little bit can be client. Send (msg);
MessageBox.Show ("sent successfully");
}
catch (System.Net.Mail.SmtpException ex)
{
MessageBox.Show (ex. Message, "Error sending message");
}
}

Through Gmail to send mail, the success rate is very high, almost all can be sent to, recommended to use, the above several methods, I think we have enough to do the development of the use. As for the demo I've done, I've sorted it out and shared it with everyone.
My independent Blog 90 Boys sharing network welcome everyone to visit, we work together to learn more knowledge, sharing network happy to share, happy life!!

C # Three ways to send mail (LOCALHOST,SMTP,SSL-SMTP)

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.