How sent e-mail from ASP.NET site using GMail's SMTP server?

10,935

Solution 1

This could be a firewall issue. The firewall on the server your code is running on could be blocking traffic on TCP port 587. It is also possible that it is being blocked in the network infrastructure between your server and the Internet.

Solution 2

Here's a class I've used in the past:

namespace MyApp
{
    public class GMailer
    {
        public static string GmailUsername { get; set; }
        public static string GmailPassword { get; set; }
        public static string GmailHost { get; set; }
        public static int GmailPort { get; set; }
        public static bool GmailSSL { get; set; }

        public string ToEmail { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
        public bool IsHtml { get; set; }

        static GMailer()
        {
            GmailHost = "smtp.gmail.com";
            GmailPort = 25; // Gmail can use ports 25, 465 & 587; but must be 25 for medium trust environment.
            GmailSSL = true;
        }

        public void Send()
        {
            SmtpClient smtp = new SmtpClient();
            smtp.Host = GmailHost;
            smtp.Port = GmailPort;
            smtp.EnableSsl = GmailSSL;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential(GmailUsername, GmailPassword);

            using (var message = new MailMessage(GmailUsername, ToEmail))
            {
                message.Subject = Subject;
                message.Body = Body;
                message.IsBodyHtml = IsHtml;
                smtp.Send(message);
            }
        }
    }
}

The class needs to be configured in your Application_Start event:

GMailer.GmailUsername = "[email protected]";
GMailer.GmailPassword = "password";

Usage:

GMailer mailer = new GMailer();
mailer.ToEmail = "[email protected]";
mailer.Subject = "Email Subject Line";
mailer.Body = "This is a test message";
mailer.IsHtml = false;
mailer.Send();
Share:
10,935
Rasto
Author by

Rasto

Entrepreneur, UX consultant, full-stack developer with strongest competency in Swift & iOS. Can get die-hard about top-notch UX and code quality. Diving deeper into React and getting excited about GraphQL. My pro history chapters are written in C#, Objective-C, Java, JS, TS, Flow, even Haskel, Prolog & Pascal. Sports and outdoor enthusiast. Love exploring cultures around the world. Found in mountains in the winter and at seaside during summer. Amater photographer. Always happy to learn and share what I learned e.g. externally giving lectures at my alma mater.

Updated on June 05, 2022

Comments

  • Rasto
    Rasto almost 2 years

    Possible Duplicate:
    Sending email through Gmail SMTP server with C#

    I would like my ASP.NET MVC application to send some standard letters to users of the web site. For testing purposes I have no local SMTP server and my provider has non that I know of. So I have to use public services like GMail's SMTP.

    How do I send e-mail using smtp.gmail.com and my GMail account? What exactly should I put to Web.config and whot to code provided my e-mail is [email protected] and my password is password?

    Thank you


    EDIT

    When I try following demo program:

    class Program {
        static void Main(string[] args) {
            var client = new SmtpClient("smtp.gmail.com", 587) {
                Credentials = new NetworkCredential("[email protected]", "puzzlehunters111"),
                EnableSsl = true
            };
            client.Send("[email protected]", "[email protected]", "test", "testbody");
            Console.WriteLine("Sent");
            Console.ReadLine();
        }
    }
    

    It fails with exception. Most inner exception carries this message:

    No connection could be made because the target machine actively refused it 209.85.227.109:587
    

    Also any all present answers (3 earliest) give me the same exception. What can I do with that?