236 lines
8.7 KiB
C#
236 lines
8.7 KiB
C#
|
using Model;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Net.Mail;
|
|||
|
namespace FineUIPro.Web.common
|
|||
|
{
|
|||
|
public class MailHelper
|
|||
|
{
|
|||
|
#region 从web.config中取得发送邮件参数
|
|||
|
|
|||
|
////SMTP服务器
|
|||
|
//public static string MailSmtpServer
|
|||
|
//{
|
|||
|
// get { return System.Configuration.ConfigurationManager.AppSettings["mailSmtpServer"]; }
|
|||
|
//}
|
|||
|
|
|||
|
////系统发件地址
|
|||
|
//public static string MailServerFrom
|
|||
|
//{
|
|||
|
// get
|
|||
|
// {
|
|||
|
|
|||
|
// if (getEmailPop(ID))
|
|||
|
// {
|
|||
|
|
|||
|
// }
|
|||
|
// else
|
|||
|
// {
|
|||
|
// return System.Configuration.ConfigurationManager.AppSettings["MailServerFrom"];
|
|||
|
// }
|
|||
|
// }
|
|||
|
//}
|
|||
|
|
|||
|
////系统发件密码
|
|||
|
//public static string MailServerpassword
|
|||
|
//{
|
|||
|
// get { return System.Configuration.ConfigurationManager.AppSettings["MailServerpassword"]; }
|
|||
|
//}
|
|||
|
|
|||
|
//系统发件人名称
|
|||
|
//public static string MailServeruserName
|
|||
|
//{
|
|||
|
// get { return System.Configuration.ConfigurationManager.AppSettings["MailServeruserName"]; }
|
|||
|
//}
|
|||
|
#endregion
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 私有构造方法,不允许创建实例
|
|||
|
/// </summary>
|
|||
|
private MailHelper()
|
|||
|
{
|
|||
|
// TODO: Add constructor logic here
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// SendNetMail(须配置SMTP服务器地址)(多个收件人、抄送人、附件其参数用";"隔开,最后一个不能有";")
|
|||
|
/// </summary>
|
|||
|
/// <param name="mailFrom">发件人</param>
|
|||
|
/// <param name="mailTo">收件人(多个收件人用";"隔开,最后一个不能有";")</param>
|
|||
|
/// <param name="mailSubject">主题</param>
|
|||
|
/// <param name="mailBody">内容</param>
|
|||
|
/// <param name="mailAttch">附件(多个附件用";"隔开,最后一个不能有";")</param>
|
|||
|
/// <param name="mailCode">密码(对加密过的)</param>
|
|||
|
/// <param name="mailPriority">优先级</param>
|
|||
|
/// <param name="mailCC">抄送(多个抄送人用";"隔开,最后一个不能有";")</param>
|
|||
|
/// <param name="resultMessage">输出信息</param>
|
|||
|
public static bool SendNetMail(Email_Pop ps, string mailFrom, string[] mailTo, string mailSubject, string mailBody, string mailAttch, string mailCode, string mailPriority, string[] mailCC, out string resultMessage)
|
|||
|
{
|
|||
|
//初始化输出参数
|
|||
|
resultMessage = "";
|
|||
|
|
|||
|
//发件人和收件人不为空
|
|||
|
if (string.IsNullOrEmpty(ps.EmailYx) || mailTo == null)
|
|||
|
{
|
|||
|
resultMessage = "Please Fill Email Addresser Or Addressee . ";
|
|||
|
return false;
|
|||
|
}
|
|||
|
if (!string.IsNullOrEmpty(ps.EmailYx))
|
|||
|
{
|
|||
|
mailFrom = ps.EmailYx;
|
|||
|
}
|
|||
|
|
|||
|
MailMessage email = new MailMessage();
|
|||
|
//MailAddress emailFrom = new MailAddress(mailFrom);
|
|||
|
|
|||
|
//发件人
|
|||
|
email.From = new MailAddress(mailFrom, ps.EmailYx);//发件人地址
|
|||
|
|
|||
|
//收件人
|
|||
|
if (mailTo != null && mailTo.Length > 0)
|
|||
|
{
|
|||
|
foreach (string send in mailTo)
|
|||
|
{
|
|||
|
if (!String.IsNullOrEmpty(send))
|
|||
|
{
|
|||
|
email.To.Add(send);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
//加抄送
|
|||
|
if (mailCC != null && mailCC.Length > 0)
|
|||
|
{
|
|||
|
|
|||
|
foreach (string cc in mailCC)
|
|||
|
{
|
|||
|
if (!String.IsNullOrEmpty(cc))
|
|||
|
{
|
|||
|
email.CC.Add(cc);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
//主题
|
|||
|
email.Subject = mailSubject;
|
|||
|
//内容
|
|||
|
email.Body = mailBody;
|
|||
|
//附件
|
|||
|
if (!string.IsNullOrEmpty(mailAttch))
|
|||
|
{
|
|||
|
string[] attachments = mailAttch.Split(';');
|
|||
|
foreach (string file in attachments)
|
|||
|
{
|
|||
|
System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(file, System.Net.Mime.MediaTypeNames.Application.Octet);
|
|||
|
//为附件添加发送时间
|
|||
|
System.Net.Mime.ContentDisposition disposition = attach.ContentDisposition;
|
|||
|
disposition.CreationDate = System.IO.File.GetCreationTime(file);
|
|||
|
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
|
|||
|
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
|
|||
|
//添加附件
|
|||
|
email.Attachments.Add(attach);
|
|||
|
}
|
|||
|
}
|
|||
|
//优先级
|
|||
|
email.Priority = (mailPriority == "High") ? System.Net.Mail.MailPriority.High : System.Net.Mail.MailPriority.Normal;
|
|||
|
//内容编码、格式
|
|||
|
email.BodyEncoding = System.Text.Encoding.UTF8;
|
|||
|
email.IsBodyHtml = true;
|
|||
|
//SMTP服务器
|
|||
|
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(ps.EmailFwq);
|
|||
|
|
|||
|
client.UseDefaultCredentials = true;
|
|||
|
//验证(Credentials 凭证)
|
|||
|
client.Credentials = new System.Net.NetworkCredential(mailFrom, ps.EmailPass);
|
|||
|
|
|||
|
//处理待发的电子邮件的方法 (Delivery 发送,传输)
|
|||
|
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
|
|||
|
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
//发送邮件
|
|||
|
client.Send(email);
|
|||
|
if (mailTo != null && mailTo.Length > 0)
|
|||
|
{
|
|||
|
foreach (string send in mailTo)
|
|||
|
{
|
|||
|
if (!String.IsNullOrEmpty(send))
|
|||
|
{
|
|||
|
Email_SendLog newSendEmail = new Email_SendLog();
|
|||
|
newSendEmail.EmailId = Guid.NewGuid().ToString();
|
|||
|
newSendEmail.EmailTile = mailSubject;
|
|||
|
newSendEmail.EmailContent = mailBody;
|
|||
|
newSendEmail.EmailURL = send;
|
|||
|
newSendEmail.EmailName = send;
|
|||
|
newSendEmail.EmailStatus = "发送成功";
|
|||
|
newSendEmail.CreateTime = DateTime.Now;
|
|||
|
newSendEmail.CreateName = "sys";
|
|||
|
BLL.Email_Send.Email_SendLogService.AddEmail_SendLog(newSendEmail);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
if (mailTo != null && mailTo.Length > 0)
|
|||
|
{
|
|||
|
List<Email_SendLog> ss = new List<Email_SendLog>();
|
|||
|
foreach (string send in mailTo)
|
|||
|
{
|
|||
|
if (!String.IsNullOrEmpty(send))
|
|||
|
{
|
|||
|
Email_SendLog newSendEmail = new Email_SendLog();
|
|||
|
newSendEmail.EmailId = Guid.NewGuid().ToString();
|
|||
|
newSendEmail.EmailTile = mailSubject;
|
|||
|
newSendEmail.EmailContent = mailBody;
|
|||
|
newSendEmail.EmailURL = send;
|
|||
|
newSendEmail.EmailName = send;
|
|||
|
newSendEmail.EmailStatus = "发送失败";
|
|||
|
newSendEmail.CreateTime = DateTime.Now;
|
|||
|
newSendEmail.CreateName = "sys";
|
|||
|
BLL.Email_Send.Email_SendLogService.AddEmail_SendLog(newSendEmail);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
resultMessage = ex.Message;
|
|||
|
return false;
|
|||
|
}
|
|||
|
finally
|
|||
|
{
|
|||
|
//及时释放占用的资源
|
|||
|
email.Attachments.Clear();
|
|||
|
email.Attachments.Dispose();
|
|||
|
}
|
|||
|
}
|
|||
|
public static bool getDateNow()
|
|||
|
{
|
|||
|
bool f = false;
|
|||
|
|
|||
|
string dataTime = DateTime.Now.ToString("dd");
|
|||
|
Sys_Set pops = new Sys_Set();
|
|||
|
pops = BLL.Sys_SetService.GetSetById(3);
|
|||
|
|
|||
|
|
|||
|
if (pops != null)
|
|||
|
{
|
|||
|
if (pops.SetValue.Equals(dataTime))
|
|||
|
{
|
|||
|
f = true;
|
|||
|
}
|
|||
|
}
|
|||
|
return f;
|
|||
|
}
|
|||
|
|
|||
|
public static Email_Pop getEmailPop(string EmailID)
|
|||
|
{
|
|||
|
Email_Pop pp = new Email_Pop();
|
|||
|
pp = BLL.Email_Send.Email_PopService.GetEmail_Pop(EmailID);
|
|||
|
|
|||
|
return pp;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|