C#.NET发送EMAIL的几种方法

C#.NET发送EMAIL的常用几种方法 MailMessage/SmtpClient/CDO.Message

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Net.Mail;
using System.Net;

namespace Pub.Class
{
/// <summary>
/// 发送Email类
/// </summary>
public class Email
{
#region 私有成员
private static object lockHelper = new object();
private string _From;
private string _FromEmail;
private string _Subject;
private string _Body;
private string _SmtpServer;
private string _SmtpPort = "25";
private string _SmtpUserName;
private string _SmtpPassword;
private System.Web.Mail.MailFormat _Format = System.Web.Mail.MailFormat.Html;
private System.Text.Encoding _Encoding = System.Text.Encoding.Default;
#endregion

#region 属性
/// <summary>
/// 正文内容类型
/// </summary>
public System.Web.Mail.MailFormat Format { set { _Format = value; } }
/// <summary>
/// 正文内容编码
/// </summary>
public System.Text.Encoding Encoding { set { _Encoding = value; } }
/// <summary>
/// FromEmail 发送方地址(如test@163.com)
/// </summary>
public string FromEmail { set { _FromEmail = value; } }
/// <summary>
/// From
/// </summary>
public string From { set { _From = value; } }
/// <summary>
/// 主题
/// </summary>
public string Subject { set { _Subject = value; } }
/// <summary>
/// 内容
/// </summary>
public string Body { set { _Body = value; } }
/// <summary>
/// SmtpServer
/// </summary>
public string SmtpServer { set { _SmtpServer = value; } }
/// <summary>
/// SmtpPort
/// </summary>
public string SmtpPort { set { _SmtpPort = value; } }
/// <summary>
/// SmtpUserName
/// </summary>
public string SmtpUserName { set { _SmtpUserName = value; } }
/// <summary>
/// SmtpPassword
/// </summary>
public string SmtpPassword { set { _SmtpPassword = value; } }
#endregion

#region 构造器
/// <summary>
/// 构造器
/// </summary>
public Email() { }
#endregion

#region Send
/// <summary>
/// 发送EMAIL
/// </summary>
/// <example>
/// <code>
/// Email _Email = new Email();
/// _Email.FromEmail = "test@163.com";
/// _Email.Subject = "&lt;div>aaaa&lt;/div>";
/// _Email.Body = "aaaaaaaaaaaaa";
/// _Email.SmtpServer = "smtp.163.com";
/// _Email.SmtpUserName = "aaa";
/// _Email.SmtpPassword = "aaa";
/// _Email.Send("test@163.com");
/// </code>
/// </example>
/// <param name="toEmail">收信人 接收方地址</param>
/// <returns>成功否</returns>
public bool SmtpMailSend(string toEmail) {
lock (lockHelper) {
System.Web.Mail.MailMessage msg = new System.Web.Mail.MailMessage();
try {
msg.From = _FromEmail;//发送方地址(如test@163.com)
msg.To = toEmail;//接收方地址
msg.BodyFormat = _Format;//正文内容类型
msg.BodyEncoding = _Encoding;//正文内容编码
msg.Subject = _Subject;//主题
msg.Body = _Body;//内容
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");//设置为需要用户验证
if (!_SmtpPort.Equals("25")) msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", _SmtpPort);//设置端口
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", _SmtpUserName);//设置验证用户名
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", _SmtpPassword);//设置验证密码
System.Web.Mail.SmtpMail.SmtpServer = _SmtpServer;//邮件服务器地址(如smtp.163.com)
System.Web.Mail.SmtpMail.Send(msg);//发送
return true;
} catch { } finally {

}
}
return false;
}

/// <summary>
/// 发送EMAIL
/// </summary>
/// <param name="toEmail">Email</param>
/// <returns>是否成功</returns>
public bool CDOMessageSend(string toEmail) {
lock (lockHelper) {
CDO.Message bjMail = new CDO.Message();
try {
objMail.To = toEmail;
objMail.From = _FromEmail;
objMail.Subject = _Subject;
if (_Format.Equals(System.Web.Mail.MailFormat.Html)) objMail.HTMLBody = _Body; else objMail.TextBody = _Body;
//if (!_SmtpPort.Equals("25")) objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value= _SmtpPort; //设置端口
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value= _SmtpServer;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value= 1;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"].Value= 10;
objMail.Configuration.Fields.Update();
objMail.Send();
return true;
} catch {} finally{

}
System.Runtime.InteropServices.Marshal.ReleaseComObject(objMail);
bjMail = null;
}
return false;
}
/// <summary>
/// CDOMessageSend
/// </summary>
/// <param name="toEmail"></param>
/// <param name="sendusing"></param>
/// <returns></returns>
public bool CDOMessageSend(string toEmail,int sendusing) {
lock (lockHelper) {
CDO.Message bjMail = new CDO.Message();
try {
objMail.To = toEmail;
objMail.From = _FromEmail;
objMail.Subject = _Subject;
if (_Format.Equals(System.Web.Mail.MailFormat.Html)) objMail.HTMLBody = _Body; else objMail.TextBody = _Body;
if (!_SmtpPort.Equals("25")) objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value= _SmtpPort; //设置端口
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value= _SmtpServer;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value= sendusing;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value= _FromEmail;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpuserreplyemailaddress"].Value= _FromEmail;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value= _SmtpUserName;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value= _SmtpUserName;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value= _SmtpPassword;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value=1;

objMail.Configuration.Fields.Update();
objMail.Send();
return true;
} catch { } finally{

}
System.Runtime.InteropServices.Marshal.ReleaseComObject(objMail);
bjMail = null;
}
return false;
}
/// <summary>
/// SmtpClientSend
/// </summary>
/// <param name="toEmail"></param>
/// <returns></returns>
public bool SmtpClientSend(string toEmail) {
lock (lockHelper) {
System.Net.Mail.MailMessage message = new MailMessage(_FromEmail, toEmail, _Subject, _Body);
message.SubjectEncoding = _Encoding;
message.BodyEncoding = _Encoding;
message.IsBodyHtml = true;
message.Priority = MailPriority.High;

SmtpClient client = new SmtpClient(_SmtpServer);
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(_SmtpUserName, _SmtpPassword);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Port = Str.ToInt(_SmtpPort, 587);
client.EnableSsl = true;
try {
client.Send(message);
} catch {
return false;
}
return true;
}
}

#endregion
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值