none
Sending the Mail through SMTP in C# RRS feed

  • Question

  • HI ALL,

    I have faced some weired things while I was writing the code for sending the mail through SMTP. Definitely I might be doing mistake but I thought to get its full understanding so that I should learn from this mistake.

    Following sample of code throws exception - 

    SmtpClient client = new SmtpClient(GalaxyCommonConfig.smtpServer, GalaxyCommonConfig.smtpPort)
                    {
                        Credentials = new NetworkCredential(GalaxyCommonConfig.ServiceUserName, GalaxyCommonConfig.Servicepassword, GalaxyCommonConfig.ServiceDomain),
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                        UseDefaultCredentials = false;
                    };

    client.sendMail()...

    But Following code does not throw exception - 

    SmtpClient client = new SmtpClient(GalaxyCommonConfig.smtpServer, GalaxyCommonConfig.smtpPort)
                    {
                        Credentials = new NetworkCredential(GalaxyCommonConfig.ServiceUserName, GalaxyCommonConfig.Servicepassword, GalaxyCommonConfig.ServiceDomain),
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                    };

    I have a big confusion, because when I use UsedefaultCredentials = false, then why it is throwing exception. I am not using default credentials..in above code..

    Please help me to clear the concept.

    I also found one more weiredeness - 

    Following code also work fine - 

     SmtpClient client = new SmtpClient(GalaxyCommonConfig.smtpServer, GalaxyCommonConfig.smtpPort)
                    {
                        UseDefaultCredentials = false;
                        Credentials = new NetworkCredential(GalaxyCommonConfig.ServiceUserName, GalaxyCommonConfig.Servicepassword, GalaxyCommonConfig.ServiceDomain),
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                    };

    How it can be... It is disturbing me alot as I am not able to set my concept..


    • Moved by Mike Feng Thursday, February 21, 2013 6:02 AM
    Sunday, February 17, 2013 3:52 AM

Answers

  • You are getting an error because the mail server refueed to accept the connection.  SMTP has lot of options and if you add up all the different option it is a hard class to get working.  First there are three different type of connections

    1) A local mail server

    2) A remote Outlook Server

    3) A POP Mail Server

    Then there are two different types of credentials

    1) Using a credentails in your code

    2) Using the credentials on th elocal PC.  these are setup in Control Panel - Mail where yoiu would normally setup a POP account or an Outlook Account

    Next there are different security options like no encryption, SSL and TLS.  the security level is different depending on the Port number you are using the then mail server you are using.  Normally I refer people to check you mail server webpage for details on SMTP.  Each mail server is different.

    Then finally firewalls and virus checkers often block the SMTP ports making it impossible in some networks to get SMTP working without getting your MIS department to unblock the port number.


    jdweng

    Sunday, February 17, 2013 10:39 AM

All replies

  • You are getting an error because the mail server refueed to accept the connection.  SMTP has lot of options and if you add up all the different option it is a hard class to get working.  First there are three different type of connections

    1) A local mail server

    2) A remote Outlook Server

    3) A POP Mail Server

    Then there are two different types of credentials

    1) Using a credentails in your code

    2) Using the credentials on th elocal PC.  these are setup in Control Panel - Mail where yoiu would normally setup a POP account or an Outlook Account

    Next there are different security options like no encryption, SSL and TLS.  the security level is different depending on the Port number you are using the then mail server you are using.  Normally I refer people to check you mail server webpage for details on SMTP.  Each mail server is different.

    Then finally firewalls and virus checkers often block the SMTP ports making it impossible in some networks to get SMTP working without getting your MIS department to unblock the port number.


    jdweng

    Sunday, February 17, 2013 10:39 AM
  • Be aware your question has few relation to the Library classes (maybe that it is always used in .Net programming so every question is valid then).

    Try first to invest in the reply from Joel and ask it then in the VC# forum which you told you use as program language.

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/threads


    Success
    Cor

    Sunday, February 17, 2013 11:27 AM