Coder Perfect

Using Gmail to send email in.NET

Problem

Instead of relying on my server to send an email, I was considering using my Gmail account to send the messages. The emails are tailored to the musicians I feature on my show.

Is it possible to accomplish this?

Asked by Mike Wills

Solution #1

Be sure to use System.Net.Mail, not the deprecated System. Web.Mail. Doing SSL with System. Web.Mail is a gross mess of hacky extensions.

using System.Net;
using System.Net.Mail;

var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";

var smtp = new SmtpClient
{
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body
})
{
    smtp.Send(message);
}

Also, check at the Signing in to Google > 2-Step Verification setting on the Google Account > Security page.

Answered by Domenic

Solution #2

The answer given above is incorrect. DeliveryMethod = SmtpDeliveryMethod must be configured. If you don’t connect to the internet, you’ll get a “client was not authenticated” problem. It’s also a good idea to include a timeout.

Revised code:

using System.Net.Mail;
using System.Net;

var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@yahoo.com", "To Name");
const string fromPassword = "password";
const string subject = "test";
const string body = "Hey now!!";

var smtp = new SmtpClient
{
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
    Timeout = 20000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body
})
{
    smtp.Send(message);
}

Answered by Donny V.

Solution #3

To make the other responses function “from a server,” first enable access to less secure apps in your gmail account.

It appears that Google’s security policy has just altered. Until you modify your account settings as mentioned here, the top-rated response will no longer work: https://support.google.com/accounts/answer/6010255?hl=en-GB

Google has altered the position of the setting location again again as of March 2016!

Answered by BCS Software

Solution #4

To send an email with an attachment, use this method. Simple and concise.

source: http://coding-issues.blogspot.in/2012/11/sending-email-with-attachments-from-c.html

using System.Net;
using System.Net.Mail;

public void email_send()
{
    MailMessage mail = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
    mail.From = new MailAddress("your mail@gmail.com");
    mail.To.Add("to_mail@gmail.com");
    mail.Subject = "Test Mail - 1";
    mail.Body = "mail with attachment";

    System.Net.Mail.Attachment attachment;
    attachment = new System.Net.Mail.Attachment("c:/textfile.txt");
    mail.Attachments.Add(attachment);

    SmtpServer.Port = 587;
    SmtpServer.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");
    SmtpServer.EnableSsl = true;

    SmtpServer.Send(mail);

}

Answered by Ranadheer Reddy

Solution #5

Sign-in attempts from some apps or devices that do not follow modern security standards may be blocked by Google. Blocking certain apps and devices makes your account safer because they are easier to break into.

The following are some examples of apps that do not comply with the most recent security standards:

As a result, on your Google account, you must enable Less Secure Sign-In.

Go to: after you’ve signed into your Google account

https://myaccount.google.com/lesssecureapps or https://www.google.com/settings/security/lesssecureapps

You can use the following code in C#:

using (MailMessage mail = new MailMessage())
{
    mail.From = new MailAddress("email@gmail.com");
    mail.To.Add("somebody@domain.com");
    mail.Subject = "Hello World";
    mail.Body = "<h1>Hello</h1>";
    mail.IsBodyHtml = true;
    mail.Attachments.Add(new Attachment("C:\\file.zip"));

    using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
    {
        smtp.Credentials = new NetworkCredential("email@gmail.com", "password");
        smtp.EnableSsl = true;
        smtp.Send(mail);
    }
}

Answered by mjb

Post is based on https://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail