[ACCEPTED]-Send Email via C# through Google Apps account-google-apps

Accepted answer
Score: 94

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

<configuration>
  <system.net>
    <mailSettings>
      <smtp from="example@domain.com" deliveryMethod="Network">
          <network host="smtp.gmail.com" port="587"
              userName="example@domain.com" password="password"/>
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

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

var message = new MailMessage("navin@php.net");
// here is an important part:
message.From = new MailAddress("example@domain.com", "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 5 sure that you're sending email from the 4 same email address with which you're trying 3 to authenticate at Gmail.

Note: Starting with 2 .NET 4.0 you can insert enableSsl="true" into 1 web.config as opposed to setting it in code.

Score: 9

This is what I use in WPF 4

SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("sender_email@domain.tld", "P@$$w0rD");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;

try 
{
    MailAddress maFrom = new MailAddress("sender_email@domain.tld", "Sender's Name", Encoding.UTF8),
    MailAddress maTo = new MailAddress("recipient_email@domain2.tld", "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 6 and Anti-Viruses. These creepy things tend 5 to block applications sending email. I use 4 McAfee Enterprise and I have to add the 3 executable name (like Bazar.* for both Bazar.exe 2 and Bazar.vshost.exe) to be able to send 1 emails...

Score: 3

change the port to 465

0

Score: 3

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

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

Score: 0

My code is connecting to smtp.google.com 4 using TLS on Port=587 (SSL should be port 3 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 2 these important prerequisites on your G 1 SUITE account

More Related questions