By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,846 Members | 2,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,846 IT Pros & Developers. It's quick & easy.

c# Error when trying to send email

P: n/a
i need to present user with an Outlook mail item. the following code is a
modication to the code found
here(http://support.microsoft.com/?kbid=819398). However, when the line is
question is run(Outlook.MailItemClass mItem =
(Outlook.MailItemClass)oDoc.MailEnvelope.Item;), it generated an exception
of type System.ExecuteOnEngineException with the following message:

"Object reference not set to an instance of an object"

private static Word._Application oWord = null;

public static void SendEmail(ref Word._Application oWord)

{

object oDocType = Word.WdNewDocumentType.wdNewEmailMessage;

oDoc = oWord.Documents.Add(

ref oMissing,

ref oMissing,

ref oDocType,

ref oFalse);

Outlook.MailItemClass mItem = (Outlook.MailItemClass)oDoc.MailEnvelope.Item;

mItem.To = "so******@hotmail.com";

mItem.BCC = "none";

mItem.CC = "none";

mItem.Subject = "Test message";

mItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;

mItem.HTMLBody = "something going on here";

mItem.ItemEvents_Event_Close += new
Outlook.ItemEvents_CloseEventHandler(oWord_Close);

oWord.Visible = true;

// Loop until there are no more references to release.

while (Marshal.ReleaseComObject(mItem) > 0);

mItem = null;

// Invoke the .NET garbage collector.

GC.Collect();

GC.WaitForPendingFinalizers();

}

// Close the Word application after the message has been sent.

private static void oWord_Close(ref bool e)

{

CloseWordApp(ref oWord);

}
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Are you wanting to send e-mail or is this just the beginning, and you plan
to do more with the Outlook object model? If you are just sending e-mail,
just use SMTP! It's a lot easier to set up and use, and you won't need to
mess with Outlook to do it. Nothing wrong with outlook. It's an excellent
tool. However, if you just want to send a little e-mail, it's often
overkill.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Devhead" <de******@bellsouth.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
i need to present user with an Outlook mail item. the following code is a
modication to the code found
here(http://support.microsoft.com/?kbid=819398). However, when the line is
question is run(Outlook.MailItemClass mItem =
(Outlook.MailItemClass)oDoc.MailEnvelope.Item;), it generated an exception
of type System.ExecuteOnEngineException with the following message:

"Object reference not set to an instance of an object"

private static Word._Application oWord = null;

public static void SendEmail(ref Word._Application oWord)

{

object oDocType = Word.WdNewDocumentType.wdNewEmailMessage;

oDoc = oWord.Documents.Add(

ref oMissing,

ref oMissing,

ref oDocType,

ref oFalse);

Outlook.MailItemClass mItem = (Outlook.MailItemClass)oDoc.MailEnvelope.Item;
mItem.To = "so******@hotmail.com";

mItem.BCC = "none";

mItem.CC = "none";

mItem.Subject = "Test message";

mItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;

mItem.HTMLBody = "something going on here";

mItem.ItemEvents_Event_Close += new
Outlook.ItemEvents_CloseEventHandler(oWord_Close);

oWord.Visible = true;

// Loop until there are no more references to release.

while (Marshal.ReleaseComObject(mItem) > 0);

mItem = null;

// Invoke the .NET garbage collector.

GC.Collect();

GC.WaitForPendingFinalizers();

}

// Close the Word application after the message has been sent.

private static void oWord_Close(ref bool e)

{

CloseWordApp(ref oWord);

}

Nov 16 '05 #2

P: n/a
need to present Outlook user interface mainly to have a way to retrieve
contact list so user can select where the email is going.

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:4N********************@comcast.com...
Are you wanting to send e-mail or is this just the beginning, and you plan
to do more with the Outlook object model? If you are just sending e-mail,
just use SMTP! It's a lot easier to set up and use, and you won't need to
mess with Outlook to do it. Nothing wrong with outlook. It's an excellent
tool. However, if you just want to send a little e-mail, it's often
overkill.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Devhead" <de******@bellsouth.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
i need to present user with an Outlook mail item. the following code is a
modication to the code found
here(http://support.microsoft.com/?kbid=819398). However, when the line
is
question is run(Outlook.MailItemClass mItem =
(Outlook.MailItemClass)oDoc.MailEnvelope.Item;), it generated an
exception
of type System.ExecuteOnEngineException with the following message:

"Object reference not set to an instance of an object"

private static Word._Application oWord = null;

public static void SendEmail(ref Word._Application oWord)

{

object oDocType = Word.WdNewDocumentType.wdNewEmailMessage;

oDoc = oWord.Documents.Add(

ref oMissing,

ref oMissing,

ref oDocType,

ref oFalse);

Outlook.MailItemClass mItem =

(Outlook.MailItemClass)oDoc.MailEnvelope.Item;

mItem.To = "so******@hotmail.com";

mItem.BCC = "none";

mItem.CC = "none";

mItem.Subject = "Test message";

mItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;

mItem.HTMLBody = "something going on here";

mItem.ItemEvents_Event_Close += new
Outlook.ItemEvents_CloseEventHandler(oWord_Close);

oWord.Visible = true;

// Loop until there are no more references to release.

while (Marshal.ReleaseComObject(mItem) > 0);

mItem = null;

// Invoke the .NET garbage collector.

GC.Collect();

GC.WaitForPendingFinalizers();

}

// Close the Word application after the message has been sent.

private static void oWord_Close(ref bool e)

{

CloseWordApp(ref oWord);

}


Nov 16 '05 #3

P: n/a
Are you sure that the error is on the line

Outlook.MailItemClass mItem =
(Outlook.MailItemClass)oDoc.MailEnvelope.Item;

or is that just the line that is highlighted when the debugger signals
the error? Remember that for method calls, the debugger often indicates
the "current line" as being the line _after_ the call that is raising
the exception. (This has to do with the execution stack... the way that
compiled code runs inside the machine.)

If this is the case, then the problem may instead be the line,

oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oDocType,
ref oFalse);

which would make more sense, because I don't see you initializing
oMissing, oDocType, or oFalse anywhere. Could one of these be null?
These are "ref" arguments, not "out" arguments, so
oWord.Documents.Add() is expecting them to be set to something, and may
change them on return.

Nov 16 '05 #4

P: n/a
Nick, if you would be so kind, I have the same problem in my
application. All of the mail interfaces I saw in .NET, such as SMTP,
seemed designed to send an e-mail "silently" in the background with no
intervention from the user. I needed to show a standard "send mail"
dialog, with access to the address book and an option to set the
subject or type more into the message. In the end I resorted to
invoking Outlook.

Am I correct that SMTP is just for create-and-send behind the scenes,
or did I misunderstand the documentation?

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.