5

Possible Duplicate:
Sending email through Gmail SMTP server with C#

For mailing with C# and using the Gmail SMTP server is there any kind of tricky thing that we should do? Because after lot of searching I found some ways for doing this, but I'm getting a failure exception as a result. I guess it's because I don't handle TSL for Gmail (because it works with TSL), but I don't know how to handle TSL with C# for doing this. I really appreciate any help or link to a useful sample. Here's my code:

public string SendMail(string senderMail, string receiverMail, string attachmentPath)
{
  var fromMailAddress = new MailAddress(senderMail);
  var toMailAddress = new MailAddress(receiverMail);

  MailMessage mailMessage = new MailMessage(fromMailAddress, toMailAddress);
  mailMessage.Subject = "My Subject";
  mailMessage.Body = "This is the body of this message for testing purposes";

  Attachment attachFile = new Attachment(attachmentPath);
  mailMessage.Attachments.Add(attachFile);

  SmtpClient emailClient = new SmtpClient();

  NetworkCredential credential = new NetworkCredential();
  credential.UserName = fromMailAddress.User;
  credential.Password = "password";

  emailClient.Credentials = credential;
  emailClient.Port = 587;
  emailClient.Host = "smtp.gmail.com";

  //emailClient.EnableSsl = true; //Here should be for TSL, but how?

  emailClient.Send(mailMessage);
}
Community
  • 1
  • 1
Sam
  • 51
  • 2
  • Welcome to SO! This is a good question and you attached the right code. However, a similar question was asked before, see the link above. It could be that you are encountering a special exception; in that case people can help you better, if you post the exact message of your exception. – Marijn Nov 30 '10 at 13:04
  • You used the word "TSL" three times, but I don't know what that is. Did you mean "TLS"? – Gabe Nov 30 '10 at 18:31
  • thanks for the guidance, yeah I meant TLS:D it's my new stupid keyboard which make me do these kinds of mistakes or maybe I'm blind or something:D. again thanks for the guidance, I really appreciate it. – Sam Dec 03 '10 at 14:49

2 Answers2

5

Try the following code. This is the working code I have been using for a long time.

    // Configure mail client (may need additional
    // code for authenticated SMTP servers).
    SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);

    // Set the network credentials.
    mailClient.Credentials = new NetworkCredential("YourGmailEmail@gmail.com", "YourGmailPassword");

    //Enable SSL.
    mailClient.EnableSsl = true;

    // Create the mail message (from, to, subject, body).
    MailMessage mailMessage = new MailMessage();
    mailMessage.From = new MailAddress("zain.you2@gmail.com");
    mailMessage.To.Add(to);

    mailMessage.Subject = subject;
    mailMessage.Body = body;
    mailMessage.IsBodyHtml = isBodyHtml;
    mailMessage.Priority = mailPriority;

    // Send the mail.
    mailClient.Send(mailMessage);

Reference: Sending Email using a Gmail Account.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zain Shaikh
  • 6,013
  • 6
  • 41
  • 66
  • thank you so much, It works perfectly. I was almost wanted to install IIS and manual SMTP for handling this problem but now I can use GMail perfectly, thanks a lot. – Sam Dec 03 '10 at 15:04
1

You should tell the message of the exception. But yes, uncomment that emailClient.EnableSsl = true; If it still not working, your firewall or router is problably blocking the ports.

alansiqueira27
  • 8,129
  • 15
  • 67
  • 111