You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
255 lines
6.1 KiB
255 lines
6.1 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Net.Mail;
|
|
using System.Net;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using System.Net.Security;
|
|
|
|
|
|
namespace QMFrameWork.Mail
|
|
{
|
|
/// <summary>
|
|
/// Smtp邮件服务
|
|
/// </summary>
|
|
public class SmtpMailSender
|
|
{
|
|
#region 属性
|
|
|
|
/// <summary>
|
|
/// 邮件信息
|
|
/// </summary>
|
|
private MailMessage mailMessage;
|
|
|
|
/// <summary>
|
|
/// Smtp客户端
|
|
/// </summary>
|
|
private SmtpClient smtpClient;
|
|
|
|
#endregion
|
|
|
|
#region 构造函数
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="ServerInfo">邮件服务器对象</param>
|
|
/// <param name="MailInfo">邮件内容对象</param>
|
|
public SmtpMailSender(SmtpServerInfo ServerInfo, MailInfo MailInfo)
|
|
{
|
|
//判断邮件服务器信息的完整性
|
|
CheckSmtpServerInfo(ServerInfo);
|
|
|
|
mailMessage = new MailMessage();
|
|
smtpClient = new SmtpClient();
|
|
smtpClient.Host = ServerInfo.SmtpServer;
|
|
smtpClient.Port = 25;
|
|
smtpClient.UseDefaultCredentials = true;
|
|
smtpClient.Credentials = new NetworkCredential(ServerInfo.SenderUser, ServerInfo.SenderPassword, ServerInfo.DoMain);
|
|
|
|
//判断邮件信息是否填写完整
|
|
CheckMailInfo(MailInfo);
|
|
//发件人
|
|
AddFrommail(ServerInfo.SenderUserEmail);
|
|
//邮件标题
|
|
AddSubject(MailInfo.Subject);
|
|
//邮件内容
|
|
AddContent(MailInfo.Content);
|
|
//收件人
|
|
AddReceiver(MailInfo.ToMail);
|
|
//附件
|
|
AddAttachment(MailInfo.Additional);
|
|
|
|
mailMessage.IsBodyHtml = true;
|
|
|
|
mailMessage.BodyEncoding = System.Text.Encoding.Default;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 发件人邮箱
|
|
|
|
///// <summary>
|
|
///// 发件人邮箱
|
|
///// </summary>
|
|
//public String FromMail
|
|
//{
|
|
// get
|
|
// {
|
|
// return mailMessage.From.Address;
|
|
// }
|
|
|
|
// set
|
|
// {
|
|
// mailMessage.From = new MailAddress(value);
|
|
// }
|
|
//}
|
|
|
|
/// <summary>
|
|
/// 发件人邮箱
|
|
/// </summary>
|
|
private void AddFrommail(String FormMail)
|
|
{
|
|
mailMessage.From = new MailAddress(FormMail);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 邮件标题
|
|
|
|
///// <summary>
|
|
///// 邮件标题
|
|
///// </summary>
|
|
//public String Subject
|
|
//{
|
|
// get
|
|
// {
|
|
// return mailMessage.Subject;
|
|
// }
|
|
|
|
// set
|
|
// {
|
|
// mailMessage.Subject = value;
|
|
// }
|
|
//}
|
|
|
|
/// <summary>
|
|
/// 邮件标题
|
|
/// </summary>
|
|
/// <param name="Subject"></param>
|
|
private void AddSubject(String Subject)
|
|
{
|
|
mailMessage.Subject = Subject;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 邮件内容
|
|
|
|
///// <summary>
|
|
///// 邮件内容
|
|
///// </summary>
|
|
//public String Content
|
|
//{
|
|
// get
|
|
// {
|
|
// return mailMessage.Body;
|
|
// }
|
|
// set
|
|
// {
|
|
// mailMessage.Body = value;
|
|
// }
|
|
//}
|
|
|
|
/// <summary>
|
|
/// 邮件内容
|
|
/// </summary>
|
|
private void AddContent(String Content)
|
|
{
|
|
mailMessage.Body = Content;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 添加收件人
|
|
|
|
/// <summary>
|
|
/// 添加收件人
|
|
/// </summary>
|
|
/// <param name="toEmail"></param>
|
|
private void AddReceiver(String[] toEmail)
|
|
{
|
|
//添加收件人
|
|
foreach (String mail in toEmail)
|
|
{
|
|
mailMessage.To.Add(mail);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 添加附件
|
|
|
|
/// <summary>
|
|
/// 添加附件
|
|
/// </summary>
|
|
/// <param name="filename"></param>
|
|
private void AddAttachment(String[] filename)
|
|
{
|
|
//为空时返回
|
|
if (filename == null || filename.Length <= 0)
|
|
{
|
|
return;
|
|
}
|
|
//循环添加附件
|
|
foreach (String fname in filename)
|
|
{
|
|
mailMessage.Attachments.Add(new Attachment(fname));
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 判断邮件信息是否填写完整
|
|
|
|
/// <summary>
|
|
/// 判断邮件信息是否填写完整
|
|
/// </summary>
|
|
private void CheckMailInfo(MailInfo MailInfo)
|
|
{
|
|
//收件人
|
|
if (MailInfo.ToMail == null || MailInfo.ToMail.Length <= 0)
|
|
{
|
|
throw new Exception("收件人为空.");
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(MailInfo.Subject))
|
|
{
|
|
throw new Exception("标题为空.");
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(MailInfo.Content))
|
|
{
|
|
throw new Exception("内容为空.");
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 判断邮件服务器信息的完整性
|
|
|
|
/// <summary>
|
|
/// 判断邮件服务器的信息完整性
|
|
/// </summary>
|
|
public void CheckSmtpServerInfo(SmtpServerInfo ServerInfo)
|
|
{
|
|
//邮件服务器域名
|
|
if (ServerInfo.SmtpServer == null || ServerInfo.DoMain == null)
|
|
{
|
|
throw new Exception("请设置邮件服务器域名.");
|
|
}
|
|
|
|
//发件人
|
|
if (ServerInfo.SenderUser == null || ServerInfo.SenderPassword == null || ServerInfo.SenderUserEmail == null)
|
|
{
|
|
throw new Exception("请设置发件人信息.");
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 标准邮件发送接口
|
|
|
|
/// <summary>
|
|
/// 发送
|
|
/// </summary>
|
|
public void Send()
|
|
{
|
|
smtpClient.Send(mailMessage);
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|
|
|