Sending mail from my gmail account through .Net

Below is sample to send mail from your gmail account.

We will use System.Net.Mail to send mail from gmail account.

using System.Net;
using System.Net.Mail;
var fromAddress = new MailAddress("fromaddress@gmail.com", "Your Name");
var toAddress = new MailAddress("toaddress@example.com", "Receipt Name");
const string fromPassword = "YourPassword";
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
})
{
    smtp.Send(message);
}

Posted in |

0 comments:

Post a Comment