using System.Net.Mail;
调用:SendEmailForGetpass("收件人名称", "xx@xx.com", "smtp.163.com", "发件人名称", "xx@xx.com","xxxx","标题","主体");
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="toname">收件人名称</param>
/// <param name="toemail">收件人邮件地址</param>
/// <param name="smtpclient">伺服器</param>
/// <param name="fromname">发件人名称</param>
/// <param name="fromemail">发件人邮件地址</param>
/// <param name="password">发件人邮箱密码</param>
/// <param name="subject">邮件标题</param>
/// <param name="body">邮件主体</param>
/// <returns></returns>
public bool SendEmailForGetpass(string toname, string toemail, string smtpclient, string fromname, string fromemail, string password, string subject, string body)
{
try
{
MailAddress from = new MailAddress(fromemail, fromname);
MailAddress to = new MailAddress(toemail, toname);
MailMessage message = new MailMessage(from, to);
message.Subject = subject;
message.Body = body;
message.Priority = MailPriority.High;
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient(smtpclient);//伺服器,如"smtp.163.com"
client.Credentials = new System.Net.NetworkCredential(fromemail, password);
client.Send(message);
return true;
}
catch (Exception e)
{
return false;
}
}
/// 发送邮件
/// </summary>
/// <param name="toname">收件人名称</param>
/// <param name="toemail">收件人邮件地址</param>
/// <param name="smtpclient">伺服器</param>
/// <param name="fromname">发件人名称</param>
/// <param name="fromemail">发件人邮件地址</param>
/// <param name="password">发件人邮箱密码</param>
/// <param name="subject">邮件标题</param>
/// <param name="body">邮件主体</param>
/// <returns></returns>
public bool SendEmailForGetpass(string toname, string toemail, string smtpclient, string fromname, string fromemail, string password, string subject, string body)
{
try
{
MailAddress from = new MailAddress(fromemail, fromname);
MailAddress to = new MailAddress(toemail, toname);
MailMessage message = new MailMessage(from, to);
message.Subject = subject;
message.Body = body;
message.Priority = MailPriority.High;
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient(smtpclient);//伺服器,如"smtp.163.com"
client.Credentials = new System.Net.NetworkCredential(fromemail, password);
client.Send(message);
return true;
}
catch (Exception e)
{
return false;
}
}
调用:SendEmailForGetpass("收件人名称", "xx@xx.com", "smtp.163.com", "发件人名称", "xx@xx.com","xxxx","标题","主体");
如果碰上"身份验证失败的情况",则使用System.Web.Mail发送
代码
public bool SendMail(string to, string title, string content)
{
System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
mail.Body = content;
mail.From = "XXX@XXX.cn";
mail.Subject = title;
mail.To = to;
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.BodyFormat = System.Web.Mail.MailFormat.Html;
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "XXX@XXX.cn");//邮箱帐号
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "XXX");//密码
System.Web.Mail.SmtpMail.SmtpServer = "XXX"; //伺服器
try
{
System.Web.Mail.SmtpMail.Send(mail);
return true;
}
catch (Exception ex)
{
return false;
}
}
{
System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
mail.Body = content;
mail.From = "XXX@XXX.cn";
mail.Subject = title;
mail.To = to;
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.BodyFormat = System.Web.Mail.MailFormat.Html;
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "XXX@XXX.cn");//邮箱帐号
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "XXX");//密码
System.Web.Mail.SmtpMail.SmtpServer = "XXX"; //伺服器
try
{
System.Web.Mail.SmtpMail.Send(mail);
return true;
}
catch (Exception ex)
{
return false;
}
}