.net 一款很简单的直接发送邮件功能分享,直接Copy修改一下就能使用
public void Send(string subject, string body, string toEmail)
{
string username= "发送邮箱" //--从数据库中得到用来发邮件的Emial
string password= "发送邮箱密码" //--从数据库中得到用来发邮件的密码
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(username, "ERP");
mailMessage.To.Add(new MailAddress(toEmail)); // --你要发送给谁的邮箱
mailMessage.Subject = subject; //邮件标题
mailMessage.Body = body; //邮件内容
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587); //这是google邮箱 smtp和端口的设置,如果要换成其它邮箱发送,可以自己百度一下各邮箱的smtp设置
//使用SSL加密连线
smtpClient.EnableSsl = true;
NetworkCredential networkCredential = new NetworkCredential(username, password);
smtpClient.Credentials = networkCredential;
try
{
smtpClient.Send(mailMessage);
Console.WriteLine("订单同步失败报告:To:" + toEmail + " 发送成功");
}
catch (Exception ex)
{
Console.WriteLine("订单同步失败报告:To:" + toEmail + " 发送失败,失败原因" + ex.Message);
}
}
打点小广告,感谢大家支持