NET 邮件发送

/******************************* * *  文件名:SendEmail.cs *  copyright(C) 2007-2008 星星 *  文件编号:01 *  创 建 人:星星 *  日    期:2007-05-31 *  修 改 人:星星 *  日    期:2007-05-31 *  描    述:发送Email的类 * ********************************/ namespace SendMail {     using System;     using System.Net;     using System.Net.Mail;     using System.Net.Configuration;     using System.Web.Configuration;     /// <summary>     /// 可以发送多种邮件     ///  gmail的smtp采用了ssl连接:   (aspnetxm@gmail.com,jsp.china@gmail.com)     ///  Outgoing Mail (SMTP) Server - requires TLS: smtp.gmail.com (use authentication)     ///  Use Authentication: Yes     ///  Use STARTTLS: Yes (some clients call this SSL)     ///  Port: 465 or 587, 25     ///  其他的 port设为<0则使用默认的 Use STARTTLS: no     /// </summary>     public class EMail     {         private string m_Host;//smtp.gmail.com         private int m_Port, m_Encoding; //端口,编码         private bool m_EnableSsl, m_IsBodyHtml, m_defaultCredentials; //加密, HTMl格式,身份验证         private string m_SmtpUserName, m_SmtpUserPassword,m_FromMail;         /// <summary>         /// 用于web.config中已经配置好邮件帐号         /// </summary>         public EMail()         {             this.m_EnableSsl    = false;             this.m_IsBodyHtml   = false;             this.m_Encoding     = 936;             //从web.config读取邮件配置             System.Configuration.Configuration config   = WebConfigurationManager.OpenWebConfiguration("~/");             MailSettingsSectionGroup mailSettings       = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");             this.m_SmtpUserName         = mailSettings.Smtp.Network.UserName;             this.m_SmtpUserPassword     = mailSettings.Smtp.Network.Password;             this.m_FromMail             = mailSettings.Smtp.From;             this.m_defaultCredentials   = mailSettings.Smtp.Network.DefaultCredentials;         }         /// <summary>         /// 直接设置邮件帐号         /// </summary>         /// <param name="Port">0为默认设置</param>         public EMail(string SmtpUserName, string SmtpUserPassword, string Host, int Port, bool DefaultCredentials)         {             this.m_Host     = Host;             this.m_Port     = Port;             this.m_Encoding = 936;             this.m_EnableSsl    = false;             this.m_IsBodyHtml   = false;             this.m_EnableSsl    = false;             this.m_defaultCredentials   = DefaultCredentials;             this.m_SmtpUserName         = SmtpUserName;             this.m_SmtpUserPassword     = SmtpUserPassword;         }         //是否以HTML格式发送         public bool IsBodyHtml         {             set { this.m_IsBodyHtml = value; }             get { return this.m_IsBodyHtml; }         }         //编码方式         public int Encoding         {             set { this.m_Encoding = value; }             get { return this.m_Encoding; }         }         //是否加密         public bool EnableSsl         {             set { this.m_EnableSsl = value; }             get { return this.m_EnableSsl; }         }         /// <summary>         ///直接设置邮件帐号的发送方法 return: true ,false         /// </summary>         /// <param name="CC, Bcc, FileName">可以为null</param>         public bool Send(string From, string To, string Subject, string Body, string[] CC, string[] Bcc, string FileName)         {             //Create MailMessage             MailMessage mailMessage = new MailMessage(From, To, Subject, Body);             mailMessage.IsBodyHtml = this.m_IsBodyHtml;                                             //Body is Html             mailMessage.SubjectEncoding = System.Text.Encoding.GetEncoding(this.m_Encoding);        //Mail Subject Encoding             mailMessage.BodyEncoding = System.Text.Encoding.GetEncoding(this.m_Encoding);           //Mail Body Encoding                         if (!String.IsNullOrEmpty(FileName))             {   // add accessories                 mailMessage.Attachments.Add(new Attachment(FileName));             }             if (CC != null && CC.Length > 0)             {                 foreach (string ccAddress in CC)                 {                     mailMessage.CC.Add(new MailAddress(ccAddress));                 }             }             if (Bcc != null && Bcc.Length > 0)             {                 foreach (string bccAddress in Bcc)                 {                     mailMessage.Bcc.Add(bccAddress);                 }             }             //Send Email             SmtpClient smtpClient = new SmtpClient(this.m_Host);             if (this.m_Port > 0)                 smtpClient.Port = this.m_Port;             smtpClient.EnableSsl = this.m_EnableSsl;             try             {                 if (this.m_defaultCredentials) //要身份验证                     smtpClient.Credentials = new NetworkCredential(this.m_SmtpUserName, this.m_SmtpUserPassword);                 smtpClient.Send(mailMessage);                 return true;             }             catch             {                 return false;             }         }         /// <summary>         /// 用于web.config中已经配置好邮件帐号 的发送方法 return: true ,false         /// </summary>         /// <param name="CC, Bcc, FileName">可以为null</param>         public bool Send( string To, string Subject, string Body, string[] CC, string[] Bcc, string FileName)         {             //Create MailMessage             MailMessage mailMessage = new MailMessage(m_FromMail, To, Subject, Body);             mailMessage.IsBodyHtml = this.m_IsBodyHtml;                                             //Body is Html             mailMessage.SubjectEncoding = System.Text.Encoding.GetEncoding(this.m_Encoding);        //Mail Subject Encoding             mailMessage.BodyEncoding = System.Text.Encoding.GetEncoding(this.m_Encoding);           //Mail Body Encoding             if (!String.IsNullOrEmpty(FileName))             {   // add accessories                 mailMessage.Attachments.Add(new Attachment(FileName));             }             if (CC != null && CC.Length > 0)             {                 foreach (string ccAddress in CC)                 {                     mailMessage.CC.Add(new MailAddress(ccAddress));                 }             }             if (Bcc != null && Bcc.Length > 0)             {                 foreach (string bccAddress in Bcc)                 {                     mailMessage.Bcc.Add(bccAddress);                 }             }             //Send Email             SmtpClient smtpClient = new SmtpClient(this.m_Host);             smtpClient.EnableSsl = this.m_EnableSsl;             try             {                 if (this.m_defaultCredentials)      //要身份验证                     smtpClient.Credentials = new NetworkCredential(this.m_SmtpUserName, this.m_SmtpUserPassword);                 smtpClient.Send(mailMessage);                 return true;             }             catch             {                 return false;             }         }     } } //////////////////////// using System; using System.Data; using System.Configuration; using System.Collections; 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.Net.Mail; using System.Net; using System.Net.Configuration; using System.Web.Configuration; using SendMail; public partial class SendEmail : System.Web.UI.Page {     protected void Page_Load(object sender, EventArgs e)     {     }     protected void Btn_Click(object sender, EventArgs e)     {         string strEmailAdr = "myairen100@126.com", strShow;         /*//邮件直接配置         EMail mail = new EMail("aspnettest", "aspnett", "smtp.gmail.com", 25,true);         bool bResult = mail.Send("aspnetxm@gmail.com", strEmailAdr, "测试Gmail", "ok", null, null, null);         */         //邮件在web.config中配置         EMail mail = new EMail();         mail.EnableSsl = true;         bool bResult = mail.Send( strEmailAdr, "测试Gmail", "ok", null, null, null);                 if (bResult)            strShow = "邮件发送成功。";         else             strShow = "邮件发送失败";         Response.Write(strShow);             } }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值