2

I am using the following very simple code and I keep getting System.Net.Mail.SmtpException failure sending mail. This is my code:

static void main(string[] args)
{
    MailAddress from = new MailAddress("[email protected]", "Mr. Test");
    MailAddress to = new MailAddress("[email protected]", "mr. man");
    MailMessage msg = new MailMessage(from, to);
    msg.Subject = "email";
    msg.Body = "This is email.";
    SmtpClient client = new SmtpClient("smtp.gmail.com");

    client.Send(msg);
}

I have never tried programmatically sending email before so I appreciate the help.

3
  • 1
    Can you get a full error stack with the message? Try exception.ToString() Commented Sep 7, 2011 at 19:06
  • 2
    I would assume it's because you aren't authenticating with gmail. Commented Sep 7, 2011 at 19:07
  • @George: How do I authenticate with gmail? Commented Sep 7, 2011 at 19:09

3 Answers 3

5

You're missing credentials and sending as TLS (secured connection):

Credentials = new NetworkCredential("[email protected]", "mypwd"), 
EnableSsl = true 

More Details here: Sending email through Gmail SMTP server with C#

Sign up to request clarification or add additional context in comments.

Comments

2

First thing I see incorrect with the code given is there is no username/password given for validation with the smtp server.

Also, to get a better idea of what exactly is causing the SmtpException, catch the exception in your debugger and look at the details of the exception. I've gotten good explanations of what is causing SMTP errors by doing this.

You can try following these directions to send mail using SMTP via gmail. http://blogs.msdn.com/b/mariya/archive/2006/06/15/633007.aspx

Comments

2

Google does not want you to use port 25, they want you to use 587 (ssl) or 467. They also require that you authenticate when sending mail.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.