1. 程式人生 > >.net阿里雲傳送郵件25埠不能使用

.net阿里雲傳送郵件25埠不能使用

 

//原始程式碼

public bool Send(郵箱配置 email, string to, string subject, string body)
{
SmtpClient smtp = new SmtpClient();
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Host = email.郵箱伺服器;
smtp.Port = port;
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential(email.郵箱賬號, email.郵箱密碼);
MailMessage mm = new MailMessage();
mm.Priority = MailPriority.Normal;
mm.From = new MailAddress(email.郵箱賬號, email.傳送郵件暱稱);
mm.To.Add(to);
mm.Subject = subject;
mm.Body = body;
mm.IsBodyHtml = true;
try
{
smtp.Send(mm);
}
catch (Exception ex)
{
return false;
}

return true;
}

釋出到阿里雲伺服器上沒有用因為阿里雲關掉了25埠,想使用465,但是也沒有用,只能重寫程式碼

public bool Send(郵箱配置 email, string to, string subject, string body)
{
try
{
CDO.Message oMsg = new CDO.Message();

oMsg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = 465;//設定埠
oMsg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = email.郵箱伺服器;
oMsg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = email.郵箱賬號;
oMsg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpuserreplyemailaddress"].Value = email.郵箱賬號;
oMsg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value = email.郵箱賬號;
oMsg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = email.郵箱賬號;
oMsg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = email.郵箱密碼;
oMsg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;
oMsg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = 1;
oMsg.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"].Value = "true";//這一句指示是否使用ssl
oMsg.Configuration.Fields.Update();
oMsg.HTMLBody = body;

oMsg.Subject = subject;

oMsg.From =email.傳送郵件暱稱+"<"+ email.郵箱賬號+">";
oMsg.To = to;
oMsg.Send();
}
catch (System.Net.Mail.SmtpException ex)
{
throw ex;
return false;
}
return true;
}

cod.message的引用位置: C:\Windows\System32\cdosys.dll

文章出處引用地址https://www.ie81.com/Technology/225.html?tdsourcetag=s_pcqq_aiomsg