我正在尝试通过以下操作方法发送电子邮件
[HttpPost]
public ActionResult ForgotPassword1()
{
dbAlKhaleejEntities _context = new dbAlKhaleejEntities();
var email = Request["Email"];
var email_adress = _context.CUSTOMERs.First(em => email == em.CUSTOMER_EMAIL);
var mailto = email_adress.CUSTOMER_EMAIL;
MailMessage msg = new MailMessage();
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Credentials = new NetworkCredential("umairliaquat63@gmail.com", "abc");
client.Host = "smtp.gmail.com";
client.Port = 587;
msg.From = new MailAddress("umairliaquat63@gmail.com");
msg.To.Add(mailto);
msg.Subject = "Password recovery";
msg.Body = "Test Recovering the password";
msg.IsBodyHtml = true;
msg.Priority = MailPriority.High;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Send(msg);
return RedirectToAction("forgetPassword");
}
但在“client.Send(msg)”行,它抛出异常“SMTP服务器需要安全连接或客户端未经过身份验证。服务器响应为:5.5.1需要身份验证”。我该如何解决这个问题
答案 0 :(得分:1)
据我所知,您不需要编写自己的代码,因为SmtpClient
可以使用您的配置:
<system.net>
<mailSettings>
<smtp from="It's me <its@my.email>">
<network host="smtp.ip.or.domain" port="527" defaultCredentials="false" userName="noreply" password="12345" enableSsl="true"/>
</smtp>
</mailSettings>
</system.net>
验证您的邮件设置两次。此外,SMTP服务器可以拒绝黑名单中的IP连接,也可以拒绝白名单中的IP连接。 SMTP服务器可以将您的邮件检测为垃圾邮件。
您可以使用自己的本地SMTP或专门的服务进行群发邮件。
答案 1 :(得分:0)
试试这个代码段:
using (var client = new SmtpClient(SmtpServerHost, SmtpPort)
{
Credentials = new NetworkCredential(NetworkCredentialUserName, Password),
EnableSsl = this.enableSSL
}
)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress(FromMailingAddress);
msg.Subject = this.Subject;
}