69

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?

4
  • Does port 465 work better maybe? Commented Apr 16, 2009 at 21:01
  • What is the problem with code above? Commented Feb 19, 2011 at 12:38
  • 1
    I'm getting a timeout issue, but looking at all the code smaples, it seems like my code and settings are correct. Commented Jul 11, 2014 at 20:12
  • 3
    Late addition: Google requires you to "Enable less secure apps" for this to work: support.google.com/accounts/answer/6010255?hl=en Commented Sep 8, 2015 at 13:10

5 Answers 5

95

There is no need to hardcode all SMTP settings in your code. Put them in web.config instead. This way you can encrypt these settings if needed and change them on the fly without recompiling your application.

<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: Starting with .NET 4.0 you can insert enableSsl="true" into web.config as opposed to setting it in code.

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

7 Comments

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>
you can also put the enableSsl in the config.
@Shay - how can you enableSSL in the config?
@Rinat its new with .NET 4.0 just put enableSsl in <smtp>
Thanks. Added +1. Unfortunately 90% of production application are still .NET 3.5 and below. Esp. ones on the Azure.
|
9

This is what I use in WPF 4

SmtpClient client = new SmtpClient(); client.Credentials = new NetworkCredential("[email protected]", "P@$$w0rD"); client.Port = 587; client.Host = "smtp.gmail.com"; client.EnableSsl = true; try { MailAddress maFrom = new MailAddress("[email protected]", "Sender's Name", Encoding.UTF8), MailAddress maTo = new MailAddress("[email protected]", "Recipient's Name", Encoding.UTF8); MailMessage mmsg = new MailMessage(maFrom, maTo); mmsg.Body = "<html><body><h1>Some HTML Text for Test as BODY</h1></body></html>"; mmsg.BodyEncoding = Encoding.UTF8; mmsg.IsBodyHtml = true; mmsg.Subject = "Some Other Text as Subject"; mmsg.SubjectEncoding = Encoding.UTF8; client.Send(mmsg); MessageBox.Show("Done"); } catch (Exception ex) { MessageBox.Show(ex.ToString(), ex.Message); //throw; } 

Watch for Firewalls and Anti-Viruses. These creepy things tend to block applications sending email. I use McAfee Enterprise and I have to add the executable name (like Bazar.* for both Bazar.exe and Bazar.vshost.exe) to be able to send emails...

3 Comments

I want to make a "contact us" page so users send emails to site-admin. Is there a way to set "From" to their email address??
You can spoof the sender's address; but there's a good risk of getting your email marked as spam. I suggest you send an email from [email protected] to both admin and the person who's trying to contact you. Then your admin can send a reply to all.
There's a small bug here, causing the display names to be thrown away. The MailMessage construction should look like this: MailMessage mmsg = new MailMessage(maFrom, maTo);
4

change the port to 465

Comments

3

There is not need to do anything. Just login in your current account first time and follow instructions.

Your problem will resolve. It occur because you had created the account in google apps but did not login. Just login and follow the instructions and try.

1 Comment

Given that the OP's code seems fine, this may actually have been the problem.
0

My code is connecting to smtp.google.com using TLS on Port=587 (SSL should be port 465) but still needs EnableSsl=true

SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; smtp.Port = 587; smtp.EnableSsl = true; smtp.UseDefaultCredentials = false; NetworkCredential credentials = new NetworkCredential(); credentials.UserName = "INSERT EMAIL"; credentials.Password = "INSERT PASSWORD"; smtp.Credentials = credentials; MailAddress addressFrom = new MailAddress(credentials.UserName); MailAddress addressTo = new MailAddress("INSERT RECIPIENT"); MailMessage msg = new MailMessage(addressFrom, addressTo); msg.Subject = "SUBJECT" msg.Body = "BODY"; smtp.Send(msg); 

Notice these important prerequisites on your G SUITE account

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.