Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Send email using Outlook error “Object reference not set to an instance of an object” from C# application

43 views
Skip to first unread message

lee...@gmail.com

unread,
Mar 6, 2017, 8:32:56 AM3/6/17
to
I am writing a C# application and part of it is to use Outlook to send out an email. It is working fine when Outlook is running, however, I get an error window "Object reference not set to an instance of an object" when Outlook is not running. The error happens at line email.Send(). Here is the code

Outlook.Application app = null;
try
{
if (System.Diagnostics.Process.GetProcessesByName("OUTLOOK").Count() > 0)
{
app = System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
}
else
{
app = new Outlook.Application();
Outlook.NameSpace ns = app.GetNamespace("MAPI");
ns.Logon(Type.Missing, Type.Missing, false, false);
}
Outlook.MailItem eMail = app.CreateItem(Outlook.OlItemType.olMailItem);
eMail.Subject = "Test";
eMail.To = "a...@xxx.com";
eMail.Body = string.Empty;
eMail.Send();
}
catch (Exception ex)
{
throw ex;
}

Any suggestion is appreciated.
0 new messages