November 18, 2020

How to send Email programmatically using logged in Outlook instance using .Net

  November 18, 2020

Let's say we are in between a test script and if we want to send out an email as the next step.

Microsoft.Office.Interop.Outlook gives us the option to do this.

Using this NuGet package we can send an email with the already logged in credentials in outlook.

When using this we don't need to mention our email/password in the code.

Using this we can:

  • Sent text email
  • Sent email with HTML body
  • Attach Files
  • Customize subject 
  • Mention recipients in To, CC, and BCC
Literally, we can do anything that we do in outlook application, but in a programmatical way.

Below given is an example of such a code, This can be modified according to our needs.

using Outlook = Microsoft.Office.Interop.Outlook;
public void SendEmail(string mailToRecipients, string mailCCRecipients, string subjectLine,[Optional] string attachments,string HTMLBody)
{
	//// Create the Outlook application object.
	Outlook.Application oApp = new Outlook.Application();

	////Create the new message.
	Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

	//Add a recipient.
	// TODO: Change the following recipient where appropriate.
	Outlook.Recipients oRecips = oMsg.Recipients;
	List<string> oTORecip = new List<string>();
	List<string> oCCRecip = new List<string>();

	var ToRecip = mailToRecipients.Split(',');
	var CCRecip = mailCCRecipients.Split(',');
	

	foreach (string ToRecipient in ToRecip)
	{
		oTORecip.Add(ToRecipient);
	}

	foreach (string CCRecipient in CCRecip)
	{
		oCCRecip.Add(CCRecipient);
	}

	foreach (string to in oTORecip)
	{
		Outlook.Recipient oTORecipt = oRecips.Add(to);
		oTORecipt.Type = (int)Outlook.OlMailRecipientType.olTo;
		oTORecipt.Resolve();
	}

	foreach (string cc in oCCRecip)
	{
		Outlook.Recipient oCCRecipt = oRecips.Add(cc);
		oCCRecipt.Type = (int)Outlook.OlMailRecipientType.olCC;
		oCCRecipt.Resolve();
	}

	//Set the basic properties.
	oMsg.Subject = subjectLine;
	if (attachments.Length > 0)
	{

		//Add an attachments.
		string sDisplayName = "MyAttachment";
		int iPosition = 1;
		int iAttachType = (int)Outlook.OlAttachmentType.olByValue;

		var Sendattachments = attachments.Split(',');

		foreach(var attachment in Sendattachments)
		{
			Outlook.Attachment oAttach = oMsg.Attachments.Add(attachment, iAttachType, iPosition, sDisplayName);
		}
   
	}

	if(HTMLBody.Length>0)
	{
		oMsg.HTMLBody = HTMLBody;
	}

	//Send the message.
	oMsg.Save();
	oMsg.Send();

	//Explicitly release objects.
	oTORecip = null;
	oCCRecip = null;
	oMsg = null;
	oApp = null;

}
logoblog

Thanks for reading How to send Email programmatically using logged in Outlook instance using .Net

Previous
« Prev Post

No comments:

Post a Comment

Bookmark this website for more workarounds and issue fixes.

Verify Zip file contents without extracting using C# + Selenium

While doing automation testing, we may get a scenario where we need to download a zip file and to validate the contents inside it. One way t...