Sending email from gmail smtp server

You can do smtp server setting in either web.config or backend code.

Web.config

<mailSettings>
      <smtp>
            <network host="smtp.gmail.com" userName="your@gmail.com" password="yourpassword" port="587" enableSsl="true"/>
      </smtp>
    </mailSettings>

C#,

           SmtpClient smtp = new SmtpClient();
            var fromAddress = new MailAddress"your@gmail.com", "Your Name");
            const string fromPassword = "yourpassword";            
smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
            };

It is very handy if you don’t have a smtp server setup.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s