You can send email from C# using MailMessage class. In this example i have
explained how to send email using Gmail SMTP server.
You can also check "
Send Email with Attachments"
and "
Send Email by requesting delivery/read receipt from C#"
Please find below C# source code, that shows how to send an email from a Gmail address using SMTP server. The Gmail SMTP server name is smtp.gmail.com and the port using send mail is 587 and also using NetworkCredential for password based authentication.
I have created sample windows application and added button = btnSendEmailUsingGmail, in this button click event the below code added.
using System;
using System.Net.Mail;
using System.Windows.Forms;
namespace SampleWindowsApp
{
/// <summary>
/// Send Email Form
/// </summary>
/// <seealso cref="System.Windows.Forms.Form" />
public partial class SendEmailForm : Form
{
/// <summary>
/// Initializes a new instance of the <see cref="SendEmailForm"/> class.
/// </summary>
public SendEmailForm()
{
InitializeComponent();
}
/// <summary>
/// Handles the Click event of the btnSendEmailUsingGmail control.
/// Author = Himasagar Kutikuppala
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void btnSendEmailUsingGmail_Click(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
//Change below gmail address & To email address
mail.From = new MailAddress("Your_Gmail_Address@gmail.com");
mail.To.Add("To_Email_Address");
mail.Subject = "Test Mail From Gmail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
//Change your gmail user id & password
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("The email has been sent successfully.");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}