Q: Sending email in .NET through Gmail

D: Instead of relying on my host to send email, I was thinking of sending the messages though my Gmail account. The emails are personalized emails to the bands I play on my show. Is it possible to do?

Test Case #10


File ID: #32336-3-cc


MailAddress fromAddress;
MailAddress toAddress;
SmtpClient smtp;
public Email(string recipient, string subject, string body)
{
    fromAddress = new MailAddress("eamc.bloodbank@gmail.com");
    toAddress = new MailAddress(recipient);
    smtp = new SmtpClient{
        Host = "smtp.gmail.com",
        Port = 587,
        EnableSsl = true,
        DeliveryMethod = SmtpDeliveryMethod.Network,
        UseDefaultCredentials = false,
        Credentials = new NetworkCredential(fromAddress.Address, "bloodbus")
    };
    using (var message = new MailMessage(fromAddress, toAddress)
    {
        Subject = subject,
                  Body = body
                     })
    {
        smtp.Send(message);
    }
}

  1. Interesting note: If you swap 'UseDefaultCredentials = false,' and 'Credentials = ...' it will not authenticate.
  2. I was having a hard time getting this working even with trying various tweaks. As suggested on a related post, I found that it was actually my antivirus that was preventing emails from being successfully sent. The antivirus in question is McAffee, and its "Access Protection" has a "Antivirus Standard Protection" category that has a "Prevent mass mailing worms from sending email" rule. Tweaking / disabling that rule got this code working for me!
  3. There are no problems with SPF using this method. Every email client can be configured to do exactly this. You just may get problems if you use your own server (i.e. something else than `smtp.gmail.com`) with `something@gmail.com` as sender. Btw: `smtp.gmail.com` automatically overwrites the sender address if it's not yours.

Comments Quality
Accurate?:
Precise?:
Concise?:
Useful?: