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