去网上搜c#邮件发送程序时,基本找到的都是零散的函数,没做封装,有的连异常处理都没有;能用的调用起来也不太灵活方便,所以,特地做了个封装。代码中如果有错误,欢迎指正。
以下是程序:
using System;
using System.Web.Mail;
using System.Collections;
namespace Asai.Mail
{
/// <summary>
/// C#邮件发送类 V1.0
/// --Author:laifangsong QQ:25313644
/// --最后修改:2007-07-27
///
/// 支持批量(发送、抄送、密送)
/// 支持多语言、带多个附件
/// 支持smtp发送邮件服务器验证
/// </summary>
public class DotNetSendMail
{
public DotNetSendMail()
{}
/// <summary>
/// 邮件发送结果。如果发送过程出现错误,该值为捕获到的异常提示;否则,该值为“OK”。
/// </summary>
public string SendMailResult
{
get
{
return this.m_SendMailResult;
}
set
{
this.m_SendMailResult = value;
}
}
private string m_SendMailResult;
邮件发送时,额外的功能属性设置
/// <summary>
/// 发送邮件方法,方法中几个参数是发邮件时所必须的
/// </summary>
/// <param name="p_From">发件人</param>
/// <param name="p_To">邮件人,多个收件人逗号隔开</param>
/// <param name="p_Subject">邮件标题</param>
/// <param name="p_Body">正文</param>
/// <param name="p_SmtpServer">发送邮件服务器</param>
/// <param name="p_SmtpUsername">发件服务器登录名</param>
/// <param name="p_SmtpPassword">发件服务器登录密码</param>
/// <returns></returns>
public bool SendMail(string p_From, string p_To, string p_Subject, string p_Body, string p_SmtpServer, string p_SmtpUsername, string p_SmtpPassword)
{
try
{
System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
mail.From = p_From;
mail.To = p_To;
mail.Subject = p_Subject;
mail.Body = p_Body;
SmtpMail.SmtpServer = p_SmtpServer;
//邮件服务器验证,需要输入您在邮件发送服务器上注册的邮箱用户名和密码
if(p_SmtpUsername!="")
{
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", p_SmtpUsername);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", p_SmtpPassword);
}
//以上是发邮件时所必需的,下面是额外的一些功能设置
if(this.m_Cc!="")
{
mail.Cc = this.m_Cc;
}
if(this.m_Bcc!="")
{
mail.Bcc = this.m_Bcc;
}
switch(this.m_BodyFormat)
{
case 1:
mail.BodyFormat = MailFormat.Html;
break;
case 2:
mail.BodyFormat = MailFormat.Text;
break;
default:
break;
}
switch(this.m_MailPriority)
{
case 1:
mail.Priority = System.Web.Mail.MailPriority.Low;
break;
case 2:
mail.Priority = System.Web.Mail.MailPriority.Normal;
break;
case 3:
mail.Priority = System.Web.Mail.MailPriority.High;
break;
default:
break;
}
if(this.m_AttachFiles.Count>0)
{
foreach(string file in m_AttachFiles)
{
if(file.Trim()!="")
{
mail.Attachments.Add(new MailAttachment(file.Trim()));
}
}
}
SmtpMail.Send(mail);
this.m_SendMailResult = "OK";
return true;
}
catch(Exception ex)
{
this.m_SendMailResult = ex.ToString();
return false;
}
}
}
}
using System.Web.Mail;
using System.Collections;
namespace Asai.Mail
{
/// <summary>
/// C#邮件发送类 V1.0
/// --Author:laifangsong QQ:25313644
/// --最后修改:2007-07-27
///
/// 支持批量(发送、抄送、密送)
/// 支持多语言、带多个附件
/// 支持smtp发送邮件服务器验证
/// </summary>
public class DotNetSendMail
{
public DotNetSendMail()
{}
/// <summary>
/// 邮件发送结果。如果发送过程出现错误,该值为捕获到的异常提示;否则,该值为“OK”。
/// </summary>
public string SendMailResult
{
get
{
return this.m_SendMailResult;
}
set
{
this.m_SendMailResult = value;
}
}
private string m_SendMailResult;
邮件发送时,额外的功能属性设置
/// <summary>
/// 发送邮件方法,方法中几个参数是发邮件时所必须的
/// </summary>
/// <param name="p_From">发件人</param>
/// <param name="p_To">邮件人,多个收件人逗号隔开</param>
/// <param name="p_Subject">邮件标题</param>
/// <param name="p_Body">正文</param>
/// <param name="p_SmtpServer">发送邮件服务器</param>
/// <param name="p_SmtpUsername">发件服务器登录名</param>
/// <param name="p_SmtpPassword">发件服务器登录密码</param>
/// <returns></returns>
public bool SendMail(string p_From, string p_To, string p_Subject, string p_Body, string p_SmtpServer, string p_SmtpUsername, string p_SmtpPassword)
{
try
{
System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
mail.From = p_From;
mail.To = p_To;
mail.Subject = p_Subject;
mail.Body = p_Body;
SmtpMail.SmtpServer = p_SmtpServer;
//邮件服务器验证,需要输入您在邮件发送服务器上注册的邮箱用户名和密码
if(p_SmtpUsername!="")
{
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", p_SmtpUsername);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", p_SmtpPassword);
}
//以上是发邮件时所必需的,下面是额外的一些功能设置
if(this.m_Cc!="")
{
mail.Cc = this.m_Cc;
}
if(this.m_Bcc!="")
{
mail.Bcc = this.m_Bcc;
}
switch(this.m_BodyFormat)
{
case 1:
mail.BodyFormat = MailFormat.Html;
break;
case 2:
mail.BodyFormat = MailFormat.Text;
break;
default:
break;
}
switch(this.m_MailPriority)
{
case 1:
mail.Priority = System.Web.Mail.MailPriority.Low;
break;
case 2:
mail.Priority = System.Web.Mail.MailPriority.Normal;
break;
case 3:
mail.Priority = System.Web.Mail.MailPriority.High;
break;
default:
break;
}
if(this.m_AttachFiles.Count>0)
{
foreach(string file in m_AttachFiles)
{
if(file.Trim()!="")
{
mail.Attachments.Add(new MailAttachment(file.Trim()));
}
}
}
SmtpMail.Send(mail);
this.m_SendMailResult = "OK";
return true;
}
catch(Exception ex)
{
this.m_SendMailResult = ex.ToString();
return false;
}
}
}
}
调用:
DotNetSendMail sendMail = new DotNetSendMail();
//sendMail.Cc = "laifangsong##gmail.com";
//sendMail.Bcc = "laifangsong##gmail.com";
//sendMail.BodyFormat = 2;
//sendMail.MailPriority = 3;
//sendMail.AttachFiles.Add(Server.MapPath("attachFile.txt"));
bool sendResultFlag = sendMail.SendMail("laifangsong##126.com","laifs##163.com","标题","内容","smtp.126.com","laifangsong##126.com","123456");
if(sendResultFlag)
{
Response.Write("发送成功!");
}
else
{
Response.Write("发送失败!<br/>");
Response.Write("以下是错误信息:<br/><br/>");
Response.Write(sendMail.SendMailResult);
Response.End();
}
组件及源码下载:/Files/jiny-z/csharp_mail.rar