C# email sending over Gmail (SSL)

C# email sending over Gmail (SSL)

Sedat Ozdemir Email Sending.png
Sedat Ozdemir Email Sending.png

Hello guys, I have a function about Gmail based mail sender with SSL encryption. You can use in your project for email sending.

What is the SMTP Server?

What happens when you send out an email? The process of email delivery is actually quite similar to classical mail: an organized system takes care of your envelope and through a series of steps it drops it off to your recipient. In this process, the SMTP server is simply a computer running SMTP, and which acts more or less like the postman. Once the messages have been picked up they are sent to this server, which takes care of concretely delivering emails to their recipients.

The Code

static void EmailSender(string fromAddress, string fromPassword, string fromName, string toAddress, string toName, string Subject, string Body) {
  var fromAddressVariable = new MailAddress(fromAddress, fromName);
  var toAddressVariable = new MailAddress(toAddress, toName);
  var smtp = new SmtpClient {
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddressVariable.Address, fromPassword)
  };
  using(var message = new MailMessage(fromAddress, toAddress) {
    Subject = Subject,
    Body = Body
  }) {
    try {
      smtp.Send(message);
      Console.WriteLine("Mail successfully sent.");
    } catch(Exception e) {
      Console.Write("Error : " + e.Message.ToString());
    }
  }
}
static void Main(string[] args) {
  EmailSender("your email address here", "your email password here", "your email name here", "to email address here", "to name here", "email subject here", "email body here");
  Console.ReadKey();
}

See other post

Tarafından yazıldı
Sedat Ozdemir
Bir yorum bırak

1 yorum

Teşekkürler!

Bloğumu ziyaret ettiğiniz ve yazılarımı okuduğunuz için teşekkürler!