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
{
///
/// Smtp邮件服务
///
public class SmtpMailSender
{
#region 属性
///
/// 邮件信息
///
private MailMessage mailMessage;
///
/// Smtp客户端
///
private SmtpClient smtpClient;
#endregion
#region 构造函数
///
///
///
/// 邮件服务器对象
/// 邮件内容对象
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 发件人邮箱
/////
///// 发件人邮箱
/////
//public String FromMail
//{
// get
// {
// return mailMessage.From.Address;
// }
// set
// {
// mailMessage.From = new MailAddress(value);
// }
//}
///
/// 发件人邮箱
///
private void AddFrommail(String FormMail)
{
mailMessage.From = new MailAddress(FormMail);
}
#endregion
#region 邮件标题
/////
///// 邮件标题
/////
//public String Subject
//{
// get
// {
// return mailMessage.Subject;
// }
// set
// {
// mailMessage.Subject = value;
// }
//}
///
/// 邮件标题
///
///
private void AddSubject(String Subject)
{
mailMessage.Subject = Subject;
}
#endregion
#region 邮件内容
/////
///// 邮件内容
/////
//public String Content
//{
// get
// {
// return mailMessage.Body;
// }
// set
// {
// mailMessage.Body = value;
// }
//}
///
/// 邮件内容
///
private void AddContent(String Content)
{
mailMessage.Body = Content;
}
#endregion
#region 添加收件人
///
/// 添加收件人
///
///
private void AddReceiver(String[] toEmail)
{
//添加收件人
foreach (String mail in toEmail)
{
mailMessage.To.Add(mail);
}
}
#endregion
#region 添加附件
///
/// 添加附件
///
///
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 判断邮件信息是否填写完整
///
/// 判断邮件信息是否填写完整
///
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 判断邮件服务器信息的完整性
///
/// 判断邮件服务器的信息完整性
///
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 标准邮件发送接口
///
/// 发送
///
public void Send()
{
smtpClient.Send(mailMessage);
}
#endregion
}
}