/// <summary>
/// 通過System.Web.Mail.MailMessage去發(fā)送,可以不被阿里云限制25端口的使用
/// 暫時一般都用465端口
/// </summary>
/// <param name="smtpserver">SMTP服務(wù),譬如:smtp.126.com</param>
/// <param name="userName">發(fā)件箱</param>
/// <param name="pwd">密碼</param>
/// <param name="nickName">昵稱</param>
/// <param name="strfrom">發(fā)件箱</param>
/// <param name="strto">收件箱</param>
/// <param name="MessageSubject">主題</param>
/// <param name="MessageBody">內(nèi)容</param>
/// <param name="SUpFile">附件</param>
/// <param name="port">端口</param>
/// <param name="enablessl">SSL加密</param>
/// <returns></returns>
public static bool SendWebEmail( string smtpserver, string userName, string pwd, string nickName, string strfrom, string strto,
string MessageSubject, string MessageBody, string SUpFile, int port, int enablessl = 0)
{
System.Web.Mail.MailMessage mmsg = new System.Web.Mail.MailMessage();
//郵件主題
mmsg.Subject = MessageSubject;
mmsg.BodyFormat = System.Web.Mail.MailFormat.Html;
//郵件正文
mmsg.Body = MessageBody;
//正文編碼
mmsg.BodyEncoding = Encoding.UTF8;
//優(yōu)先級
mmsg.Priority = System.Web.Mail.MailPriority.High;
System.Web.Mail.MailAttachment data = null ;
if (SUpFile != "" )
{
SUpFile = HttpContext.Current.Server.MapPath(SUpFile); //獲得附件在本地地址
System.Web.Mail.MailAttachment attachment = new System.Web.Mail.MailAttachment(SUpFile); //create the attachment
mmsg.Attachments.Add(attachment); //add the attachment
}
//發(fā)件者郵箱地址
mmsg.From = string .Format( "\"{0}\"<{1}>" ,nickName, strfrom);
//收件人收箱地址
mmsg.To = strto;
mmsg.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate" , "1" );
//用戶名
mmsg.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendusername" , userName);
//密碼
mmsg.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendpassword" , pwd);
//端口
mmsg.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/smtpserverport" , port);
//使用SSL
mmsg.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/smtpusessl" , (enablessl == 1 ? "true" : "false" ));
//Smtp服務(wù)器
System.Web.Mail.SmtpMail.SmtpServer = smtpserver;
try
{
System.Web.Mail.SmtpMail.Send(mmsg);
}
catch (Exception ex)
{
LogHelper.Error(ex.ToString());
return false ;
}
return true ;
}
|