去网上搜c#邮件发送程序时,基本找到的都是零散的函数,没做封装,有的连异常处理都没有;能用的调用起来也不太灵活方便,所以,特地做了个封装。代码中如果有错误,欢迎指正。
以下是程序:
 using System;
using System; using System.Web.Mail;
using System.Web.Mail; using System.Collections;
using System.Collections;
 namespace Asai.Mail
namespace Asai.Mail {
{ 
     /// <summary>
    /// <summary> /// C#邮件发送类 V1.0
    /// C#邮件发送类 V1.0 /// --Author:laifangsong QQ:25313644
    /// --Author:laifangsong QQ:25313644 /// --最后修改:2007-07-27
    /// --最后修改:2007-07-27 ///
    ///  /// 支持批量(发送、抄送、密送)
    /// 支持批量(发送、抄送、密送) /// 支持多语言、带多个附件
    /// 支持多语言、带多个附件 /// 支持smtp发送邮件服务器验证
    /// 支持smtp发送邮件服务器验证 /// </summary>
    /// </summary> public class DotNetSendMail
    public class DotNetSendMail {
    { public DotNetSendMail()
        public DotNetSendMail() {}
        {} 
         /// <summary>
        /// <summary> /// 邮件发送结果。如果发送过程出现错误,该值为捕获到的异常提示;否则,该值为“OK”。
        /// 邮件发送结果。如果发送过程出现错误,该值为捕获到的异常提示;否则,该值为“OK”。 /// </summary>
        /// </summary> public string SendMailResult
        public string SendMailResult {
        { get
            get {
            { return this.m_SendMailResult;
                return this.m_SendMailResult; }
            } set
            set {
            { this.m_SendMailResult = value;
                this.m_SendMailResult = value; }
            } }
        } private string m_SendMailResult;
        private string m_SendMailResult;

 邮件发送时,额外的功能属性设置
        邮件发送时,额外的功能属性设置

