如何在Asp.net MVC 5中发送电子邮件 [英] how to send email in Asp.net mvc 5

查看:115
本文介绍了如何在Asp.net MVC 5中发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该使用什么代码在C#中发送电子邮件?

What code should I use to send email in C#?

我试图找到一个特定的代码,以便可以从我的网站发送电子邮件. 然后我得到一个错误: "SMTP服务器需要安全连接,或者客户端未通过身份验证.服务器响应为:5.5.1需要身份验证.有关详细信息,请访问"

I tried to find a specific code so that I could send an email from my website. And then I get an error: "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at"

public void sendEmail(string toEmail, string subject, string emailBody)
{
     string senderEmail = "My_Email";
     string senderPassword = "My_Email_Password";
     SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
     client.EnableSsl = true;
     client.Timeout = 500000;
     client.DeliveryMethod = SmtpDeliveryMethod.Network;
     client.UseDefaultCredentials = false;
     client.Credentials = new NetworkCredential(senderEmail, senderPassword);
     MailMessage mailMessage = new MailMessage(senderEmail, toEmail, subject, emailBody);
     mailMessage.IsBodyHtml = true;
     mailMessage.BodyEncoding = UTF8Encoding.UTF8;
     client.Send(mailMessage);
}

我需要使用Google API吗?

Do I need to use Google API??

推荐答案

这是一个安全问题,默认情况下,Gmail阻止自定义应用程序访问您的电子邮件帐户.您可以将其设置为接受来自应用程序的登录.

Its a security issue, Gmail by default prevents access for your e-mail account from custom applications. You can set it up to accept the login from your application.

您需要通过以下链接转到安全设置: https://www.google. com/settings/security/lesssecureapps 并启用安全性较低的应用.这样您就可以从所有应用程序登录.

You need to go to security settings at the followig link https://www.google.com/settings/security/lesssecureapps and enable less secure apps. So that you will be able to login from all apps.

这篇关于如何在Asp.net MVC 5中发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