Sending email using Gmail SMTP in asp.net mvc application?
💻 coding

Sending email using Gmail SMTP in asp.net mvc application?

1 min read 253 words
1 min read
ShareWhatsAppPost on X
  • 1You can send emails in ASP.NET MVC using Gmail's SMTP service with C#.
  • 2A GMailer class can be created to manage email properties and sending functionality.
  • 3Configuration settings for Gmail credentials and SMTP details can be stored in a config file.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"You can send emails in ASP.NET MVC using Gmail's SMTP service with C#."

Sending email using Gmail SMTP in asp.net mvc application?

You can use GMAIL SMTP service to send emails using c# .net MVC. You can specify the property constants in a config file for easy alteration.

Create Gmail Class comprises of all needed data type and member function as below.

public class GMailer
{
 public static string GmailUsername { get; set; }
 public static string GmailPassword { get; set; }
 public static string GmailHost { get; set; }
 public static int GmailPort { get; set; }
 public static bool GmailSSL { get; set; }

 public string ToEmail { get; set; }
 public string Subject { get; set; }
 public string Body { get; set; }
 public bool IsHtml { get; set; }

 static GMailer()
 {
 GmailHost = "smtp.gmail.com";
 GmailPort = 25; // Gmail can use ports 25, 465 & 587; but must be 25 for medium trust environment.
 GmailSSL = true;
 }

 public void Send()
 {
 SmtpClient smtp = new SmtpClient();
 smtp.Host = GmailHost;
 smtp.Port = GmailPort;
 smtp.EnableSsl = GmailSSL;
 smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
 smtp.UseDefaultCredentials = false;
 smtp.Credentials = new NetworkCredential(GmailUsername, GmailPassword);

 using (var message = new MailMessage(GmailUsername, ToEmail))
 {
 message.Subject = Subject;
 message.Body = Body;
 message.IsBodyHtml = IsHtml;
 smtp.Send(message);
 }
 }
}

Then just used the following code wherever you want to send the email to the required email account.

GMailer.GmailUsername = "youremailid@gmail.com";
 GMailer.GmailPassword = "YourPassword";

 GMailer mailer = new GMailer();
 mailer.ToEmail = "sumitchourasia91@gmail.com";
 mailer.Subject = "Verify your email id";
 mailer.Body = "Thanks for Registering your account";
 mailer.IsHtml = true;
 mailer.Send();

Hope this will help you. Mark as answer if this helps you.

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 27 August 2018 · 1 min read · 253 words

Part of AskGif Blog · coding

You might also like

Sending email using Gmail SMTP in asp.net mvc application? | AskGif Blog