首页 > 代码库 > 邮件发送
邮件发送
/// <summary>
/// 邮件发送
/// </summary>
/// <param name="host">服务器地址</param>
/// <param name="from">邮箱账号</param>
/// <param name="pwd">邮箱密码</param>
/// <param name="tomailList">接收邮箱(Ps:多个时以英文“,”分开)</param>
/// <param name="subject">标题</param>
/// <param name="body">内容</param>
/// <returns></returns>
public static bool SendMail(string host, string from, string pwd, string tomailList, string subject, string body)
{
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Host = host;
client.UseDefaultCredentials = true;
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
client.Credentials = new System.Net.NetworkCredential(from, pwd);
System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage();
Message.From = new System.Net.Mail.MailAddress(from);
foreach (string str in tomailList.Split(‘,‘))
{
if (!string.IsNullOrEmpty(str))
{
Message.To.Add(str);
}
}
Message.Subject = subject;
Message.SubjectEncoding = System.Text.Encoding.UTF8;
Message.BodyEncoding = System.Text.Encoding.UTF8;
Message.Priority = System.Net.Mail.MailPriority.High;
Message.IsBodyHtml = true;
try
{
Message.Body = body;
client.Send(Message);
return true;
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// 返回指定长度的随机数
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
public static string CreateRandom(int length)
{
char[] Pattern = new char[] { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘ };
string result = "";
int n = Pattern.Length;
System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
for (int i = 0; i < length; i++)
{
int rnd = random.Next(0, n);
result += Pattern[rnd];
}
return result;
}
邮件发送