How to Email sending in .NET through Gmail

View: 910    Dowload: 0   Comment: 0   Post by: hanhga   Category: Visual studio & .NET   Fields: Other

Use System.Net.Mail, not the deprecated System.Web.Mail in C#.


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


Above example will work fine if you change your gmail account setting by visiting https://support.google.com/accounts/answer/6010255?hl=en-GB

Google has recently changed it’s security policy for less secure apps.

How to Email sending in .NET through Gmail

Use System.Net.Mail, not the deprecated System.Web.Mail in C#.

Posted on 22-08-2016 


To comment you must be logged in members.

Files with category

File suggestion for you
File top downloads
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd