Skip to content

Instantly share code, notes, and snippets.

@Max-Edwards
Created August 16, 2017 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Max-Edwards/a27a9f14539a854239e751a4ee9754e6 to your computer and use it in GitHub Desktop.
Save Max-Edwards/a27a9f14539a854239e751a4ee9754e6 to your computer and use it in GitHub Desktop.
Mailkit, Email Send.
public static async Task SendMail()
{
try
{
//From Address
string FromAddress = "testingthetested@gmail.com";
string FromAdressTitle = "Email from ASP.NET Core 1.1";
//To Address
string ToAddress = "testingthetested@gmail.com";
string ToAdressTitle = "Microsoft ASP.NET Core";
string Subject = "Hello World - Sending email using ASP.NET Core 1.1";
string BodyContent = "ASP.NET Core was previously called ASP.NET 5. It was renamed in January 2016. It supports cross-platform frameworks ( Windows, Linux, Mac ) for building modern cloud-based internet-connected applications like IOT, web apps, and mobile back-end.";
//Smtp Server
string SmtpServer = "localhost";
//Smtp Port Number
int SmtpPortNumber = 25;
var mimeMessage = new MimeMessage();
mimeMessage.From.Add(new MailboxAddress(FromAdressTitle, FromAddress));
mimeMessage.To.Add(new MailboxAddress(ToAdressTitle, ToAddress));
mimeMessage.Subject = Subject;
mimeMessage.Body = new TextPart("plain")
{
Text = BodyContent
};
using (var client = new SmtpClient())
{
client.Connect(SmtpServer, SmtpPortNumber, false);
// Note: only needed if the SMTP server requires authentication
// Error 5.5.1 Authentication
// client.Authenticate("From Address Email", "Password");
await client.SendAsync(mimeMessage);
Console.WriteLine("The mail has been sent successfully !!");
Console.ReadLine();
client.Disconnect(true);
}
}
catch (Exception ex)
{
throw ex;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment