Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

59 linhas
2.4 KiB

using System;
using System.Collections.Generic;
using System.Web;
using System.Net.Mail;
/// <summary>
/// 发送邮件
/// </summary>
namespace Basic.Tools
{
public class Mail
{
/// <summary>
/// 发送邮件(smtp服务器/发送邮箱/邮箱密码/接收邮箱/发件人名称/邮件标题/邮件内容)
/// </summary>
/// <param name="_Host"></param>
/// <param name="_from"></param>
/// <param name="_pwd"></param>
/// <param name="_tomail"></param>
/// <param name="_senderDisplayName"></param>
/// <param name="_Subject"></param>
/// <param name="_Body"></param>
/// <returns></returns>
public static bool Send(string _Host, string _from, string _pwd, string _tomail, string _senderName, string _Subject, string _Body)
{
bool Result = false;
try
{
//创建smtpclient对象
System.Net.Mail.SmtpClient client = new SmtpClient();
client.Host = _Host;//163的smtp服务器是 smtp.163.com
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(_from, _pwd);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
MailAddress mailfrom = new MailAddress(_from, _senderName, encoding);//发件人邮箱地址,名称,编码UTF8
MailAddress mailto = new MailAddress(_tomail, "A", encoding);//收件人邮箱地址,名称,编码UTF8
//创建mailMessage对象
System.Net.Mail.MailMessage message = new MailMessage(mailfrom, mailto);
message.Subject = _Subject;
//正文默认格式为html
message.Body = _Body;
message.IsBodyHtml = true;
message.BodyEncoding = encoding;
message.SubjectEncoding = encoding;
//message.HeadersEncoding = encoding;
client.Send(message);
Result = true;
}
catch (Exception ex)
{
string e = ex.Message.ToString();
Result = false;
}
return Result;
}
}
}