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.
 
 
 
 
 

281 lines
11 KiB

using System;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
namespace NFine.Code
{
public class MailHelper
{
/// <summary>
/// 邮件服务器地址
/// </summary>
public string MailServer { get; set; }
/// <summary>
/// 用户名
/// </summary>
public string MailUserName { get; set; }
/// <summary>
/// 密码
/// </summary>
public string MailPassword { get; set; }
/// <summary>
/// 名称
/// </summary>
public string MailName { get; set; }
private MailMessage message = new MailMessage();//申明邮件类
/// <summary>
/// 同步发送邮件
/// </summary>
/// <param name="to">收件人邮箱地址</param>
/// <param name="subject">主题</param>
/// <param name="body">内容</param>
/// <param name="encoding">编码</param>
/// <param name="isBodyHtml">是否Html</param>
/// <param name="enableSsl">是否SSL加密连接</param>
/// <returns>是否成功</returns>
public bool Send(string to, string subject, string body, string encoding = "UTF-8", bool isBodyHtml = true, bool enableSsl = false)
{
try
{
MailMessage message = new MailMessage();
// 接收人邮箱地址
message.To.Add(new MailAddress(to));
message.From = new MailAddress(MailUserName, MailName);
message.BodyEncoding = Encoding.GetEncoding(encoding);
message.Body = body;
//GB2312
message.SubjectEncoding = Encoding.GetEncoding(encoding);
message.Subject = subject;
message.IsBodyHtml = isBodyHtml;
SmtpClient smtpclient = new SmtpClient(MailServer, 25);
smtpclient.Credentials = new System.Net.NetworkCredential(MailUserName, MailPassword);
//SSL连接
smtpclient.EnableSsl = enableSsl;
smtpclient.Send(message);
return true;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 异步发送邮件 独立线程
/// </summary>
/// <param name="to">邮件接收人</param>
/// <param name="title">邮件标题</param>
/// <param name="body">邮件内容</param>
/// <param name="port">端口号</param>
/// <returns></returns>
public void SendByThread(string to, string title, string body, int port = 25)
{
new Thread(new ThreadStart(delegate()
{
try
{
SmtpClient smtp = new SmtpClient();
//邮箱的smtp地址
smtp.Host = MailServer;
//端口号
smtp.Port = port;
//构建发件人的身份凭据类
smtp.Credentials = new NetworkCredential(MailUserName, MailPassword);
//构建消息类
MailMessage objMailMessage = new MailMessage();
//设置优先级
objMailMessage.Priority = MailPriority.High;
//消息发送人
objMailMessage.From = new MailAddress(MailUserName, MailName, System.Text.Encoding.UTF8);
//收件人
objMailMessage.To.Add(to);
//标题
objMailMessage.Subject = title.Trim();
//标题字符编码
objMailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
//正文
objMailMessage.Body = body.Trim();
objMailMessage.IsBodyHtml = true;
//内容字符编码
objMailMessage.BodyEncoding = System.Text.Encoding.UTF8;
//发送
smtp.Send(objMailMessage);
}
catch (Exception)
{
throw;
}
})).Start();
}
public static void SendEmail(string ConnectionString, string SendHost, int SendPort, string SendDisplayName, string SendAddress, string SendPassword, string TOAddress, string CCAddress, string Subject, bool IsBodyHtml, string Body, string POCode)
{
try
{
SmtpClient smtpClient = new SmtpClient
{
EnableSsl = true,
UseDefaultCredentials = false,
Host = SendHost,
Port = SendPort,
Credentials = new NetworkCredential(SendAddress, SendPassword)
};
MailMessage mailMessage = new MailMessage
{
Subject = Subject,
SubjectEncoding = Encoding.GetEncoding("utf-8"),
BodyEncoding = Encoding.GetEncoding("utf-8"),
From = new MailAddress(SendAddress, SendDisplayName),
IsBodyHtml = IsBodyHtml,
Body = Body
};
string filePath = System.Web.HttpContext.Current.Server.MapPath("~\\File\\POReleaseFile\\" + POCode + ".pdf");
AddAttachments(filePath);//添加附件
string[] array = TOAddress.Split(new char[]
{
','
});
string[] array2 = array;
for (int i = 0; i < array2.Length; i++)
{
string text = array2[i];
if (!string.IsNullOrEmpty(text))
{
mailMessage.To.Add(text);
}
}
string[] array3 = CCAddress.Split(new char[]
{
','
});
array2 = array3;
for (int i = 0; i < array2.Length; i++)
{
string text2 = array2[i];
if (!string.IsNullOrEmpty(text2))
{
mailMessage.CC.Add(text2);
}
}
ServicePointManager.ServerCertificateValidationCallback = ((object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) => true);
smtpClient.Send(mailMessage);
InsertData(ConnectionString, SendHost, SendPort, SendDisplayName, SendAddress, SendPassword, TOAddress, CCAddress, Subject, IsBodyHtml, Body, "1", null);
}
catch (Exception ex)
{
InsertData(ConnectionString, SendHost, SendPort, SendDisplayName, SendAddress, SendPassword, TOAddress, CCAddress, Subject, IsBodyHtml, Body, "2", ex.Message);
throw;
}
}
public static void AddAttachments(string attachmentsPath)//这里的参数代表附件的绝对路径
{
try
{
string[] path = attachmentsPath.Split(';'); //以什么符号分隔可以自定义
Attachment data;
ContentDisposition disposition;
for (int i = 0; i < path.Length; i++)
{
data = new Attachment(path[i], MediaTypeNames.Application.Octet);
disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(path[i]);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]);
disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private static void InsertData(string ConnectionString, string SendHost, int SendPort, string SendDisplayName, string SendAddress, string SendPassword, string TOAddress, string CCAddress, string Subject, bool IsBodyHtml, string Body, string Status, string Message)
{
try
{
try
{
string text = "INSERT INTO ICSSendMail(ID, SendHost, SendPort, SendDisplayName, SendAddress, \r\n SendPassword, TOAddress, CCAddress, Subject, IsBodyHtml, \r\n Body, Retry, Status, Message, MUSER, MUSERName, CreatTIME, ModifyTIME)\r\n VALUES(NEWID(),'{0}','{1}','{2}','{3}',\r\n '{4}','{5}','{6}','{7}','{8}',\r\n '{9}','0','{10}','{11}','','',GETDATE(),GETDATE())";
text = string.Format(text, new object[]
{
SendHost,
SendPort,
SendDisplayName,
SendAddress,
SendPassword,
TOAddress,
CCAddress,
Subject,
IsBodyHtml,
Body.Replace("'", "''"),
Status,
Message
});
ExecuteDate(ConnectionString, text);
}
catch (Exception ex)
{
throw ex;
}
}
catch (Exception ex)
{
throw ex;
}
}
private static void ExecuteDate(string conStr, string sql)
{
try
{
using (SqlConnection sqlConnection = new SqlConnection(conStr))
{
sqlConnection.Open();
try
{
using (SqlTransaction sqlTransaction = sqlConnection.BeginTransaction())
{
using (SqlCommand sqlCommand = new SqlCommand())
{
sqlCommand.Connection = sqlConnection;
sqlCommand.Transaction = sqlTransaction;
sqlCommand.CommandText = sql;
try
{
int num = sqlCommand.ExecuteNonQuery();
sqlTransaction.Commit();
}
catch (Exception ex)
{
sqlTransaction.Rollback();
throw ex;
}
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (sqlConnection.State == ConnectionState.Open)
{
sqlConnection.Close();
}
sqlConnection.Dispose();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}