通过smtp直接发送邮件
/// <summary>
/// SMTP发送邮件
/// </summary>
/// <param name="fromEmail">发送邮件地址</param>
/// <param name="toEmail">收件箱</param>
/// <param name="subject">邮箱主题</param>
/// <param name="contentAttachment"></param>
/// <param name="fromEmailPwd">发送邮件密码</param>
/// <returns></returns>
public static bool SendMailBySMTP(string fromEmail, string toEmail, string subject, Attachment contentAttachment,
string fromEmailPwd)
{
bool rr = true;
if (ConfigurationManager.AppSettings["CreditoEmailSwitch"].ToString() == "true")
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(fromEmail, toEmail);
mail.SubjectEncoding = Encoding.UTF8;
mail.Subject = subject;
mail.IsBodyHtml = true; //是否允许内容为 HTML 格式
mail.BodyEncoding = Encoding.UTF8;
string emailContent = "邮件内容";
mail.Body = emailContent;
mail.Attachments.Add(contentAttachment); //添加一个附件
int defaltFlag = 0;
SmtpClient smtp = new SmtpClient("fastsmtp.qiye.163.com");//serviceFlag > 14 ? "smtp.qiye.163.com" :
smtp.Port = 25;
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential(fromEmail, fromEmailPwd); //SMTP 验证
//smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
rr = false;
SysLog.Write("发送邮件异常", ex.Message + "时间:" + DateTime.Now.ToString());
}
finally
{
mail.Attachments.Dispose(); //邮件发送完毕,释放对附件的锁定
}
}
else
{
rr = false;
}
return rr;
}