.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

猜你喜欢

转载自www.cnblogs.com/liwangxing-xiaotaiye/p/10132066.html