发送邮件的两种方式

[size=large][b]一、实现代码如下:[/b][/size]

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Net.Mail;
using System.Net;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string sendMailContent = "我是用sendMail方式发送的";
string CDOsendMailContent = "我是用CDOsendMail方式发送的";


bool rr = true;
bool rr1 = true;

//使用企业邮箱发送邮件-------------------------------------------
rr = SenMailBySMTP("primeton@micromarketing.com.cn", "njm@biaodashi.com", "表达式测试", sendMailContent, "mail.micromarketing.com.cn", "primeton@micromarketing.com.cn", "******");
rr1 = CDOsendMail("primeton@micromarketing.com.cn", "表达式测试", "primeton@micromarketing.com.cn", "primeton@micromarketing.com.cn", "primeton@micromarketing.com.cn", "******", "mail.micromarketing.com.cn", "njm@biaodashi.com", CDOsendMailContent);
//------------------------------------------------------------

//使用hotmail及gmail邮箱发送邮件---------------------------------
//rr = SenMailBySMTP("njm168@hotmail.com", "njm@biaodashi.com", "表达式测试", sendMailContent, "smtp.live.com", "njm168@hotmail.com", "******");
//rr1 = CDOsendMail("njm168@hotmail.com", "表达式测试", "njm168@hotmail.com", "njm168@hotmail.com", "njm168@hotmail.com", "******", "smtp.live.com", "njm@biaodashi.com", CDOsendMailContent);
//------------------------------------------------------------

if (rr)
{
Response.Write("sendmailbystmp发送成功");
}
else
{
Response.Write("sendmailbystmp发送失败");
}

if (rr1)
{
Response.Write("CDOsendMail发送成功");
}
else
{
Response.Write("CDOsendMail发送失败");
}
}

/// <summary>
/// 使用CDO组件发送邮件CDO是Collaboration Data Objects的简称,它是一组高层的COM对象集合,并经历了好几个版本的演化,现在在Windows2000和Exchange2000中使用的都是CDO2.0的版本(分别为cdosys.dll和cdoex.dll)。CDOSYS构建在SMTP协议和NNTP协议之上,并且作为Windows2000 Server的组件被安装,您可以在系统目录(如c:\winnt或c:\windows)的system32子目录中找到它(cdosys.dll)。CDO组件相对于先前介绍的SmtpMail对象功能更为丰富,并提供了一些SmtpMail类所没有提供的功能,如通过需要认证的SMTP服务器发送邮件等。
/// </summary>
/// <param name="from">from名称</param>
/// <param name="subject">主题</param>
/// <param name="senderEmail">发送者email</param>
/// <param name="emailAccount">发送者账号</param>
/// <param name="username"></param>
/// <param name="password"></param>
/// <param name="smtp"></param>
/// <param name="toEmail"></param>
/// <param name="content"></param>
/// <returns></returns>
public bool CDOsendMail(string from, string subject, string senderEmail, string emailAccount, string username, string password, string smtp, string toEmail, string content)
{
try
{
CDO.Message oMsg = new CDO.Message();

oMsg.From = "表达式邮件服务中心<" + from + ">";//from;
oMsg.To = toEmail;
oMsg.Subject = subject;
//oMsg.HTMLBody = "<html><body><b>大家好!!</b><img src=\"http://www.micromarketing.com.cn/jmail/count.aspx?id=505&email=<收件人地址>\" width=0 height=0>link</a></body></html>";
oMsg.HTMLBody = content;

CDO.IConfiguration iConfg = oMsg.Configuration;
ADODB.Fields oFields = iConfg.Fields;

oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;

oFields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"]
.Value = senderEmail; //sender mail

oFields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"]
.Value = emailAccount; //email account

oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"]
.Value = username;
oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"]
.Value = password;

oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"]
.Value = 1;

//value=0 代表Anonymous验证方式(不需要验证)
//value=1 代表Basic验证方式(使用basic (clear-text) authentication.
//The configuration sendusername/sendpassword or postusername/postpassword fields
// are used to specify credentials.)
//Value=2 代表NTLM验证方式(Secure Password Authentication in Microsoft Outlook Express)

oFields["http://schemas.microsoft.com/cdo/configuration/languagecode"].Value = 0x0804;
oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = smtp;

//oFields["http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"].Value = 60;//超时设置, 以秒为单位

//hotmail、gmail邮箱要求启用以下两项
//oFields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = 25;
//oFields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"].Value = true; //'是否使用套接字 true/false


oFields.Update();
oMsg.BodyPart.Charset = "gb2312";
oMsg.HTMLBodyPart.Charset = "gb2312";

oMsg.Send();
oMsg = null;


}
catch (Exception e)
{
Response.Write(e.Message);
return false;

}

return true;

}


/// <summary>
/// 通过smtp直接发送邮件
/// </summary>
/// <param name="fromEmail"></param>
/// <param name="toEmail"></param>
/// <param name="subject"></param>
/// <param name="body"></param>
/// <param name="smtp"></param>
/// <param name="account"></param>
/// <param name="pwd"></param>
/// <returns></returns>
protected bool SenMailBySMTP(string fromEmail, string toEmail, string subject, string body, string smtpAddress, string account, string pwd)
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(fromEmail, toEmail);

//mail
mail.From = new MailAddress(fromEmail, "表达式邮件服务中心", Encoding.UTF8);
mail.SubjectEncoding = Encoding.UTF8;
mail.Subject = subject;
mail.IsBodyHtml = true; //是否允许内容为 HTML 格式
mail.BodyEncoding = Encoding.UTF8;
mail.Body = body;

//mail.Attachments.Add(new Attachment("E:\\foo.txt")); //添加一个附件

SmtpClient smtp = new SmtpClient(smtpAddress);

//hotmail、gmail邮箱要求启用以下两项
//smtp.Port = 25; //smtp.Port = 587;//Gmail使用的端口
//smtp.EnableSsl = true;


smtp.Credentials = new NetworkCredential(account, pwd); //SMTP 验证
//smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

bool rr = true;

try
{
smtp.Send(mail);
}
catch (Exception ex)
{
Response.Write(ex.Message);
rr = false;
}

mail.Attachments.Dispose(); //邮件发送完毕,释放对附件的锁定


return rr;

}
}


[color=red][size=large][b]注:组件支持在附件中[/b][/size][/color]

[size=large][b]二、可选项内容(参考)[/b][/size]
附錄 A Web儲存系統結構描述屬性
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值