http://yeak2001.iteye.com/blog/859770
(转)C#发送邮件三种方法,Localhost,SMTP,SSL-SMTP
通过.Net FrameWork 2.0下提供的“System.Net.Mail”可以轻松的实现,本文列举了3种途径来发送:
1.通过Localhost;
2.通过普通SMTP;
3.通过SSL的SMTP;
下面一个一个来说:
1.通过LocalHost
C#代码
- public void SendMailLocalhost()
- {
- System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
- msg.To.Add("[email protected]");
- msg.To.Add("[email protected]");
- msg.CC.Add([email protected]);
- msg.From = new MailAddress("[email protected]com", "AlphaWu", System.Text.Encoding.UTF8);
- msg.Subject = "这是测试邮件";//邮件标题
- msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
- msg.Body = "邮件内容";//邮件内容
- msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
- 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, "发送邮件出错");
- }
- }
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.UTF8);
/* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/
msg.Subject = "这是测试邮件";//邮件标题
msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
msg.Body = "邮件内容";//邮件内容
msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
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, "发送邮件出错");
}
}
2.通过普通SMTP
C#代码
- public void SendMailUseZj()
- {
- System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
- msg.To.Add([email protected]);
- msg.To.Add([email protected]);
- msg.CC.Add("[email protected]");
- msg.From = new MailAddress("[email protected]", "AlphaWu", System.Text.Encoding.UTF8);
- msg.Subject = "这是测试邮件";//邮件标题
- msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
- msg.Body = "邮件内容";//邮件内容
- msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
- 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, "发送邮件出错");
- }
- }
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.UTF8);
/* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/
msg.Subject = "这是测试邮件";//邮件标题
msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
msg.Body = "邮件内容";//邮件内容
msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
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.163.com不行
3.通过SSL的SMTP
C#代码
- public void SendMailUseGmail()
- {
- System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
- msg.To.Add([email protected]);
- msg.To.Add([email protected]);
- msg.CC.Add([email protected]);
- msg.From = new MailAddress("[email protected]", "AlphaWu", System.Text.Encoding.UTF8);
- msg.Subject = "这是测试邮件";//邮件标题
- msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
- msg.Body = "邮件内容";//邮件内容
- msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
- msg.IsBodyHtml = false;//是否是HTML邮件
- msg.Priority = MailPriority.High;//邮件优先级
- SmtpClient client = new SmtpClient();
- client.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
- //上述写你的GMail邮箱和密码
- client.Port = 587;//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, "发送邮件出错");
- }
- }
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.UTF8);
/* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/
msg.Subject = "这是测试邮件";//邮件标题
msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
msg.Body = "邮件内容";//邮件内容
msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
msg.IsBodyHtml = false;//是否是HTML邮件
msg.Priority = MailPriority.High;//邮件优先级
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
//上述写你的GMail邮箱和密码
client.Port = 587;//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来发送邮件,成功率极高,几乎都可以发到,推荐使用。