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.
103 lines
3.6 KiB
103 lines
3.6 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net;
|
|
using System.Net.Mail;
|
|
using System.Linq;
|
|
using System.Web;
|
|
|
|
namespace FangYar.WebUI.WebCommon
|
|
{
|
|
/// <summary>
|
|
/// 发送邮件帮助类
|
|
/// </summary>
|
|
public class EmailHelp
|
|
{
|
|
|
|
|
|
public static void SendEmail(EmailMo mo)
|
|
{
|
|
|
|
//邮件发送账号
|
|
var EmailLogin = System.Configuration.ConfigurationManager.AppSettings["EmailLogin"] + "";
|
|
//邮件发送授权码
|
|
var EmailKey = System.Configuration.ConfigurationManager.AppSettings["EmailKey"] + "";
|
|
//邮件发送SMTP
|
|
var EmailHost = System.Configuration.ConfigurationManager.AppSettings["EmailHost"] + "";
|
|
|
|
string returnstr = "{\"code\":0,\"msg\":\"error\",\"data\":[]}";
|
|
try
|
|
{
|
|
MailMessage mailMessage = new MailMessage();//定义邮件
|
|
SmtpClient smtpClient = new SmtpClient();//定义发件客户端
|
|
mailMessage.From = new MailAddress(mo.FormName);//邮件发送人地址
|
|
mailMessage.Subject = mo.TitleName;//邮件主题
|
|
mailMessage.Body = mo.BodyData;//邮件内容
|
|
mailMessage.IsBodyHtml = true;//HTML格式
|
|
mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//编码UTF8
|
|
mailMessage.Priority = MailPriority.Normal;//邮件发送的优先性为正常
|
|
for (int i = 0; i < mo.ReceiveList.Count; i++)
|
|
{
|
|
mailMessage.To.Add(mo.ReceiveList[i]);
|
|
}
|
|
smtpClient.UseDefaultCredentials = false;//使用默认凭据
|
|
smtpClient.EnableSsl = false;//启动SSL,即安全发送
|
|
smtpClient.Credentials = new NetworkCredential(EmailLogin, EmailKey);
|
|
smtpClient.Host = EmailHost;//发送连接服务器主机IP
|
|
smtpClient.Port = 25;//端口号 25,465
|
|
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//传递电子邮件消息,通过网络发送电子邮件到SMTP
|
|
smtpClient.Send(mailMessage);//确认发送按钮
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
returnstr = "{\"code\":-1,\"msg\":\"error\",\"data\":\"" + ex + "\"}";
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public class EmailMo
|
|
{
|
|
/// <summary>
|
|
/// 发件人
|
|
/// </summary>
|
|
public string FormName { get; set; }
|
|
/// <summary>
|
|
/// 主题
|
|
/// </summary>
|
|
public string TitleName { get; set; }
|
|
/// <summary>
|
|
/// 内容数据
|
|
/// </summary>
|
|
public string BodyData { get; set; }
|
|
/// <summary>
|
|
/// 收件人信息集合
|
|
/// </summary>
|
|
public string ReceiveInfo { get; set; }
|
|
/// <summary>
|
|
/// 收件人拆分成
|
|
/// </summary>
|
|
public List<string> ReceiveList
|
|
{
|
|
get
|
|
{
|
|
try
|
|
{
|
|
ReceiveInfo = ReceiveInfo.Replace(",", ",");
|
|
ReceiveInfo = ReceiveInfo.Replace(";", ",");
|
|
ReceiveInfo = ReceiveInfo.Replace(";", ",");
|
|
ReceiveInfo = ReceiveInfo.Replace("、", ",");
|
|
var arr = ReceiveInfo.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
|
|
return arr;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
return new List<string>();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|