CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: sending emails

  1. #1
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    sending emails

    I wrote the following piece of code for sending an email. First, I wanted to try it with gmail.

    Code:
        public class EmailSender
        {
            public static void SendEmail(
                     string from,
                     string to,
                     string smtpaddress, int port,
                     string username,
                     string password,
                     string title,
                     string body)
            {
                try
                {
                    SmtpClient client = new SmtpClient(smtpaddress, port);
                    client.Credentials = new System.Net.NetworkCredential(username, password);
                    client.EnableSsl = true;
                    client.UseDefaultCredentials = false;
    
                    MailMessage mail = new MailMessage();
                    mail.To.Add(to);
                    mail.From = new MailAddress(from);
                    mail.Subject = title;
                    mail.Body = body;
                    mail.IsBodyHtml = true;
    
                    client.Send(mail);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    Using this is pretty simple:
    Code:
       class Program
       {
          public static void Main(string[] args)
          {
             EmailSender.SendEmail(
                "john.doe@gmail.com",
                "jane.doe@gmail.com",
                "smtp.gmail.com",
                465,
                "john.doe",
                "password",
                "test",
                "<p>This is a test email!</p>");
          }
       }
    This doesn't work. I get a SmtpException, "The operation has timed out."

    I also tried it like this:
    Code:
       class Program
       {
          public static void Main(string[] args)
          {
             EmailSender.SendEmail(
                "<john.doe@gmail.com>",
                "<jane.doe@gmail.com>",
                "smtp.gmail.com",
                465,
                "john.doe",
                "password",
                "test",
                "<p>This is a test email!</p>");
          }
       }
    Same problem. If I change the port to 587 (http://mail.google.com/support/bin/a...n&answer=13287), I get the following exception:
    The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
    Anyone has suggestions what could be wrong?

    Thank you.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  2. #2
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: sending emails

    I am sure that most of the code you posted leaves off the actual username, however, many places require that the username be the e-mail address. Have you tried the full email address as the username?

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: sending emails

    Quote Originally Posted by sotoasty View Post
    I am sure that most of the code you posted leaves off the actual username, however, many places require that the username be the e-mail address. Have you tried the full email address as the username?
    Yes, I did that too.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: sending emails

    I got it: the order of these operations is essential:
    Code:
    client.Credentials = new System.Net.NetworkCredential(username, password);
    client.UseDefaultCredentials = false;
    it should be the other way around:
    Code:
    client.UseDefaultCredentials = false;
    client.Credentials = new System.Net.NetworkCredential(username, password);
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: sending emails

    Yikes. Requiring params to be in a certain order isn't too cool.

    Glad you got it figured out.

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: sending emails

    From MSDN :

    Some SMTP servers require that the client be authenticated before the server sends e-mail on its behalf. Set this property to true when this SmtpClient object should authenticate using the default credentials of the currently logged on user. If the UseDefaultCredentials property is set to false, then the value set in the Credentials property will be used for the credentials when connecting to the server.

    The thing that is actually strange to me is that the default value is false, in any case, so the way I see it, only this :
    Code:
    client.Credentials = new System.Net.NetworkCredential(username, password);
    Was needed.

  7. #7
    Join Date
    Jul 2009
    Posts
    8

    Re: sending emails

    This is great code, so I really wanted to try it.

    However, when I implement it within a very simple Windows Forms app with a simple Send button, it appears to fail.

    "Not responding... vshost.exe has stopped working."

    No exception was thrown. (I coded it to generate a message box if any exception was encountered, but none was encountered.)

    The code simply appears to hang on: client.Send(mail);

    All of my parameters look OK. (I substituted my gmail account credentials and tried sending a test message to my hotmail account.)

    My firewall does not appear to be blocking outgoing connections to the specified port.

    So, does anybody have any ideas why this might be hanging for me?

    Thanks.

  8. #8
    Join Date
    Jul 2009
    Posts
    8

    Smile Re: sending emails

    The key to finally getting this to work for me was the following:

    I was successful when I used port number 587.
    (For whatever reason, using port 465 did not work for me.)

    Also, btw: I was successful when I used my full gmail username.
    (e.g., I used "my_username@gmail.com" rather than simply "my_username".)

    In summary: Thank you very much, cilu! This is great!
    Last edited by Fortress7; July 12th, 2009 at 08:39 PM.

  9. #9
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: sending emails

    Right. I forgot to mention that it doesn't work on port 465, only 587.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  10. #10
    Join Date
    Sep 2010
    Posts
    1

    Re: sending emails

    Quote Originally Posted by cilu View Post
    I got it: the order of these operations is essential:
    Code:
    client.Credentials = new System.Net.NetworkCredential(username, password);
    client.UseDefaultCredentials = false;
    it should be the other way around:
    Code:
    client.UseDefaultCredentials = false;
    client.Credentials = new System.Net.NetworkCredential(username, password);
    you saved my day thanks

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured