1

I have simple console app and trying to send mail via SMTP class in C# but no mail is sent, app is closed and no exception is thrown as well. Problem line is client.Send(msg).

using (var client = new SmtpClient()) {
  client.SendCompleted += new SendCompletedEventHandler(smtp_SendCompleted);
  client.Port = 587;
  client.DeliveryMethod = SmtpDeliveryMethod.Network;                     
  client.EnableSsl = True;
  client.Timeout = 20000;
  client.Credentials = new NetworkCredential(@ConfigurationManager.AppSettings["senderEmailAddress"].ToString(),@ConfigurationManager.AppSettings["senderEmailPassword"].ToString());
  client.Host = "smtp.gmail.com;

  using (var msg = new MailMessage()) { 
    msg.From = new MailAddress(@ConfigurationManager.AppSettings["senderEmailAddress"].ToString());
    msg.Sender = new MailAddress(@ConfigurationManager.AppSettings["senderEmailAddress"].ToString());
    msg.Subject = fileName;
    msg.Attachments.Add(new Attachment(OutputFilePath));
    msg.To.Add(EmailAddress);
    client.Send(msg); }}

private void smtp_SendCompleted(object sender, AsyncCompletedEventArgs e) {
        if (e.Cancelled == true || e.Error != null) {
            throw new Exception(e.Cancelled ? "EMail sedning was canceled." : "Error: " + e.Error.ToString());
        }
    }

Is possible at least to get anyhow exception?

petrppe
  • 145
  • 2
  • 14
  • Have you configured your Gmail account to allow external connections? – acostela Sep 23 '15 at 08:01
  • I'm not sure what you mean but I use this account in smartphone or Outlook. – petrppe Sep 23 '15 at 08:06
  • This was answered earlier in SO. [http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail](http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail) – Hari Prasad Sep 23 '15 at 08:08
  • I saw in your code that you are using "gmail". Usually Gmail block for security reasons your apps and you need to allow them to access your account. Take a look here https://support.google.com/accounts/answer/6010255?hl=en – acostela Sep 23 '15 at 08:09
  • I have allowed less secure apps and also have using system.net.mail. – petrppe Sep 23 '15 at 08:14
  • Maybe could help that app is closed on line client.Send(msg). – petrppe Sep 23 '15 at 08:23
  • if there was a permissions failure, or if the SMTP could not connect, an exception would be thrown. Check your event viewer for errors. – user1666620 Sep 23 '15 at 08:48

2 Answers2

0

Please make sure 2 step authentication must be turn off in sender gmail account.

if 2 step authentication remain on then it will not able to make secure SSL connection with gmail server.

Satish Kumar sonker
  • 1,250
  • 8
  • 15
0

The only thing I can see which is missing from your code is the following:

client.UseDefaultCredentials = false;

Add it above the following line of code:

client.Credentials = new NetworkCredential(@ConfigurationManager.AppSettings["senderEmailAddress"].ToString(),@ConfigurationManager.AppSettings["senderEmailPassword"].ToString());

If there was an error regarding the login into gmail, an exception would be thrown - make sure to check your event viewer and make sure you don't have an empty catch block which is swallowing the exception.

For gmail there is no point specifying msg.From since google doesn't allow impersonation and so it will be ignored.

user1666620
  • 4,800
  • 18
  • 27
  • @petrppe how are you checking whether this worked or not? are you waiting to receive the email, or are you checking the outbox/sent mails section? how do you know that no exceptions are thrown? have you wrapped the entire SMTPClient using section in a try/catch? – user1666620 Sep 23 '15 at 09:27
  • I am waiting to receive the email. Yes, smtp client is entirely wrapped in try-catch blog. – petrppe Sep 23 '15 at 09:32
  • @petrppe have you checked the drafts/outbox/sent items of the gmail account you are trying to connect to? – user1666620 Sep 23 '15 at 09:34
  • A little bit of progress.. I received one email after while, I have no idea how cause I haven't changed anything. Anyway it's still only one email from many attempts and app is still crashing. – petrppe Sep 23 '15 at 10:05
  • why is the app crashing? what exception is being thrown? what does the event viewer say? it won't have crashed without an exception showing at the very least in the event viewer. – user1666620 Sep 23 '15 at 10:06
  • There is no exception thrown. Just on the line client.Send(msg) app is crashed or exited without any notification or something. I don't know how to use debugging app with event viewer. – petrppe Sep 23 '15 at 10:24
  • @petrppe https://technet.microsoft.com/en-us/library/aa997769(v=exchg.65).aspx check Windows Logs > Application and check for errors/warnings at the time your application crashed. the event viewer Application log will show any uncaught exceptions thrown by default. if your application is just dying without any error logged, you have an uncaught exception somewhere. – user1666620 Sep 23 '15 at 10:28
  • If you mean this http://oi62.tinypic.com/2jbmplu.jpg (in the elipse), I don't see anything from this app. It's not crashing, it's just exiting like I would click button for stop debugging. – petrppe Sep 23 '15 at 11:45
  • It does same thing even if I have no configuration of smtp client. – petrppe Sep 23 '15 at 12:50