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)