邮件发送类
采用配置方式进行实现,已备后查
<!--mail sender config-->
<system.net>
<mailSettings>
<smtp from="XX@XX.com">
<network host="smtp.XXX.com" userName="XX@XX.com" password="XX" defaultCredentials="true" port="25"/>
</smtp>
</mailSettings>
</system.net>
public class Mail {
const String BccFrom = "XX@XX.com";
/// <summary>
/// 邮件发送
/// </summary>
/// <param name="mailEntity"></param>
/// <returns></returns>
public Boolean Send ( MailEntity mailEntity ) {
Boolean isSucSended = true;
try {
using ( MailMessage mailMessager = new MailMessage () ) {
//是否按Html方式表现邮件正文
mailMessager.IsBodyHtml = true;
//邮件等级
mailMessager.Priority = MailPriority.High;
//邮件主题
mailMessager.Subject = mailEntity.Subject;
//邮件内容
mailMessager.Body = mailEntity.Body;
//邮件编码
mailMessager.BodyEncoding = System.Text.Encoding.Default;
//邮件收件人(列表)
if ( mailEntity.To.Count > 0 ) {
foreach ( String ccItem in mailEntity.To ) {
mailMessager.To.Add ( ccItem );
}
}
//抄送人(列表)
if ( mailEntity.CC.Count > 0 ) {
foreach ( String ccItem in mailEntity.CC ) {
mailMessager.CC.Add ( ccItem );
}
}
//密送发件人(列表)
if ( mailEntity.To.Count > 0 ) {
foreach ( String ccItem in mailEntity.Bcc ) {
mailMessager.Bcc.Add ( ccItem );
}
}
//默认密送发件人
if ( mailMessager.From != null ) {
mailMessager.Bcc.Add ( mailMessager.From );
}
else {
mailMessager.Bcc.Add ( BccFrom );
}
//邮件传送通知选项
mailMessager.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
MailAccountEntity mailAccount = GetConfig ();
{
SmtpClient smtpClient = new SmtpClient ( mailAccount.Host , mailAccount.Port );
{
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new NetworkCredential ( mailAccount.UserName , mailAccount.Password );
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Send ( mailMessager );
}
}
}
}
catch ( SmtpFailedRecipientException ex ) {
isSucSended = false;
}
catch ( System.Exception ex ) {
isSucSended = false;
}
return isSucSended;
}
/// <summary>
/// 获取MailAccount(SmtpAddress,UserName,Password,Port)
/// </summary>
/// <returns></returns>
private MailAccountEntity GetConfig ( ) {
MailAccountEntity mailAccount = new MailAccountEntity ();
{
//读取Config的中MailSettings配置段
Configuration config = ConfigurationManager.OpenExeConfiguration ( ConfigurationUserLevel.None );
MailSettingsSectionGroup mailSettings = NetSectionGroup.GetSectionGroup ( config ).MailSettings;
SmtpSection stmpSection = mailSettings.Smtp;
{
mailAccount.Host = stmpSection.Network.Host;
mailAccount.UserName = stmpSection.Network.UserName;
mailAccount.Password = stmpSection.Network.Password;
mailAccount.Port = stmpSection.Network.Port;
}
}
return mailAccount;
}
}
const String BccFrom = "XX@XX.com";
/// <summary>
/// 邮件发送
/// </summary>
/// <param name="mailEntity"></param>
/// <returns></returns>
public Boolean Send ( MailEntity mailEntity ) {
Boolean isSucSended = true;
try {
using ( MailMessage mailMessager = new MailMessage () ) {
//是否按Html方式表现邮件正文
mailMessager.IsBodyHtml = true;
//邮件等级
mailMessager.Priority = MailPriority.High;
//邮件主题
mailMessager.Subject = mailEntity.Subject;
//邮件内容
mailMessager.Body = mailEntity.Body;
//邮件编码
mailMessager.BodyEncoding = System.Text.Encoding.Default;
//邮件收件人(列表)
if ( mailEntity.To.Count > 0 ) {
foreach ( String ccItem in mailEntity.To ) {
mailMessager.To.Add ( ccItem );
}
}
//抄送人(列表)
if ( mailEntity.CC.Count > 0 ) {
foreach ( String ccItem in mailEntity.CC ) {
mailMessager.CC.Add ( ccItem );
}
}
//密送发件人(列表)
if ( mailEntity.To.Count > 0 ) {
foreach ( String ccItem in mailEntity.Bcc ) {
mailMessager.Bcc.Add ( ccItem );
}
}
//默认密送发件人
if ( mailMessager.From != null ) {
mailMessager.Bcc.Add ( mailMessager.From );
}
else {
mailMessager.Bcc.Add ( BccFrom );
}
//邮件传送通知选项
mailMessager.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
MailAccountEntity mailAccount = GetConfig ();
{
SmtpClient smtpClient = new SmtpClient ( mailAccount.Host , mailAccount.Port );
{
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new NetworkCredential ( mailAccount.UserName , mailAccount.Password );
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Send ( mailMessager );
}
}
}
}
catch ( SmtpFailedRecipientException ex ) {
isSucSended = false;
}
catch ( System.Exception ex ) {
isSucSended = false;
}
return isSucSended;
}
/// <summary>
/// 获取MailAccount(SmtpAddress,UserName,Password,Port)
/// </summary>
/// <returns></returns>
private MailAccountEntity GetConfig ( ) {
MailAccountEntity mailAccount = new MailAccountEntity ();
{
//读取Config的中MailSettings配置段
Configuration config = ConfigurationManager.OpenExeConfiguration ( ConfigurationUserLevel.None );
MailSettingsSectionGroup mailSettings = NetSectionGroup.GetSectionGroup ( config ).MailSettings;
SmtpSection stmpSection = mailSettings.Smtp;
{
mailAccount.Host = stmpSection.Network.Host;
mailAccount.UserName = stmpSection.Network.UserName;
mailAccount.Password = stmpSection.Network.Password;
mailAccount.Port = stmpSection.Network.Port;
}
}
return mailAccount;
}
}
[Serializable]
public class MailEntity {
private String subject;
/// <summary>
/// 邮件主题
/// </summary>
public String Subject {
get {
return subject;
}
set {
subject = value;
}
}
private String body;
/// <summary>
/// 邮件内容
/// </summary>
public String Body {
get {
return body;
}
set {
body = value;
}
}
private List<String> to = new List<String> ();
/// <summary>
/// 收件人
/// </summary>
public List<String> To {
get {
return to;
}
set {
to = value;
}
}
private List<String> cc = new List<String> ();
/// <summary>
/// 抄送 (CC) 收件人的地址集合。
/// </summary>
public List<String> CC {
get {
return cc;
}
set {
cc = value;
}
}
private List<String> bcc = new List<String> ();
/// <summary>
/// 密件抄送 (BCC) 收件人的地址集合
/// </summary>
public List<String> Bcc {
get {
return bcc;
}
set {
bcc = value;
}
}
}
public class MailEntity {
private String subject;
/// <summary>
/// 邮件主题
/// </summary>
public String Subject {
get {
return subject;
}
set {
subject = value;
}
}
private String body;
/// <summary>
/// 邮件内容
/// </summary>
public String Body {
get {
return body;
}
set {
body = value;
}
}
private List<String> to = new List<String> ();
/// <summary>
/// 收件人
/// </summary>
public List<String> To {
get {
return to;
}
set {
to = value;
}
}
private List<String> cc = new List<String> ();
/// <summary>
/// 抄送 (CC) 收件人的地址集合。
/// </summary>
public List<String> CC {
get {
return cc;
}
set {
cc = value;
}
}
private List<String> bcc = new List<String> ();
/// <summary>
/// 密件抄送 (BCC) 收件人的地址集合
/// </summary>
public List<String> Bcc {
get {
return bcc;
}
set {
bcc = value;
}
}
}
[Serializable]
public class MailAccountEntity {
private String userName;
/// <summary>
/// 邮箱用户名
/// </summary>
public String UserName {
get {
return userName;
}
set {
userName = value;
}
}
private String password;
/// <summary>
/// 邮箱密码
/// </summary>
public String Password {
get {
return password;
}
set {
password = value;
}
}
private Int32 port;
/// <summary>
/// 邮箱port
/// </summary>
public Int32 Port {
get {
return port;
}
set {
port = value;
}
}
private String host;
/// <summary>
/// 邮箱host,Smtp
/// </summary>
public String Host {
get {
return host;
}
set {
host = value;
}
}
}
public class MailAccountEntity {
private String userName;
/// <summary>
/// 邮箱用户名
/// </summary>
public String UserName {
get {
return userName;
}
set {
userName = value;
}
}
private String password;
/// <summary>
/// 邮箱密码
/// </summary>
public String Password {
get {
return password;
}
set {
password = value;
}
}
private Int32 port;
/// <summary>
/// 邮箱port
/// </summary>
public Int32 Port {
get {
return port;
}
set {
port = value;
}
}
private String host;
/// <summary>
/// 邮箱host,Smtp
/// </summary>
public String Host {
get {
return host;
}
set {
host = value;
}
}
}