邮件发送的帮助类
来源:互联网 发布:c语言中长整型变量 编辑:程序博客网 时间:2024/06/06 01:43
public sealed class EmailHelper
{
private static readonly string smtp;
private static readonly string fromAddress;
private static readonly string accountName;
private static readonly string password;
static EmailHelper()
{
smtp = ConfigHelper.GetValueByAttribute("SystemConfig", "smtpMallAdd");
fromAddress = ConfigHelper.GetValueByAttribute("SystemConfig", "smtpMall");
accountName = ConfigHelper.GetValueByAttribute("SystemConfig", "smtpMallName");
password = ConfigHelper.GetValueByAttribute("SystemConfig", "smtpMallPassword");
}
/// <summary>
/// 发邮件
/// </summary>
/// <param name="subject">邮件主题</param>
/// <param name="body">邮件内容</param>
/// <param name="toAddress">收件人地址</param>
public static void SendMail(string subject, string body, string toAddress)
{
SendMail(subject, body, toAddress, "");
}
/// <summary>
/// 发邮件
/// </summary>
/// <param name="subject">邮件主题</param>
/// <param name="body">邮件内容</param>
/// <param name="toAddress">收件人地址</param>
/// <param name="mailModel">邮件模版</param>
public static void SendMail(string subject, string body, string toAddress, string mailModel)
{
SendMail(subject, body, toAddress, mailModel, "");
}
/// <summary>
/// 发带附件邮件
/// </summary>
/// <param name="subject">邮件主题</param>
/// <param name="body">邮件内容</param>
/// <param name="toAddress">发送地址</param>
/// <param name="mailModel">邮件模版</param>
/// <param name="fileName">除件名称</param>
public static void SendMail(string subject, string body, string toAddress, string mailModel, string fileName)
{
var client = new SmtpClient
{
DeliveryMethod = SmtpDeliveryMethod.Network,
Host = smtp,
Credentials = new NetworkCredential(accountName, password)
}; //实例化一个SmtpClient
var msg = new MailMessage(fromAddress, toAddress);
string model = mailModel != "" ? ConfigHelper.GetValueByAttribute("SystemMailModel", mailModel) : ConfigHelper.GetValueByAttribute("SystemMailModel", "mailModel");
msg.Body = model.Replace("[description]", body);
msg.Subject = subject;
msg.IsBodyHtml = true;
if (fileName != "")
{
msg.Attachments.Add(new Attachment(fileName));
}
try
{
client.Send(msg);
}
catch (Exception)
{
}
}
/// <summary>
/// 发邮件
/// </summary>
/// <param name="subject">邮件主题</param>
/// <param name="body">邮件内容</param>
/// <param name="toAddressList">收件人地址列表</param>
public static void SendMail(string subject, string body, List<string> toAddressList)
{
foreach (var item in toAddressList)
{
SendMail(subject, body, item, "");
}
}
/// <summary>
/// 获取用户填写的Email所在的邮件服务器地址
/// </summary>
/// <param name="strEmail">email地址</param>
/// <returns>如果EMIAL地址有效则返回地址,没有则返回null</returns>
private static string GetMailServer(string strEmail)
{
if (!IsEmail(strEmail))
{
return null;
}
string strDomain = strEmail.Trim().ToLower().Split('@')[1];
var PSinfo = new ProcessStartInfo
{
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
FileName = "nslookup",
CreateNoWindow = true,
Arguments = ("-type=mx " + strDomain)
};
Process proc = Process.Start(PSinfo);
if (proc != null)
{
StreamReader Sreader = proc.StandardOutput;
var rgx = new Regex("mail exchanger = (?<mailServer>[^//s]+)");
string strResponse;
while ((strResponse = Sreader.ReadLine()) != null)
{
Match aMatch = rgx.Match(strResponse);
if (rgx.Match(strResponse).Success)
{
string Gvalue = aMatch.Groups["mailServer"].Value;
return Gvalue;
}
}
}
return null;
}
//正则表达式验证Email地址格式
private static bool IsEmail(string str_Email)
{
return Regex.IsMatch(str_Email, @"^([/w-/.]+)+[a-zA-Z0-9]+@((/[[0-9]{1,3}/.[0-9]{1,3}/.[0-9]{1,3}/.)|(([/w-]+/.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(/]?)$");
}
public static bool ValidateEmail(string strEmail)
{
string msg = GetMailServer(strEmail);
return !string.IsNullOrEmpty(msg);
}
}
- 邮件发送的帮助类
- .Net 邮件发送帮助类
- C#邮件发送帮助类
- 发送邮件的类
- PHP 发送邮件的类
- Python发送邮件的类
- C#发送邮件的类
- 邮件帮助类
- 简单邮件的邮件发送
- java 邮件发送 --- 简单的邮件发送
- 我用的发送邮件的类
- c#邮件发送 邮件类
- 关于PHP 发送SMTP邮件 的邮件类phpmailer 参数
- 用Spring的邮件封装类JavaMailSenderImpl发送邮件
- 发送邮件的类(转载)
- 改进了的一个邮件发送类
- 我常用的邮件发送类
- 改进了的一个邮件发送类
- 关于char, wchar_t, TCHAR, _T(),L,宏 _T、TEXT,_TEXT、L
- Delphi QuotedStr函数的原型
- C++实现回文
- ajaxpro综合示例-ajaxpro无刷新更新gridview数据
- DX学习笔记
- 邮件发送的帮助类
- VC 绘制颜色渐变区域
- scanf返回值
- prototype中的继承
- IIS状态代码的含义
- Building the android code
- Tuscany SCA与OSGI的结合方式
- ASP.NET和网页游戏的设想
- 函数指针和指针函数