views:

5825

answers:

2

Hello,

I have a standard Google Apps account. I have setup a custom domain through Google Apps. I am able to send and receive emails successfully through Google Apps when I use the Gmail interface. However, I want to send an email via code. In order to attempt this, I have been trying the following code:

MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("[email protected]");
mailMessage.Subject = "Test";
mailMessage.Body = "<html><body>This is a test</body></html>";
mailMessage.IsBodyHtml = true;

// Create the credentials to login to the gmail account associated with my custom domain
string sendEmailsFrom = "[email protected]";             
string sendEmailsFromPassword = "password";
NetworkCredential cred = new NetworkCredential(sendEmailsFrom, sendEmailsFromPassword);

SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
mailClient.EnableSsl = true;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.UseDefaultCredentials = false;
mailClient.Timeout = 20000;
mailClient.Credentials = cred;
mailClient.Send(mailMessage);

When the Send method is reached, an Exception is thrown that states:

"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required."

How do I send emails through my custom domain via Google?

Thanks!

+16  A: 

There is no need to hard code all smtp settings in your code. Put them in web.config instead:

<configuration>
  <system.net>
    <mailSettings>
      <smtp from="[email protected]" deliveryMethod="Network">
          <network host="smtp.gmail.com" port="587"
              userName="[email protected]" password="password"/>
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

End when you send email just enable SSL on your SmtpClient:

var message = new MailMessage("[email protected]");
// here is an important part:
message.From = new MailAddress("[email protected]", "Mailer");
// it's superfluous part here since from address is defined in .config file
// in my example. But since you don't use .config file, you will need it.

var client = new SmtpClient();
client.EnableSsl = true;
client.Send(message);

Make sure that you're sending email from the same email address with which you're trying to authenticate at Gmail.

Note: With .NET 4.0+ you can insert enableSsl="true" into web.config as well instead of hardcoding it

Koistya Navin
the full webconfig is under <configuration> and should be <system.net> <mailSettings> <smtp from="[email protected]" deliveryMethod="Network"> <network host="smtp.gmail.com" port="587" userName="[email protected]" password="password"/> </smtp> </mailSettings> </system.net>
Simon_Weaver
you can also put the enableSsl in the config.
Shay Erlichmen
@Shay - how can you enableSSL in the config?
Rinat Abdullin
@Rinat its new with .NET 4.0 just put enableSsl in <smtp>
Shay Erlichmen
Thanks. Added +1. Unfortunately 90% of production application are still .NET 3.5 and below. Esp. ones on the Azure.
Rinat Abdullin
Azure now supports .Net 4.0 - This works great
Vyrotek
A: 

change the port to 465

sf