简体   繁体   English

MONO / .NET 4-ASP.NET使用Google SMTP从页面发送电子邮件

[英]MONO / .NET 4 - ASP.NET sending email from page using Google SMTP

I dont know why, when I run this code which is called in the button onlick event handler, the page freezes and never comes back to be interactive. 我不知道为什么,当我运行在按钮onlick事件处理程序中调用的这段代码时,页面冻结,并且再也没有返回交互式状态。

And of course I dont recive an email. 当然,我不接收电子邮件。

public virtual void button1Clicked (object sender, EventArgs args)
    {

        SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 465);

        smtpClient.Credentials = new System.Net.NetworkCredential("my correct gmail adress", "my correct gmail account password");

        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtpClient.EnableSsl = true;
        smtpClient.Timeout = 5;
        MailMessage mail = new MailMessage();           

        mail.From = new MailAddress("my correct gmail adress", "My Site");
        mail.To.Add(new MailAddress("other correct email adress"));

        mail.Body = "Hello";

        smtpClient.Send(mail);
    }

I couldnt find the answer - so I started this question. 我找不到答案-所以我开始这个问题。

Maybe I am not specific enough, but also maybe sombody had problem like this and solved it. 也许我不够具体,但是也许某人有这样的问题并解决了。

Thanks JS 谢谢JS

If you are using the System.Net.Mail.SmtpClient : 如果您正在使用System.Net.Mail.SmtpClient

1) You are using "smtp.gmail.com", port should be 587, not 465 1)您正在使用“ smtp.gmail.com”,端口应为587,而不是465

SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);

2) Import the root cert and gmail ssl cert if you have not: 2)如果您尚未导入根证书和gmail ssl证书,请执行以下操作:

sudo mozroots --import --ask-remove
sudo certmgr -ssl smtps://smtp.gmail.com:465

3) Throw some security out the window to allow older TLS connections 3)将安全性丢到窗外以允许较早的TLS连接

    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
    SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587 );
    smtpClient.UseDefaultCredentials = false;

3) Run the program again, it will likely fail unless Google/Gmail is setup to accept ' less secure apps ' 3)再次运行该程序,除非将Google / Gmail设置为接受“ 安全性较低的应用 ”,否则该程序可能会失败

Unhandled Exception:
System.Net.Mail.SmtpException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvKF
534-5.7.14 m8ujrQCJbYSBmupvILyro-ffZjZHJHDm8Jy6SXO3OJpnw5Nx4lZlLG5aKPC9N8SvdFuOeZ
534-5.7.14 A4u5qmZkkLiWbTNjE99QrFM5WEz7yK8rcATt2eg35G77W7Z8lVvlwqbRBzVMftUl_Iq9tP
534-5.7.14 vwAs_TsNaViXxiSiPFf36j5SiyaN_ZigDciAgv9VWcASrr1BAJ0qRXtEI9blHl5iFuAwjz
534-5.7.14 mI9FPcb9kCjIuX1wxsZcDYGr2i7I> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14  Learn more at
534 5.7.14  https://support.google.com/mail/answer/78754 w9sm5345037pbs.82 - gsmtp
  at System.Net.Mail.SmtpClient.CheckStatus (SmtpResponse status, Int32 i) [0x00000] in <filename unknown>:0 
  at System.Net.Mail.SmtpClient.Authenticate (System.String user, System.String password) [0x00000] in <filename unknown>:0 
  at System.Net.Mail.SmtpClient.Authenticate () [0x00000] in <filename unknown>:0 
  at System.Net.Mail.SmtpClient.SendCore (System.Net.Mail.MailMessage message) [0x00000] in <filename unknown>:0 
  at System.Net.Mail.SmtpClient.SendInternal (System.Net.Mail.MailMessage message) [0x00000] in <filename unknown>:0 
  at System.Net.Mail.SmtpClient.Send (System.Net.Mail.MailMessage message) [0x00000] in <filename unknown>:0 

4) Log in to Gmail, look for the email they sent you concerning external account access. 4)登录Gmail,查找他们发送给您的有关外部帐户访问权限的电子邮件。 Follow the link to enable less secure apps. 点击链接以启用安全性较低的应用。

5) Try your app again. 5)再试一次您的应用程序。

Note: I use MailKit via source or Nuget for gmail and other services. 注意:我通过Source或Nuget将MailKit用于gmail和其他服务。 IMHO much cleaner and supported. 恕我直言,更清洁和支持。

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

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