1.前言
a.在软件开发中,我们经常能够遇到给用户或者客户推送邮件,推送邮件也分为很多方式,比如:推送一句话,推送一个网页等等。那么在系统开发中我们一般在什么情况下会使用邮件发送呢?下面我简单总结了一下:总结不全,纯属于整理封装的类。
(1):用户注册(推送邮件,提示用户注册成功,牢记用户名密码)
(2):修改密码(推送邮件,提示用户密码修改成功)
(3):下订单(推送邮件,提示用户订单已下)
(4):物流跟踪(推送邮件,跟踪物流信息)
(5):广告推送(推送广告,提示用户近来公司近况或者新的商品,产品)
(6):日志监控系统推送(推送错误信息,提供给程序员使用)
................................................
b.上面我们说了邮件推送的使用,那么既然邮件推送这么频繁,就需要整理出来一个公用类来给大家使用,下面我就简单整理了一下公用类,如果大家需要,请git上下载或者复制即可,不能保证百分百实现你的功能,如果不能实现你的功能,请改进。
c.git下载地址:https://github.com/kencery/Common/tree/master/KenceryCommonMethod/Smtp%E9%82%AE%E4%BB%B6%E5%8F%91%E9%80%81
d.代码整理如下,请您查看.......
2.代码整理
a.C#代码以及如何调用等都在代码中已注释,如不会使用,请仔细查看
// 源文件头信息:
// <copyright file="Email.cs,EmailHelp.cs">
// Copyright(c)2014-2034 Kencery.All rights reserved.
// 个人博客:http://www.cnblogs.com/hanyinglong
// 创建人:韩迎龙(kencery)
// 创建时间:2015-8-18
// </copyright> using System;
using System.Net;
using System.Net.Mail;
using System.Text; namespace KenceryCommonMethod
{
/// <summary>
/// 功能:实现邮件发送,分装发送邮件的调用方法
/// </summary>
/// <auther>
/// <name>Kencery</name>
/// <date>2015-8-18</date>
/// </auther>
public class Email
{
#region --------------------字段-------------------- private string _serviceType = "SMTP";
private string _host; #endregion #region --------------------属性-------------------- /// <summary>
/// 发送者邮箱
/// </summary>
public string From { get; set; } /// <summary>
/// 接收者邮箱列表
/// </summary>
public string[] To { get; set; } /// <summary>
/// 抄送者邮箱列表
/// </summary>
public string[] Cc { get; set; } /// <summary>
/// 秘抄者邮箱列表
/// </summary>
public string[] Bcc { get; set; } /// <summary>
/// 邮件主题
/// </summary>
public string Subject { get; set; } /// <summary>
/// 邮件内容
/// </summary>
public string Body { get; set; } /// <summary>
/// 是否是HTML格式
/// </summary>
public bool IsBodyHtml { get; set; } /// <summary>
/// 附件列表
/// </summary>
public string[] Attachments { get; set; } /// <summary>
/// 邮箱服务类型(Pop3,SMTP,IMAP,MAIL等),默认为SMTP
/// </summary>
public string ServiceType
{
get { return _serviceType; }
set { _serviceType = value; }
} /// <summary>
/// 邮箱服务器,如果没有定义邮箱服务器,则根据serviceType和Sender组成邮箱服务器
/// </summary>
public string Host
{
get
{
return string.IsNullOrEmpty(_host)
? (this.ServiceType + "." +
Sender.Split("@".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[])
: _host;
}
set { _host = value; }
} /// <summary>
/// 邮箱账号(默认为发送者邮箱的账号)
/// </summary>
public string UserName { get; set; } /// <summary>
/// 邮箱密码(默认为发送者邮箱的密码),默认格式GB2312
/// </summary>
public string Password { get; set; } /// <summary>
/// 邮箱优先级
/// </summary>
public MailPriority MailPriority { get; set; } /// <summary>
/// 邮件正文编码格式
/// </summary>
public Encoding Encoding { get; set; } #endregion #region ------------------调用方法------------------ /// <summary>
/// 构造参数,发送邮件,使用方法备注:公开方法中调用
/// </summary>
public void Send()
{
var mailMessage = new MailMessage(); //读取To 接收者邮箱列表
if (this.To != null && this.To.Length > )
{
foreach (string to in this.To)
{
if (string.IsNullOrEmpty(to)) continue;
try
{
mailMessage.To.Add(new MailAddress(to.Trim()));
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
//读取Cc 抄送者邮件地址
if (this.Cc != null && this.Cc.Length > )
{
foreach (var cc in this.Cc)
{
if (string.IsNullOrEmpty(cc)) continue;
try
{
mailMessage.CC.Add(new MailAddress(cc.Trim()));
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
//读取Attachments 邮件附件
if (this.Attachments != null && this.Attachments.Length > )
{
foreach (var attachment in this.Attachments)
{
if (string.IsNullOrEmpty(attachment)) continue;
try
{
mailMessage.Attachments.Add(new Attachment(attachment));
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
//读取Bcc 秘抄人地址
if (this.Bcc != null && this.Bcc.Length > )
{
foreach (var bcc in this.Bcc)
{
if (string.IsNullOrEmpty(bcc)) continue;
try
{
mailMessage.Bcc.Add(new MailAddress(bcc.Trim()));
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
//读取From 发送人地址
try
{
mailMessage.From = new MailAddress(this.From);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
} //邮件标题
Encoding encoding = Encoding.GetEncoding("GB2312");
mailMessage.Subject = string.Format("?={0}?B?{1}?=", encoding.HeaderName,
Convert.ToBase64String(encoding.GetBytes(this.Subject), Base64FormattingOptions.None));
//邮件正文是否为HTML格式
mailMessage.IsBodyHtml = this.IsBodyHtml;
//邮件正文
mailMessage.Body = this.Body;
mailMessage.BodyEncoding = this.Encoding;
//邮件优先级
mailMessage.Priority = this.MailPriority; //发送邮件代码实现
var smtpClient = new SmtpClient
{
Host = this.Host,
Credentials = new NetworkCredential(this.UserName, this.Password)
};
//认证
try
{
smtpClient.Send(mailMessage);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
} #endregion
} /// <summary>
///邮件发送接口调用:bool isTrue=EmailInfo.SendEmail(参数,........); 参数解释参考方法
/// <auther>
/// <name>Kencery</name>
/// <date>2015-8-18</date>
/// </auther>
/// </summary>
public static class EmailInfo
{
/// <summary>
/// 邮件发送方法,传递参数(使用中如出现问题,请调试)
/// </summary>
/// <param name="from">发送者邮箱名称(从配置文件中读取,比如:934532778@qq.com)(必填项)</param>
/// <param name="to">接收者邮箱列表,可以传递多个,使用string[]表示(从配置文件中读取)(必填项)</param>
/// <param name="cc">抄送者邮箱列表,可以传递多个,使用string[]表示(从配置文件中读取)</param>
/// <param name="bcc">秘抄者邮箱列表,可以传递多个,使用string[]表示(从配置文件中读取)</param>
/// <param name="subject">邮件主题,构造(必填项)</param>
/// <param name="body">邮件内容,构造发送的邮件内容,可以发送网页(必填项)</param>
/// <param name="isBodyHtml">是否是HTML格式,true为是,false为否</param>
/// <param name="attachments">邮箱附件,可以传递多个,使用string[]表示(从配置文件中读取),可空</param>
/// <param name="host">邮箱服务器(从配置文件中读取,如:smtp@qq.com)(必填项)</param>
/// <param name="password">邮箱密码(从配置文件中读取,from邮箱的密码)(必填项)</param>
/// <returns>邮件发送成功,返回true,否则返回false</returns>
public static bool SendEmail(string from, string[] to, string[] cc, string[] bcc, string subject, string body,
bool isBodyHtml, string[] attachments, string host, string password)
{
//邮箱发送不满足,限制这些参数必须传递
if (from == "" || to.Length <= || subject == "" || body == "" || host == "" || password == "")
{
return false;
} var emil = new Email
{
From = @from,
To = to,
Cc = cc,
Bcc = bcc,
Subject = subject,
Body = body,
IsBodyHtml = isBodyHtml,
Attachments = attachments,
Host = host,
Password = password
};
try
{
emil.Send();
return true;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
}