Sending an email through C# code is easy, but if you want to know whether the sent email is delivered and read by the intended recipient. When we send e-mail through code we can request a delivery notification and read receipt from the receiver, similar to the way we do in outlook.
Please find below code which is then processed by the SMTP server and the sender of the mail gets the intended response.
You can also check "Send Email using Gmail from C#" and "Send Email with Attachments"
Please find below C# source code, that shows how to send an email by requesting delivery/read receipt from C# using MailMessage class.
I have created sample windows application and added button = btnSendEmailByRequestingDeliveryAndReadReceipt, in this button click event the below code added.
using System;
using System.IO;
using System.Net;
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 btnSendEmailByRequestingDeliveryAndReadReceipt 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 btnSendEmailByRequestingDeliveryAndReadReceipt_Click(object sender, EventArgs e)
{
try
{
//Create the object of MailMessage
MailMessage mailMessage = new MailMessage();
//From Email Id & Display name
mailMessage.From = new MailAddress("FromEmail@domain.com", "FromDisplayName");
//Subject of Email
mailMessage.Subject = "Test Email Requesting delivery/read receipt";
//body or message of Email
mailMessage.Body = "This is the test email body text by requesting delivery and read receipt";
mailMessage.IsBodyHtml = true;
//if you want to send High importance email
mailMessage.Priority = MailPriority.High;
//You can add either multiple To,CC,BCC or single emails
mailMessage.To.Add(new MailAddress("ToEmailId1@domain.com;ToEmailId2@domain.com;ToEmailId3@domain.com;")); //adding multiple TO Email Id
mailMessage.CC.Add(new MailAddress("CCEmailId1@domain.com;CCEmailId2@domain.com;CCEmailId3@domain.com;")); //Adding Multiple CC email Id
mailMessage.Bcc.Add(new MailAddress("BCCEmailId1@domain.com;BCCEmailId2@domain.com;BCCEmailId3@domain.com;")); //Adding Multiple BCC email Id
//This method has been deprecated.
//mail.Headers.Add("Return-Receipt-To", "prashantmx@xyz.com");
//Use this if you need an delivery notification of an email. DeliveryNotificationOption is an enumeration
//and can be used to set the delivery notification on the following options:
//1. Delay
//2. Never
//3. None
//4. OnFailure
//5. OnSuccess
//You can use also use OnFailure enum with OnSuccess enum. If in case the e-mail
//fails to delivered you'll get notification for both the cases
mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
//Add "Disposition-Notification-To" for Read receipt
mailMessage.Headers.Add("Disposition-Notification-To", "");
//Assign the SMTP address and port
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.domain.net";
smtp.Port = 25;
smtp.EnableSsl = false; //If required make it true
//Network and security related credentials
NetworkCredential networkCredential = new NetworkCredential();
networkCredential.UserName = "user name (email id)";
networkCredential.Password = "password";
networkCredential.Domain = "Domain Name";
smtp.Credentials = networkCredential;
//Finally send Email
smtp.Send(mailMessage);
MessageBox.Show("The email has been sent successfully.");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}