简体   繁体   English

无法使用 Gmail SMTP 发送电子邮件

[英]Cant send email using Gmail SMTP

I have a web with contact form - I want feedback being sent to my gmail account.我有一个带有联系表的网络 - 我希望将反馈发送到我的 Gmail 帐户。 For this I created another, dummy gmail account for sending these messages.为此,我创建了另一个虚拟 gmail 帐户来发送这些邮件。 Code looks like this代码看起来像这样

                MailMessage msg = new MailMessage();
                msg.To.Add("to_mail@gmail.com");
                msg.From = new MailAddress("from_mail@gmail.com");                
                msg.Subject = "Feedback";
                msg.Body = txtName.Text + " " + txtEmail.Text + " " + txtMessage.Text;
                msg.IsBodyHtml = true;

                SmtpClient client = new SmtpClient();
                client.Host = "smtp.gmail.com"; 
                client.Port = 587;
                client.UseDefaultCredentials = false;
                client.Credentials = new System.Net.NetworkCredential("from_mail@gmail.com", "password");
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.EnableSsl = true;                    
                client.Send(msg);

This code works fine when I run it from localhost from my VS project, but once I compile it and upload to the web, its stops working with this error.当我从 VS 项目的 localhost 运行它时,此代码工作正常,但是一旦我编译它并上传到网络,它就会停止工作,并出现此错误。

System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

Does anyone have an idea what caused it and how to fix it?有没有人知道是什么导致了它以及如何解决它? I already enabled access for less secure applications in my gmail account.我已经在我的 gmail 帐户中启用了对不太安全的应用程序的访问。 Tried also 465 port, but that doesnt work either.也尝试过 465 端口,但这也不起作用。

Thank a lot, Jozef非常感谢,约瑟夫

Finally found an answer.终于找到了答案。 Had to click this link with my "From" Gmail account logged in.必须在登录我的“发件人”Gmail 帐户的情况下单击此链接。

https://accounts.google.com/b/0/DisplayUnlockCaptcha https://accounts.google.com/b/0/DisplayUnlockCaptcha

Possible Reason:可能的原因:

In gmail, If the current login region and the previous login region is different, then gmail ask us some security questions before login.在gmail中,如果当前登录区域和之前登录区域不同,那么gmail会在登录前询问我们一些安全问题。 So better try to login once via browser in the system which you are using that C# app.因此,最好尝试在您使用该 C# 应用程序的系统中通过浏览器登录一次。

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

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