c# send email mailkit code example
Example 1: c# send email
using System;
using System.Windows.Forms;
using System.Net.Mail;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("[email protected]");
mail.To.Add("to_address");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
Example 2: mailkit csharp send mail
using System;
using MailKit.Net.Smtp;
using MailKit;
using MimeKit;
namespace TestClient {
class Program
{
public static void Main (string[] args)
{
var message = new MimeMessage ();
message.From.Add (new MailboxAddress ("Joey Tribbiani", "[email protected]"));
message.To.Add (new MailboxAddress ("Mrs. Chanandler Bong", "[email protected]"));
message.Subject = "How you doin'?";
message.Body = new TextPart ("plain") {
Text = @"Hey Chandler,
I just wanted to let you know that Monica and I were going to go play some paintball, you in?
-- Joey"
};
using (var client = new SmtpClient ()) {
client.Connect ("smtp.friends.com", 587, false);
client.Authenticate ("joey", "password");
client.Send (message);
client.Disconnect (true);
}
}
}
}
Example 3: c# send email
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
...
try
{
SmtpClient mySmtpClient = new SmtpClient("my.smtp.exampleserver.net");
mySmtpClient.UseDefaultCredentials = false;
System.Net.NetworkCredential basicAuthenticationInfo = new
System.Net.NetworkCredential("username", "password");
mySmtpClient.Credentials = basicAuthenticationInfo;
MailAddress from = new MailAddress("[email protected]", "TestFromName");
MailAddress to = new MailAddress("[email protected]", "TestToName");
MailMessage myMail = new System.Net.Mail.MailMessage(from, to);
MailAddress replyTo = new MailAddress("[email protected]");
myMail.ReplyToList.Add(replyTo);
myMail.Subject = "Test message";
myMail.SubjectEncoding = System.Text.Encoding.UTF8;
myMail.Body = "<b>Test Mail</b><br>using <b>HTML</b>.";
myMail.BodyEncoding = System.Text.Encoding.UTF8;
myMail.IsBodyHtml = true;
mySmtpClient.Send(myMail);
}
catch (SmtpException ex)
{
throw new ApplicationException
("SmtpException has occured: " + ex.Message);
}
catch (Exception ex)
{
throw ex;
}