C implements three methods for sending mail

  • 2020-12-10 00:48:59
  • OfStack

This article illustrates three ways that C# implements sending mail. Share to everybody for everybody reference. The specific analysis is as follows:

1. The question is:

Recently the company due to an R & For the needs of I project, users required to send email reminders or let the system collect data automatically and send one ES9en-ES10en on the 1st of each week when purchasing products or shipping products. Therefore, I found relevant materials and wrote one Demo to share with you so that we could learn together.

2. Implementation code:

"System. Net. Mail" provided under.Net FrameWork 2.0 can be easily implemented. This article lists three ways to send:
1. Through Localhost;
2. Through ordinary SMTP;
3. SMTP through SSL;
One by one:

public void SendMailLocalhost()  

System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); 
msg.To.Add("a@a.com"); 
msg.To.Add("b@b.com"); 
/* msg.To.Add("b@b.com"); 
* msg.To.Add("b@b.com"); 
* msg.To.Add("b@b.com"); Can be sent to more than one person  
*/ 
msg.CC.Add(c@c.com); 
/* 
* msg.CC.Add("c@c.com"); 
* msg.CC.Add("c@c.com"); You can cc more than one person  
*/ 
msg.From = new MailAddress("a@a.com", "AlphaWu", System.Text.Encoding.UTF8); 
/* The above 3 The parameters are the sender address, the sender name, and the code */ 
msg.Subject = " This is the test email ";// Email title  
msg.SubjectEncoding = System.Text.Encoding.UTF8;// Mail subject code  
msg.Body = " Email content ";// Email content  
msg.BodyEncoding = System.Text.Encoding.UTF8;// Message content encoding  
msg.IsBodyHtml = false;// Whether it is HTML mail  
msg.Priority = MailPriority.High;// Email priority
 
SmtpClient client = new SmtpClient(); 
client.Host = "localhost"; 
object userState = msg; 
try 

client.SendAsync(msg, userState); 
// simple 1 Some can client.Send(msg); 
MessageBox.Show(" Send a success "); 

catch (System.Net.Mail.SmtpException ex) 

MessageBox.Show(ex.Message, " Sending an email incorrectly "); 

}
    public void SendMailLocalhost() 

System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); 
msg.To.Add("a@a.com"); 
msg.To.Add("b@b.com"); 
/* msg.To.Add("b@b.com"); 
* msg.To.Add("b@b.com"); 
* msg.To.Add("b@b.com"); Can be sent to more than one person  
*/ 
msg.CC.Add(c@c.com); 
/* 
* msg.CC.Add("c@c.com"); 
* msg.CC.Add("c@c.com"); You can cc more than one person  
*/ 
msg.From = new MailAddress(master@boys90.com, "dulei", System.Text.Encoding.UTF8); 
/* The above 3 The parameters are the sender address, the sender name, and the code */ 
msg.Subject = " This is the test email ";// Email title  
msg.SubjectEncoding = System.Text.Encoding.UTF8;// Mail subject code  
msg.Body = " Email content ";// Email content  
msg.BodyEncoding = System.Text.Encoding.UTF8;// Message content encoding  
msg.IsBodyHtml = false;// Whether it is HTML mail  
msg.Priority = MailPriority.High;// Email priority
SmtpClient client = new SmtpClient(); 
client.Host = "localhost"; 
object userState = msg; 
try 

client.SendAsync(msg, userState); 
// simple 1 Some can client.Send(msg); 
MessageBox.Show(" Send a success "); 

catch (System.Net.Mail.SmtpException ex) 

MessageBox.Show(ex.Message, " Sending an email incorrectly "); 

}


2. Via regular SMTP C# code as follows
public void SendMailUseZj()    
{   
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();   
msg.To.Add(a@a.com);   
msg.To.Add(b@b.com);   
/*  
* msg.To.Add("b@b.com");  
* msg.To.Add("b@b.com");  
* msg.To.Add("b@b.com"); Can be sent to more than one person   
*/   
msg.CC.Add("c@c.com");   
/*  
* msg.CC.Add("c@c.com");  
* msg.CC.Add("c@c.com"); You can cc more than one person   
*/   
msg.From = new MailAddress("master@boys90.com", "dulei", System.Text.Encoding.UTF8);   
/* The above 3 The parameters are the sender address, the sender name, and the code */   
msg.Subject = " This is the test email ";// Email title    
msg.SubjectEncoding = System.Text.Encoding.UTF8;// Mail subject code    
msg.Body = " Email content ";// Email content    
msg.BodyEncoding = System.Text.Encoding.UTF8;// Message content encoding    
msg.IsBodyHtml = false;// Whether it is HTML mail    
msg.Priority = MailPriority.High;// Email priority    
  
SmtpClient client = new SmtpClient();  
client.Credentials = new System.Net.NetworkCredential("dulei@71info.com", "userpass");   
// in 71info.com Registered email address and password    
client.Host = "smtp.71info.com";   
object userState = msg;   
try   
{   
client.SendAsync(msg, userState);   
// simple 1 Some can client.Send(msg);   
MessageBox.Show(" Send a success ");   
}   
catch (System.Net.Mail.SmtpException ex)   
{   
MessageBox.Show(ex.Message, " Sending an email incorrectly ");   
}   
}


3. SMTP via SSL
public void SendMailUseGmail()    
{   
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();   
msg.To.Add(a@a.com);   
msg.To.Add(b@b.com);   
/*  
 msg.To.Add("b@b.com");  
* msg.To.Add("b@b.com");  
* msg.To.Add("b@b.com"); Can be sent to more than one person   
*/   
msg.CC.Add(c@c.com);   
/*  
* msg.CC.Add("c@c.com");  
* msg.CC.Add("c@c.com"); You can cc more than one person   
*/   
msg.From = new MailAddress("boys90.com", "dulei", System.Text.Encoding.UTF8);   
/* The above 3 The parameters are the sender address, the sender name, and the code */   
msg.Subject = " This is the test email ";// Email title    
msg.SubjectEncoding = System.Text.Encoding.UTF8;// Mail subject code    
msg.Body = " Email content ";// Email content    
msg.BodyEncoding = System.Text.Encoding.UTF8;// Message content encoding    
msg.IsBodyHtml = false;// Whether it is HTML mail    
msg.Priority = MailPriority.High;// Email priority    
SmtpClient client = new SmtpClient();   
client.Credentials = new System.Net.NetworkCredential("boys90com@gmail.com", "password");   
// The above is yours GMail Email and password    
client.Port = 587;//Gmail Ports used    
client.Host = "smtp.gmail.com";   
client.EnableSsl = true;// after ssl encryption    
object userState = msg;   
try   
{   
client.SendAsync(msg, userState);   
// simple 1 Some can client.Send(msg);   
MessageBox.Show(" Send a success ");   
}   
catch (System.Net.Mail.SmtpException ex)   
{   
MessageBox.Show(ex.Message, " Sending an email incorrectly ");   
}   
}


Send email through Gmail, the success rate is very high, almost all can be sent, recommended use, above several methods, I think we have done the development of the use.

Hopefully this article has helped you with your C# programming.


Related articles: