【问题标题】:Sending email from asp.net using Gmail SMTP OAUTH [duplicate]使用 Gmail SMTP OAUTH 从 asp.net 发送电子邮件 [重复]
【发布时间】:2017-06-21 17:10:42
【问题描述】:

我只想实现一个示例邮件发送应用程序。 我为此做了很多研究。从各个角落我都得到了同样的解决方案。但这对我不起作用。我找不到我的错误。我正在使用下面的代码

protected void SendEmail(object sender, EventArgs e)
{
    string to = txtTo.Text;
    string from = txtEmail.Text;
    string subject = txtSubject.Text;
    string body = txtBody.Text;
    using (MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text))
    {
        mm.Subject = txtSubject.Text;
        mm.Body = txtBody.Text;
        if (fuAttachment.HasFile)
        {
            string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
            mm.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
        }
        mm.IsBodyHtml = false;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        NetworkCredential NetworkCred = new NetworkCredential(txtEmail.Text, txtPassword.Text);
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;
        smtp.Send(mm);
        ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
    }
}

得到如下异常

请建议我如何解决这个问题。

提前致谢。

【问题讨论】:

  • 通过关闭我的 gmail 帐户中的“不太安全的应用程序”选项来尝试提出的解决方案,它给出了与我上面提到的相同的异常。

标签: c# asp.net email network-programming


【解决方案1】:

假设您使用正确的 Gmail 凭据来发送电子邮件。您需要使用以下链接打开“不太安全的应用程序”设置:https://www.google.com/settings/u/1/security/lesssecureapps

【讨论】:

  • 感谢您的建议。是的,它工作正常。但我还有一个疑问,打开不太安全的应用程序选项会不会导致任何安全问题。现在是安全的吗?
  • 它不会引发任何安全问题。还有一种方法,你可以在 Gmail 中创建 App 并使用该 App 发送电子邮件。
  • 您能提出您的建议吗?
  • 查看此链接emailarchitect.net/easendmail/sdk/html/object_oauth.htm如果您有任何进一步的疑问,请告诉我
猜你喜欢
  • 2011-08-18
  • 1970-01-01
  • 2011-08-01
  • 2016-02-29
  • 1970-01-01
  • 1970-01-01
  • 2015-10-22
  • 2016-10-25
  • 1970-01-01
相关资源
最近更新 更多