.net 发送Email 单发 群发
把该引用的命名空间引用上,就能直接用了,单发短信已测试,群发未测试。
/// <summary>
/// 单发Email
/// </summary>
/// <param name="from">发信人地址</param>
/// <param name="to">收信人地址</param>
/// <param name="title">标题</param>
/// <param name="body">内容</param>
/// <returns></returns>
public static string sendEmail(string to, string title, string body)
{
try
{
MailMessage m_message = new MailMessage();
m_message.From = new MailAddress("*@126.com");
m_message.To.Add(new MailAddress(to));
m_message.Subject = title;
m_message.Body = body;
SmtpClient m_smtpClient = createClient();
m_smtpClient.Send(m_message);
return "发送成功";
}
catch (Exception e)
{
return e.Message;
}
}
/// <summary>
/// 群发Email
/// </summary>
/// <param name="from">发信人地址</param>
/// <param name="to">收信人地址列表</param>
/// <param name="title">标题</param>
/// <param name="body">内容</param>
/// <returns></returns>
public static string sendEmail(string[] to, string title, string body)
{
try
{
MailMessage m_message = new MailMessage();
m_message.From = new MailAddress("*@126.com");
foreach (string s in to)
{
m_message.To.Add(new MailAddress(s));
}
m_message.Subject = title;
m_message.Body = body;
SmtpClient m_smtpClient =createClient();
m_smtpClient.Send(m_message);
return "发送成功";
}
catch (Exception e)
{
return e.Message;
}
}
public static SmtpClient createClient()
{
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.126.com";
smtp.Port = 25;
smtp.UseDefaultCredentials = true;//不能和帐号顺序颠倒,否则出错。
smtp.Credentials = new System.Net.NetworkCredential("*@126.com", "******");
return smtp;
}