none
Error while sending email with C# console app RRS feed

  • Question

  • Hi everyone.

    I wanted to send automatic email with C# console app. Here is my code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net.Mail;
    using System.Net;
    
    namespace test_email
    {
        class Program
        {
            static void Main(string[] args)
            {
                SmtpClient client = new SmtpClient("smtp.gmail.com", 465);
                client.EnableSsl = true;
                MailAddress from = new MailAddress("[address_from]@gmail.com");
                MailAddress to = new MailAddress("[address_to]@gmail.com");
                MailMessage message = new MailMessage(from, to);
                message.Body = "This is a test e-mail message sent using gmail as a relay server ";
                message.Subject = "Gmail test email";
                NetworkCredential myCreds = new NetworkCredential("[address_from]@gmail.com", "[pass]");
                client.Credentials = myCreds;
                
    
                try
                {
                    client.Send(message);
                }
    
                catch (Exception ex)
                {
                    Console.WriteLine("Exception is:" + ex.ToString());
                }
    
                Console.WriteLine("Goodbye.");
            }
        }
    }
    

    However, when I execute this code, I get error "Application exited with code 0 (0x0). How it can be fixed?

    Regards,

    Yerkhan

    Monday, September 9, 2019 7:25 AM

Answers

  • Hi again.

    Issue was in laptop security configuration and work network settings. They didn't allow me to send email.

    I tried this on home laptop and it worked. 

    Sorry and thanks to all.

    Regards,

    Yerkhan

    • Marked as answer by Yerkhan Tuesday, September 10, 2019 12:33 PM
    Tuesday, September 10, 2019 12:33 PM

All replies

  • Hi Yerkhan,

    Thank you for posting here.

    >>Application exited with code 0 (0x0).

    I want to mention that this is not an error. When your program is completed, it will tell you it.

    By the way, I want to mention that we need use the correct port '587' instead of '465'.

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

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 9, 2019 9:18 AM
    Moderator
  • Jack,

    Thank you for your response. 

    Changed port to 587. 

    >>I want to mention that this is not an error. When your program is completed, it will tell you it.

    However, I didn't get message delivered to target email. What could be the problem?

    Also, since I've updated port, I got message that operation has timed out. Should I increase waiting?

    Regards,

    Yerkhan

    Monday, September 9, 2019 10:27 AM
  • Jack,

    I also noticed, that here is written, that SmtpClient is now obsolete. Is there alternative to this method using C# console app?

    Regards,

    Yerkhan

    Monday, September 9, 2019 11:45 AM
  • Following the link that is linked to what you linked to indicates a third-party library should be used. Still I've not seen anyone have issues with SmtpClient so personally I'd use it unless there was an issue.

    The port 465 seems correct for Google as documented here. Receiving email can take a while. I'd give it 5 or 10 minutes. Also check to see if your app reported an error.

    The "error" is just an informational message from the debugger. If you look at the console output that you're printing you should see your message before the app terminates. If you want to confirm this put a `Console.ReadLine()` call after that or set a breakpoint on the last line and it'll hit it.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, September 9, 2019 1:47 PM
    Moderator
  • Hi,

    Thanks for your reply.

    I got troubles with connecting to smtp server. I set timeout for connection to 1 hour, and it didn't succeed. Error was: System.Net.Mail.SmtpException: The operation has timed out. I used following code:

    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                SmtpServer.Port = 587; 
                SmtpServer.EnableSsl = true;
                SmtpServer.Timeout = 3600000; 
                SmtpServer.Send(mail);

    On Send command, it took a lot of time. What can be an issue?

    Regards,

    Yerkhan

    Monday, September 9, 2019 3:44 PM
  • An hour timeout? Nothing should take that long to send, even with attachments. 

    If I were to hazard a guess I'd say your firewall (or something on the network) is blocking the call. This will cause the request to block until it times out.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, September 9, 2019 4:10 PM
    Moderator
  • I also noticed, that here is written, that SmtpClient is now obsolete. Is there alternative to this method using C# console app?

    I vaguely recall sending a message to Microsoft about that type of thing. I am not sure but I think that they have that message when the version of .Net is old. The problem is (I think) that it appears even if the specific class has not changed. If that is true then it is a useless message and they are working on a solution and it seems to be not easy to fix.



    Sam Hobbs
    SimpleSamples.Info

    Monday, September 9, 2019 7:44 PM
  • I suggest using the sample in SmtpClient Class. It uses SendAsyncCancel. There might be an error occurring after the synchronous Send completes. You will need to add a NetworkCredential to the sample and make other obvious changes. Also, when you get the code working you will likely want to use an event or something like that to wait for completion.

    Also see Send email from a printer, scanner, or app - G Suite Admin Help. Although you are not using a printer or scanner I think that page has relevant information. It says that port 465 is for SSL and port 587 is for TLS.



    Sam Hobbs
    SimpleSamples.Info

    Monday, September 9, 2019 8:27 PM
  • Hi,

    Checked for three different networks, smtp server returns ping. However, message sent command always times out.

    Regards,

    Yerkhan

    Tuesday, September 10, 2019 6:03 AM
  • Hi.

    Tried using send Async method. It runs without errors or timeouts, however, does not deliver message.

    I also tried sync and async send methods on Powershell, it gives me time out exception. And using new gmail account just to check.

    Regards,

    Yerkhan


    • Edited by Yerkhan Tuesday, September 10, 2019 6:38 AM
    Tuesday, September 10, 2019 6:34 AM
  • Hi Yerkhan,

    Thanks for the feedback.

    I have checked it again. However, I could send email successfully only changing the port to 587.

    Therefore, I suggest that you could check if your email address or password is wrong. You also could set

    break point to see which line throws the exception.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 10, 2019 6:49 AM
    Moderator
  • Hi again,

    I've tried different approaches, and my guess is that problem is in gmail. Since, I used 4 different approaches, and all of them gave me time out exception.

    Regards,

    Yerkhan

    Tuesday, September 10, 2019 7:09 AM
  • It happened before when .NET v1.X => v2+, when they switch from CDOSYS based System.Web.Mail to .NET native System.Net.Mail.

    However for this one, the replacement package is shown on the obsolete message:

    https://github.com/jstedfast/MailKit
    https://github.com/jstedfast/MimeKit

    A quick glimpse make me think that it's a superior package that packs multiple features that were missing in System.Net.Mail. From the license information it comes from Xamarin which is now Microsoft (or a Microsoft Subsidiary if need to be more specific).

    I think I'll explore it on my next project that need to send and receive mail.





    Tuesday, September 10, 2019 7:16 AM
    Answerer
  • Hi again Jack,

    My guess is the settings of gmail. Could your share your gmail account settings? So I can replicate it on my account.

    Thanks.

    Regards,

    Yerkhan

    Tuesday, September 10, 2019 9:00 AM
  • Hi Yerkhan,

    Thanks for the feedback.

    My settings are the default settings, so I suggest you can restore the default settings. Or you can recreate an account to test.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 10, 2019 9:12 AM
    Moderator
  • Hi again.

    Issue was in laptop security configuration and work network settings. They didn't allow me to send email.

    I tried this on home laptop and it worked. 

    Sorry and thanks to all.

    Regards,

    Yerkhan

    • Marked as answer by Yerkhan Tuesday, September 10, 2019 12:33 PM
    Tuesday, September 10, 2019 12:33 PM