ASP.NET发送邮件Gmail篇(转)
来源:互联网 发布:软件可靠性三个指标 编辑:程序博客网 时间:2024/06/03 20:11
由于需要在Starlight Portal中提供邮件发送功能,所以在网上找了一些asp.net中发送邮件的文章,可是都不能满足需求。因为大部分的文章都介绍得很简单,只是告诉你怎么用MailMessage,而我想用Gmail的邮箱发送信件,因此会碰到一些额外的问题,比如:
- Gmail的Smtp端口不是默认的25,而是465
- Gmail的Smtp采用的是SSL连接
因此,要发送邮件,就必须解决这两个问题,其他的基本问题都可以很容易的找到解决方法。在这里记下代码,以供大家使用。
MailMessage msg = new MailMessage();
msg.From = settings.SystemEmailAccount;
msg.To = to;
msg.Subject = subject;
msg.Body = body;
if(settings.SmtpAuthenticationRequired)
{
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1" );
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", settings.SystemEmailAccount);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", settings.SystemEmailAccountPassword);
}
if(settings.SmtpPort != 25)
{
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", settings.SmtpPort.ToString());
}
if(settings.SmtpUseSSL)
{
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "1");
}
SmtpMail.SmtpServer = settings.SmtpServer;
SmtpMail.Send(msg);
msg.From = settings.SystemEmailAccount;
msg.To = to;
msg.Subject = subject;
msg.Body = body;
if(settings.SmtpAuthenticationRequired)
{
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1" );
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", settings.SystemEmailAccount);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", settings.SystemEmailAccountPassword);
}
if(settings.SmtpPort != 25)
{
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", settings.SmtpPort.ToString());
}
if(settings.SmtpUseSSL)
{
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "1");
}
SmtpMail.SmtpServer = settings.SmtpServer;
SmtpMail.Send(msg);
- ASP.NET发送邮件Gmail篇(转)
- asp.net发送邮件代码(Gmail、163能发)
- 利用asp.net自带邮件组件 发送邮件,支持126,163,gmail,qq等邮箱
- 在.net中使用Gmail发送邮件
- 在.net中使用Gmail发送邮件
- 在.net中使用Gmail发送邮件
- asp.net发送邮件
- ASP.NET发送邮件
- asp.net发送邮件
- asp.net发送邮件
- Asp.net发送邮件
- asp.net 发送邮件
- asp.net 发送邮件
- asp.net邮件发送
- asp.net 邮件发送
- asp.net发送邮件
- ASP.NET发送邮件
- asp.net发送邮件
- 文件上传时生成“日期+随机数”式文件名前缀的Java代码
- 图象素材、矢量图库、字体网站整理集合
- ibatis in not in备案
- 数据库水平分库框架设计
- First time
- ASP.NET发送邮件Gmail篇(转)
- 2009-02-16读书记录:《代码大全》(第一版)第十二章——复杂数据类型*
- delphi 字符串函数分解
- Summary of Design Heuristics -摘录至Code Complete, Second Edition
- oracle查找并杀掉锁表及对象的session及相关系统进程
- 简单工厂设计模式
- DHC log 2009-2-16
- 程序员的十层楼(1~3层)
- 测试数据库链接数