I get a “Microsoft.Office.Interop.Outlook.ApplicationClass” error while trying to send email using outlook in Windows application.
Microsoft.Office.Interop.Outlook dll is not supporting MS outlook 2021
Please advise is there any alternative. Thank you.
Error message:
Unable to cast COM object of type ‘Microsoft.Office.Interop.Outlook.ApplicationClass’ to interface type ‘Microsoft.Office.Interop.Outlook._Application’.
Sample code:
using Outlook = Microsoft.Office.Interop.Outlook;
public Boolean SendEmailThroughOUTLOOK()
{
Outlook.Application oApp = new Outlook.Application();
Outlook.MailItem oMsg = Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
try
{
if (Directory.GetFiles(WIPFolder).Length != 0)
{
string[] files = Directory.GetFiles(WIPFolder);
String sDisplayName = "FeedbackForm";
int iPosition = (int)oMsg.Body.Length + 1;
int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
// now attach the file
foreach (string SourceFile in files)
{
Outlook.Attachment oAttach = oMsg.Attachments.Add(SourceFile, iAttachType, iPosition, sDisplayName);
}
oRecip.Resolve();
oMsg.Send(); // Here I am getting the error
return true;
}
}
catch (Exception ex)
{
return false;
}
finally
{
oRecip = null;
oRecips = null;
oMsg = null;
oApp = null;
}
return false;
}
Looking for alternative to Microsoft.office.interop.oulook which can support Office 2021.
Nazia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3