简体   繁体   中英

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.

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

If you are using the System.Net.Mail.SmtpClient :

1) You are using "smtp.gmail.com", port should be 587, not 465

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

2) Import the root cert and gmail ssl cert if you have not:

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

    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 '

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. Follow the link to enable less secure apps.

5) Try your app again.

Note: I use MailKit via source or Nuget for gmail and other services. IMHO much cleaner and supported.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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