 /// <summary>
        /// <summary> /// 发送邮件方法,方法中几个参数是发邮件时所必须的
        /// 发送邮件方法,方法中几个参数是发邮件时所必须的 /// </summary>
        /// </summary> /// <param name="p_From">发件人</param>
        /// <param name="p_From">发件人</param> /// <param name="p_To">邮件人,多个收件人逗号隔开</param>
        /// <param name="p_To">邮件人,多个收件人逗号隔开</param> /// <param name="p_Subject">邮件标题</param>
        /// <param name="p_Subject">邮件标题</param> /// <param name="p_Body">正文</param>
        /// <param name="p_Body">正文</param> /// <param name="p_SmtpServer">发送邮件服务器</param>
        /// <param name="p_SmtpServer">发送邮件服务器</param> /// <param name="p_SmtpUsername">发件服务器登录名</param>
        /// <param name="p_SmtpUsername">发件服务器登录名</param> /// <param name="p_SmtpPassword">发件服务器登录密码</param>
        /// <param name="p_SmtpPassword">发件服务器登录密码</param> /// <returns></returns>
        /// <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)
        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
            try {
            { System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
                System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage(); 
                                 mail.From = p_From;
                mail.From = p_From; mail.To = p_To;
                mail.To = p_To; mail.Subject = p_Subject;
                mail.Subject = p_Subject;     mail.Body = p_Body;
                mail.Body = p_Body; SmtpMail.SmtpServer = p_SmtpServer;
                SmtpMail.SmtpServer = p_SmtpServer; //邮件服务器验证,需要输入您在邮件发送服务器上注册的邮箱用户名和密码
                //邮件服务器验证,需要输入您在邮件发送服务器上注册的邮箱用户名和密码 if(p_SmtpUsername!="")
                if(p_SmtpUsername!="") {
                { mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                    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/sendusername", p_SmtpUsername); mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", p_SmtpPassword);
                    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", p_SmtpPassword); }
                } 
                 //以上是发邮件时所必需的,下面是额外的一些功能设置
                //以上是发邮件时所必需的,下面是额外的一些功能设置 
                 if(this.m_Cc!="")
                if(this.m_Cc!="") {
                { mail.Cc = this.m_Cc;
                    mail.Cc = this.m_Cc; }
                } if(this.m_Bcc!="")
                if(this.m_Bcc!="") {
                { mail.Bcc = this.m_Bcc;
                    mail.Bcc = this.m_Bcc; }
                } switch(this.m_BodyFormat)
                switch(this.m_BodyFormat) {
                { case 1:
                    case 1: mail.BodyFormat = MailFormat.Html;
                        mail.BodyFormat = MailFormat.Html; break;
                        break; case 2:
                    case 2: mail.BodyFormat = MailFormat.Text;
                        mail.BodyFormat = MailFormat.Text; break;
                        break; default:
                    default: break;
                        break; }
                } switch(this.m_MailPriority)
                switch(this.m_MailPriority) {
                { case 1:
                    case 1: mail.Priority = System.Web.Mail.MailPriority.Low;
                        mail.Priority = System.Web.Mail.MailPriority.Low; break;
                        break; case 2:
                    case 2: mail.Priority = System.Web.Mail.MailPriority.Normal;
                        mail.Priority = System.Web.Mail.MailPriority.Normal; break;
                        break; case 3:
                    case 3: mail.Priority = System.Web.Mail.MailPriority.High;
                        mail.Priority = System.Web.Mail.MailPriority.High; break;
                        break; default:
                    default: break;
                        break; }
                } if(this.m_AttachFiles.Count>0)
                if(this.m_AttachFiles.Count>0) {
                { foreach(string file in m_AttachFiles)
                    foreach(string file in m_AttachFiles) {
                    { if(file.Trim()!="")
                        if(file.Trim()!="") {
                        { mail.Attachments.Add(new MailAttachment(file.Trim()));
                            mail.Attachments.Add(new MailAttachment(file.Trim())); }
                        } }
                    } }
                }
 SmtpMail.Send(mail);
                SmtpMail.Send(mail); this.m_SendMailResult = "OK";
                this.m_SendMailResult = "OK"; return true;
                return true; }
            } catch(Exception ex)
            catch(Exception ex) {
            { this.m_SendMailResult = ex.ToString();
                this.m_SendMailResult = ex.ToString(); return false;
                return false; }
            }             
         }
        }
 }
    } }
}
调用:

 DotNetSendMail sendMail = new DotNetSendMail();
DotNetSendMail sendMail = new DotNetSendMail(); //sendMail.Cc = "laifangsong##gmail.com";
//sendMail.Cc = "laifangsong##gmail.com"; //sendMail.Bcc = "laifangsong##gmail.com";
//sendMail.Bcc = "laifangsong##gmail.com"; //sendMail.BodyFormat = 2;
//sendMail.BodyFormat = 2; //sendMail.MailPriority = 3;
//sendMail.MailPriority = 3; //sendMail.AttachFiles.Add(Server.MapPath("attachFile.txt"));
//sendMail.AttachFiles.Add(Server.MapPath("attachFile.txt")); bool sendResultFlag = sendMail.SendMail("laifangsong##126.com","laifs##163.com","标题","内容","smtp.126.com","laifangsong##126.com","123456");
bool sendResultFlag = sendMail.SendMail("laifangsong##126.com","laifs##163.com","标题","内容","smtp.126.com","laifangsong##126.com","123456"); if(sendResultFlag)
if(sendResultFlag) {
{ Response.Write("发送成功!");
    Response.Write("发送成功!"); }
} else
else {
{ Response.Write("发送失败!<br/>");
    Response.Write("发送失败!<br/>"); Response.Write("以下是错误信息:<br/><br/>");
    Response.Write("以下是错误信息:<br/><br/>"); Response.Write(sendMail.SendMailResult);
    Response.Write(sendMail.SendMailResult); Response.End();
    Response.End(); }
}组件及源码下载:/Files/jiny-z/csharp_mail.rar
 
                    
                 


 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号