简体   繁体   English

无法通过ASP.NET中的Gmail帐户发送电子邮件

[英]Can't send email through Gmail account in ASP.NET

I've been having issues sending a simple email through my ASP.NET application with my gmail account. 我一直在通过Gmail帐户通过ASP.NET应用程序发送简单电子邮件时遇到问题。 I've looked for help as best I could, but for some reason it just keeps giving me an error and not sending any email. 我一直在寻求最大的帮助,但是由于某种原因,它总是给我一个错误,并且不发送任何电子邮件。

Here is my code that I have set up to trigger during a button press: 这是我设置为在按下按钮时触发的代码:

protected void SubmitButton_Click(object sender, EventArgs e)
{
    System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
    mail.To.Add(new System.Net.Mail.MailAddress("an email address"));
    mail.From = new System.Net.Mail.MailAddress("From Email Address");
    mail.Subject = "PDMS New User Request";
    mail.IsBodyHtml = true;
    mail.Body = "A new user has requested access" + "\n\n"
        + "Name: " + inputFName.Text.ToString() + " " + inputLName.Text.ToString()
        + "\n"
        + "Organization: " + inputOrg.Text.ToString()
        + "\n"
        + "Email: " + inputEmail.Text.ToString()
        + "\n\n"
        + "Please contact them with their login information"
        + "-PDMS System Message";
    mail.IsBodyHtml = true;
    System.Net.Mail.SmtpClient systemEmail = new System.Net.Mail.SmtpClient();
    systemEmail.UseDefaultCredentials = false;
    systemEmail.Credentials=new System.Net.NetworkCredential("From email address", "From Email Addresses' password");
    systemEmail.Port = 587 ;
    systemEmail.Host="smtp.gmail.com";
    systemEmail.EnableSsl=true;
    systemEmail.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
    try
    {
        systemEmail.Send(mail);
        Label1.Visible = true;
        Label2.Visible = false;
    }
    catch (Exception ex)
    {
        Label2.Text = "Email could not be sent due to errors";
    }

What is it that I am missing? 我想念的是什么? I just can't seem to get this code to actually send anything - is there a setting in Gmail that I need to configure first perhaps? 我似乎无法让此代码实际发送任何内容-Gmail中是否有需要首先配置的设置?

Remove Label in Catch and throw the Exception.Then we can easily identify the exact exception otherwise it display the label default text. 删除Catch中的Label并抛出异常,然后我们可以轻松地确定确切的异常,否则它将显示标签默认文本。

try
{
    systemEmail.Send(mail);
    Label1.Visible = true;
    Label2.Visible = false;
}
catch (Exception ex)
{
    throw ex;//throw the exception
}

Solution : 解决方案:

I think you are facing 5.5.1 Authentication Required 我认为您正面临5.5.1需要身份验证

The following steps will resolved your exception. 以下步骤将解决您的异常。

  • Enter the correct login password. 输入正确的登录密码。
  • To remove 2-Step Verification. 删除两步验证。
  • You have to enable login from other timezone / ip for your google account.To do this follow the Click Here and allow access by clicking the continue button. 您必须为您的Google帐户启用来自其他时区/ ip的登录名。为此,请单击“单击此处” ,然后单击“继续”按钮允许访问。
  • Go to security settings at the following Click here and enable less secure apps . 转到下面的安全设置。 单击此处 ,启用安全性较低的应用程序。 So that you will be able to login from all apps. 这样您就可以从所有应用程序登录。

I am unable to know what is the error you are getting, but yes there is a setting that needs to be done in your gmail account before you can start sending mails using code. 我无法得知您遇到的错误是什么,但是是的,您必须先在Gmail帐户中进行一项设置,然后才能开始使用代码发送邮件。 Please open below page from your google account and turn on access to less secure apps. 请从您的Google帐户打开以下页面,然后打开对安全性较低的应用的访问权限。

https://www.google.com/settings/security/lesssecureapps https://www.google.com/settings/security/lesssecureapps

Please note, if you have turned on 2 step verification for your google account then you will not see a learn more link on the above mentioned page. 请注意,如果您已为自己的Google帐户启用了两步验证,则在上述页面上将看不到“了解更多信息”链接。 From there you can create app specific password for your google account and then that app will be able to access and send mails through your google account. 您可以从那里为您的Google帐户创建应用专用密码,然后该应用即可通过您的Google帐户访问和发送邮件。

If this does not solves your issue, please edit your post and add the exact error trace that you are receiving so that we can look into it. 如果这不能解决您的问题,请编辑您的帖子并添加您收到的确切错误跟踪,以便我们进行调查。

Try this out ... I used it before so it may work 试试看...我以前用过,所以它可能会起作用

` `
public void SendEmail() { 公共无效的SendEmail(){

        MailMessage msg;
        string emailId = string.Empty;
        msg = new MailMessage();
        SmtpClient smtp = new SmtpClient();

        //sender email address
        msg.From = new MailAddress("youremail@gmail.com");

       //Receiver email address
        msg.To.Add("receiver@gmail.com");
        //email message subject
        msg.Subject = "some string";
        //email message body
        msg.Body = "Some string".Trim();
        msg.IsBodyHtml = true;
        smtp.Credentials = new NetworkCredential("yourEmail@gmail.com","yourpassword");
        smtp.Port = 587;
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        smtp.Send(msg);
    }`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM