C#发送Email邮件方法总结

通过.Net FrameWork .下提供的“System.Net.Mail”可以轻松的实现,本文列举了种途径来发送:
.通过Localhost;
.通过普通SMTP;
.通过SSL的SMTP;

下面一个一个来说:

.通过LocalHost

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]");可以发送给多人 
 
*/
 
 msg.CC.Add(
"[email protected]"); 
 
/**//* 
 * msg.CC.Add("[email protected]"); 
 * msg.CC.Add("[email protected]");可以抄送给多人 
 
*/
 
 msg.From 
= new MailAddress("[email protected]""AlphaWu", System.Text.Encoding.UTF); 
 
/**//* 上面个参数分别是发件人地址(可以随便写),发件人姓名,编码*/ 
 msg.Subject 
= "这是测试邮件";//邮件标题 
 msg.SubjectEncoding = System.Text.Encoding.UTF;//邮件标题编码 
 msg.Body = "邮件内容";//邮件内容 
 msg.BodyEncoding = System.Text.Encoding.UTF;//邮件内容编码 
 msg.IsBodyHtml = false;//是否是HTML邮件 
 msg.Priority = MailPriority.High;//邮件优先级 
 
 SmtpClient client 
= new SmtpClient(); 
 client.Host 
= "localhost"
 
object userState = msg; 
 
try 
 

 client.SendAsync(msg, userState); 
 
//简单一点儿可以client.Send(msg); 
 MessageBox.Show("发送成功"); 
 }
 
 
catch (System.Net.Mail.SmtpException ex) 
 

 MessageBox.Show(ex.Message, 
"发送邮件出错"); 
 }
 
 }
 
.通过普通SMTP
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]");可以发送给多人 
 
*/
 
 msg.CC.Add(
"[email protected]"); 
 
/**//* 
 * msg.CC.Add("[email protected]"); 
 * msg.CC.Add("[email protected]");可以抄送给多人 
 
*/
 
 msg.From 
= new MailAddress("[email protected]""AlphaWu", System.Text.Encoding.UTF); 
 
/**//* 上面个参数分别是发件人地址(可以随便写),发件人姓名,编码*/ 
 msg.Subject 
= "这是测试邮件";//邮件标题 
 msg.SubjectEncoding = System.Text.Encoding.UTF;//邮件标题编码 
 msg.Body = "邮件内容";//邮件内容 
 msg.BodyEncoding = System.Text.Encoding.UTF;//邮件内容编码 
 msg.IsBodyHtml = false;//是否是HTML邮件 
 msg.Priority = MailPriority.High;//邮件优先级 
 
 SmtpClient client 
= new SmtpClient(); 
 client.Credentials 
= new System.Net.NetworkCredential("[email protected]""userpass"); 
 
//在zj.com注册的邮箱和密码 
 client.Host = "smtp.zj.com"
 
object userState = msg; 
 
try 
 

 client.SendAsync(msg, userState); 
 
//简单一点儿可以client.Send(msg); 
 MessageBox.Show("发送成功"); 
 }
 
 
catch (System.Net.Mail.SmtpException ex) 
 

 MessageBox.Show(ex.Message, 
"发送邮件出错"); 
 }
 
 }
 
上述方法不适用于所有SMTP,经测试zj.com可以,而smtp..com不行

.通过SSL的SMTP

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]");可以发送给多人 
 
*/
 
 msg.CC.Add(
"[email protected]"); 
 
/**//* 
 * msg.CC.Add("[email protected]"); 
 * msg.CC.Add("[email protected]");可以抄送给多人 
 
*/
 
 msg.From 
= new MailAddress("[email protected]""AlphaWu", System.Text.Encoding.UTF); 
 
/**//* 上面个参数分别是发件人地址(可以随便写),发件人姓名,编码*/ 
 msg.Subject 
= "这是测试邮件";//邮件标题 
 msg.SubjectEncoding = System.Text.Encoding.UTF;//邮件标题编码 
 msg.Body = "邮件内容";//邮件内容 
 msg.BodyEncoding = System.Text.Encoding.UTF;//邮件内容编码 
 msg.IsBodyHtml = false;//是否是HTML邮件 
 msg.Priority = MailPriority.High;//邮件优先级 
 
 SmtpClient client 
= new SmtpClient(); 
 client.Credentials 
= new System.Net.NetworkCredential("[email protected]""password"); 
 
//上述写你的GMail邮箱和密码 
 client.Port = ;//Gmail使用的端口 
 client.Host = "smtp.gmail.com"
 client.EnableSsl 
= true;//经过ssl加密 
 object userState = msg; 
 
try 
 

 client.SendAsync(msg, userState); 
 
//简单一点儿可以client.Send(msg); 
 MessageBox.Show("发送成功"); 
 }
 
 
catch (System.Net.Mail.SmtpException ex) 
 

 MessageBox.Show(ex.Message, 
"发送邮件出错"); 
 }
 
 }
 

通过Gmail来发送邮件,成功率极高,几乎都可以发到,推荐使用。

你可能感兴趣的:(email)