导航

ASP.NET与GMail免费SMTP服务器

Posted on 2013-03-05 20:52  유람객  阅读(193)  评论(0编辑  收藏  举报
http://blog.csdn.net/TangZhongxin/article/details/3932734
 

// ASP.NET与GMail免费SMTP服务器
// using System.Net.Mail;

MailMessage message = new MailMessage();
message.From = new MailAddress("User@gmail.com"); //...new MailAddress("User@gmail.com", "显示的名字");
message.To.Add(new MailAddress("to@gmail.com"));

message.Subject = "邮件标题";
//message.BodyEncoding = System.Text.Encoding.UTF8;
message.Body = "邮件内容";
//message.Priority = MailPriority.High;

SmtpClient client = new SmtpClient();

// Gmail使用的端口号 - 587
// ... new SmtpClient("smtp.gmail.com", 587);

// 你的用户名与密码
// client.Credentials = new System.Net.NetworkCredential("User@gmail.com", "*****");

client.EnableSsl = true; //经过ssl加密,GMail很重要

try
{
client.Send(message);
Response.Write("邮件发送到" + message.To.ToString() + "<br>");
}
catch (Exception ee)
{
Response.Write(ee.Message);
//ee.InnerException.messageage
}

//In web.config
/*
<system.net>
<mailSettings>
<smtp from="from@gmail.com">
<network host="smtp.gmail.com" port="587" userName="User@gmail.com" password="your pwd" />
<!-- if has 'defaultCredentials="true"' , using Gmail can not send success -->
</smtp>
</mailSettings>
</system.net>
*/