Created
August 16, 2017 14:55
-
-
Save Max-Edwards/a27a9f14539a854239e751a4ee9754e6 to your computer and use it in GitHub Desktop.
Mailkit, Email Send.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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