The examples in this article describe the three ways in which C # implements sending messages. Share to everyone for your reference. The specific methods are analyzed as follows:
First, the question:
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.
Second, the implementation of code:
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:
The code is as follows:
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 more than one person
*/
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 more than one person
*/
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. Through the normal SMTP
C # code is as follows
The code is as follows:
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 more than one person
*/
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
The code is as follows:
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 more than one person
*/
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.
I hope this article is helpful to everyone's C # programming.
In addition to the Declaration,
Running GuestArticles are original, reproduced please link to the form of the address of this article
C # Three ways to implement sending messages
This address: http://www.paobuke.com/develop/c-develop/pbk23309.html
Related content C # methods for writing COM components analysis Wpf?¢d?á?ììoíí¨???? °′?¥?ùê?′ú???? íc# realization of simple access to the code of Scanning gun Information C # using Protocol Buffer (PROTOBUF) for socket communication in unity
C # Implementation of Web crawler C # crawl Web page HTML source code C # Compute string hash value (MD5, SHA) method Summary C # Implementation of all files in the zip archive C # method of saving data in a ListBox to a text file
C # Three ways to implement sending messages