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.
340 lines
11 KiB
340 lines
11 KiB
using System;
|
|
using System.Net.Mail;
|
|
using System.Net;
|
|
using System.Net.Security;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using System.Text;
|
|
|
|
|
|
namespace QM.Exchange.Adapters.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.Port = 587;
|
|
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 = Encoding.Default;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 发件人邮箱
|
|
|
|
/// <summary>
|
|
/// 发件人邮箱
|
|
/// </summary>
|
|
private void AddFrommail(String FormMail)
|
|
{
|
|
mailMessage.From = new MailAddress(FormMail);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 邮件标题
|
|
|
|
/// <summary>
|
|
/// 邮件标题
|
|
/// </summary>
|
|
/// <param name="Subject"></param>
|
|
private void AddSubject(String Subject)
|
|
{
|
|
mailMessage.Subject = Subject;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 邮件内容
|
|
|
|
/// <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
|
|
|
|
|
|
#region 大众WebRef发送接口
|
|
|
|
//发件人设置为 "subpadmin@faw-vw.com" // DOMAIN设置为 "faw-vw" 其它与上边一样 不需要设置SmtpServer
|
|
//SystemInfo sysinfo, IList<string> MailTo, string Subject, string Body
|
|
|
|
/// <summary>
|
|
/// 大众WebRef调用接口
|
|
/// </summary>
|
|
/// <param name="ServerInfo"></param>
|
|
/// <param name="MailInfo"></param>
|
|
//public static void SendMailByWebRef(SmtpServerInfo ServerInfo, MailInfo MailInfo)
|
|
//{
|
|
|
|
// ServicePointManager.ServerCertificateValidationCallback =
|
|
// delegate(Object obj, X509Certificate certificate,
|
|
// X509Chain chain, SslPolicyErrors errors)
|
|
// {
|
|
// // Replace this line with code to validate server
|
|
// // certificate.
|
|
// return true;
|
|
// };
|
|
|
|
// // Create the CreateItem request.
|
|
// //创建 CreateItem 的请求
|
|
// CreateItemType createEmailRequest = new CreateItemType();
|
|
|
|
// // Specifiy how the e-mail will be handled.
|
|
// //确定 Mail的处理方式
|
|
// createEmailRequest.MessageDisposition = MessageDispositionType.SendAndSaveCopy;
|
|
// createEmailRequest.MessageDispositionSpecified = true;
|
|
|
|
// // Specify the location of sent items.
|
|
// //确定发送信息存放的位置
|
|
// createEmailRequest.SavedItemFolderId = new TargetFolderIdType();
|
|
// DistinguishedFolderIdType sentitems = new DistinguishedFolderIdType();
|
|
// sentitems.Id = DistinguishedFolderIdNameType.sentitems;
|
|
// createEmailRequest.SavedItemFolderId.Item = sentitems;
|
|
|
|
// // Create the array of items.
|
|
// //创建存放数组
|
|
// createEmailRequest.Items = new NonEmptyArrayOfAllItemsType();
|
|
|
|
// // Create a single e-mail message.
|
|
// //创建 e-mail 信息
|
|
// MessageType message = new MessageType();
|
|
// message.Subject = MailInfo.Subject;
|
|
|
|
// message.Body = new BodyType();
|
|
// message.Body.BodyType1 = BodyTypeType.HTML;
|
|
// message.Body.Value = MailInfo.Content;
|
|
|
|
// //发送者信息
|
|
// message.Sender = new SingleRecipientType();
|
|
// message.Sender.Item = new EmailAddressType();
|
|
// //message.Sender.Item.EmailAddress = "subpadmin@faw-vw.com";
|
|
// message.Sender.Item.EmailAddress = ServerInfo.SenderUserEmail;
|
|
|
|
// message.ToRecipients = new EmailAddressType[MailInfo.ToMail.Length];
|
|
// for (int i = 0; i < MailInfo.ToMail.Length; i++)
|
|
// {
|
|
// message.ToRecipients[i] = new EmailAddressType();
|
|
// message.ToRecipients[i].EmailAddress = MailInfo.ToMail[i];
|
|
// }
|
|
|
|
// message.Sensitivity = SensitivityChoicesType.Normal;
|
|
|
|
// //大众服务器配置 ----------------- Add the message to the array of items to be created.
|
|
// createEmailRequest.Items.Items = new ItemType[1];
|
|
// createEmailRequest.Items.Items[0] = message;
|
|
|
|
// try
|
|
// {
|
|
// // Identify the service binding and the user.
|
|
// ExchangeServiceBinding esb = new ExchangeServiceBinding();
|
|
// //大众服务器配置 ----------------- esb.Credentials = new NetworkCredential(ServerInfo.SenderUser, ServerInfo.SenderPassword, "faw-vw");
|
|
// esb.Credentials = new NetworkCredential(ServerInfo.SenderUser, ServerInfo.SenderPassword, ServerInfo.DoMain);
|
|
// esb.Url = @"https://mail.faw-vw.com/EWS/Exchange.asmx";
|
|
|
|
// // Send a CreateItem request and get the CreateItem
|
|
// // response.
|
|
// CreateItemResponseType createItemResponse = esb.CreateItem(createEmailRequest);
|
|
// ArrayOfResponseMessagesType responses = createItemResponse.ResponseMessages;
|
|
// ResponseMessageType[] responseMessages = responses.Items;
|
|
|
|
// // Access the response messages.
|
|
// foreach (ResponseMessageType respMsg in responseMessages)
|
|
// {
|
|
// if (respMsg.ResponseClass == ResponseClassType.Error)
|
|
// {
|
|
// throw new Exception("Error: " + respMsg.MessageText);
|
|
// }
|
|
// else if (respMsg.ResponseClass == ResponseClassType.Warning)
|
|
// {
|
|
// throw new Exception("Warning: " + respMsg.MessageText);
|
|
// }
|
|
|
|
// // Check to determine whether the response message is the correct type.
|
|
// if (respMsg is ItemInfoResponseMessageType)
|
|
// {
|
|
// ItemInfoResponseMessageType createItemResp = (respMsg as ItemInfoResponseMessageType);
|
|
// ArrayOfRealItemsType aorit = createItemResp.Items;
|
|
|
|
// //foreach (ItemType item in aorit.Items)
|
|
// //{
|
|
// // if (item is MessageType)
|
|
// // {
|
|
// // MessageType myMessage = (item as MessageType);
|
|
// // //MessageBox.Show("Created item: " + myMessage.ItemId.Id);
|
|
|
|
// // }
|
|
// // // TODO: Add logic to check and cast for all other types.
|
|
// //}
|
|
// }
|
|
// }
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// //QMFrameWork.Log.LogManager.LogHelper.Error(new QMFrameWork.Log.LogInfo()
|
|
// //{
|
|
// // Info = ex.Message,
|
|
// // ErrorInfo = ex,
|
|
// //});
|
|
|
|
// }
|
|
// finally
|
|
// {
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
#endregion
|
|
}
|
|
}
|
|
|