110,579
社区成员
发帖
与我相关
我的任务
分享
using System.Net.Mail;
public static bool SendMail(string messTo,string messBody)
{
MailMessage mess = new MailMessage();
mess.From = new MailAddress("mmm306306@163.com", "报警系统");//发件人
mess.Subject = "报警系统";//主题
mess.IsBodyHtml = true;//允许HTML
mess.BodyEncoding = System.Text.Encoding.UTF8;//编码
mess.Body = messBody;
SmtpClient client = new SmtpClient();//new 出 客服端的 smtp
client.Host = "smtp.163.com";
client.Credentials = new System.Net.NetworkCredential("mmm306306", "xxxxxxxxx");//需要发件人的账号 密码
mess.To.Add(new MailAddress(messTo));//收件人 数组(可以群发)
try
{
client.Send(mess);
return true;
}
catch (Exception ex)
{
return false;
}
}
_________________________________________________________________________________________
MailMessage mail = new MailMessage();
mail.To = "me@mycompany.com";
mail.From = "abc@126.com";
mail.Subject = "this is a test email.";
mail.Body = "Some text goes here";
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "abc"); //set your username here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "your password"); //set your password
here SmtpMail.SmtpServer = "smtp.126.com"; //your real server goes here
SmtpMail.Send( mail );
public class MailHelper
{
/// <summary>
/// 发邮件
/// </summary>
/// <param name="to">收件人(以分号分隔的电子邮件地址列表)</param>
/// <param name="subject">主题</param>
/// <param name="message">邮件体</param>
/// <param name="ishtml">是否使用HTML发出</param>
/// <example>SendMail("xxx@xxx.com", DateTime.Now.ToString(), DateTime.Now.ToString());</example>
public static void SendMail(string to, string subject, string message, bool ishtml, Encoding encode)
{
if (ishtml == false)
{
//message = CharHelper.Html2Text(message);
}
SmtpClient smtpClient = new SmtpClient();
System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("/");
MailSettingsSectionGroup netSmtpMailSection = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
using (MailMessage msg = new MailMessage(netSmtpMailSection.Smtp.From, to, subject, message))
{
msg.BodyEncoding = encode;
msg.IsBodyHtml = ishtml;
smtpClient.Send(msg);
}
}