c# Error when trying to send email

D

Devhead

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 = "(e-mail address removed)";

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);

}
 
J

Jim Vierra

Start here - you have a ref but no objects. (yours is oMissing)

object template = System.Reflection.Missing.Value;
object newTemplate = System.Reflection.Missing.Value;
 
D

Devhead

oMissing is already defined. I just forgot to include that. the error happens at Outlook.MailItemClass mItem =

(Outlook.MailItemClass)oDoc.MailEnvelope.Item. I put a watch on this line and it reported that oDoc.MailEnvelope.Item 'did not exist'.

Start here - you have a ref but no objects. (yours is oMissing)

object template = System.Reflection.Missing.Value;
object newTemplate = System.Reflection.Missing.Value;
 
J

Jim Vierra

I have the example from ms and have found it is loaded with errors. I now have it compiling clean and running with the excption of hooking up the quit event. If you want I will send you what I have,

--
Jim Vierra
oMissing is already defined. I just forgot to include that. the error happens at Outlook.MailItemClass mItem =

(Outlook.MailItemClass)oDoc.MailEnvelope.Item. I put a watch on this line and it reported that oDoc.MailEnvelope.Item 'did not exist'.

Start here - you have a ref but no objects. (yours is oMissing)

object template = System.Reflection.Missing.Value;
object newTemplate = System.Reflection.Missing.Value;
 
J

Jim Vierra

Got it. I'll clean it up and post tomorrow. The code will work with 2003 of Office, It catches the send event as 2003 doesn't support item events like close. I still have to get the quit methood to work with the interop. It doesn't like the syntax in the KB article. Intellisense says it's good but the compiler complains about the signature. The sets to the mail fields do not rransfer but I think it's because I don't have correct access to Outlook. Word and Outlook start correctly and display the correct form.

Your code must be runing from a script??? The convention you are using is not WIndows FOrms from VS.NET or is it an older copy of VS. I am using 2005 with NET 1.0 Interop. The complier is better and the Intellisense is smarter that VS.NET.

I am doing this because I have been meaning to test automation with VS 2005 but have been ignoring it for a while. This was an excuse to get my hands dirty. Debugging someone elses code is a good way to learn a new API so when I saw that there were deficciencies in the example (half of the KB examples have never been tested) I decided it was trivial enough to go after. I learned a bunch in about 30 minutes with the compiler. It also feels good to get away from VB/VBA for awhile. Next I'll have to convert it into C++. That will really excercise my knowledge of teh Object interface as it's more primitive. Can get right at IUnknown etc.

Tomorrow. - Here's a sample - it compiles it you comment out the wApp.Quit

private void button3_Click(object sender, EventArgs e)
{
//Initialize the envelope values.
string strTo = "(e-mail address removed)";
string strBCC = "(e-mail address removed)";
string strCC = "(e-mail address removed)";
string strSubject = "Outlook Automation";
string strBody = "c:\\HTMLPage1.htm";
object template = System.Reflection.Missing.Value;

//Automate the Word document.
wApp = new Word.Application();
wApp.Visible = false;
object newTemplate = System.Reflection.Missing.Value;
object documentType = Word.WdNewDocumentType.wdNewEmailMessage;
object visible = false;
wApp.Visible = false;

Word.Document doc = wApp.Documents.Add(
ref template,
ref newTemplate,
ref documentType,
ref visible);

//Automate the Outlook mail item.
Outlook.Application oApp;
oApp = new Outlook.Application();

Outlook.MailItem mItem;
mItem = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
mItem.To = strTo;
mItem.BCC = strBCC;
mItem.CC = strCC;
mItem.Subject = strSubject;
mItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
mItem.HTMLBody = GetString(strBody);

oApp.ItemSend +=new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(oApp_ItemSend);
wApp.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.
void oApp_ItemSend(object Item, ref bool Cancel)
{
oMiss = System.Reflection.Missing.Value;
wApp.Quit(ref oMiss, ref oMiss, ref oMiss);
}

--
Jim Vierra
I have the example from ms and have found it is loaded with errors. I now have it compiling clean and running with the excption of hooking up the quit event. If you want I will send you what I have,

--
Jim Vierra
oMissing is already defined. I just forgot to include that. the error happens at Outlook.MailItemClass mItem =
(Outlook.MailItemClass)oDoc.MailEnvelope.Item. I put a watch on this line and it reported that oDoc.MailEnvelope.Item 'did not exist'.
Start here - you have a ref but no objects. (yours is oMissing)

object template = System.Reflection.Missing.Value;
object newTemplate = System.Reflection.Missing.Value;
 
D

Devhead

it will work since you now have an Outlook application object and associated MailItem object defined. i was trying to launch outlook interface from within Word. the Word automation part is not needed in the code you provided. i.e., it will work without the word automation code below. thanks.
Got it. I'll clean it up and post tomorrow. The code will work with 2003 of Office, It catches the send event as 2003 doesn't support item events like close. I still have to get the quit methood to work with the interop. It doesn't like the syntax in the KB article. Intellisense says it's good but the compiler complains about the signature. The sets to the mail fields do not rransfer but I think it's because I don't have correct access to Outlook. Word and Outlook start correctly and display the correct form.

Your code must be runing from a script??? The convention you are using is not WIndows FOrms from VS.NET or is it an older copy of VS. I am using 2005 with NET 1.0 Interop. The complier is better and the Intellisense is smarter that VS.NET.

I am doing this because I have been meaning to test automation with VS 2005 but have been ignoring it for a while. This was an excuse to get my hands dirty. Debugging someone elses code is a good way to learn a new API so when I saw that there were deficciencies in the example (half of the KB examples have never been tested) I decided it was trivial enough to go after. I learned a bunch in about 30 minutes with the compiler. It also feels good to get away from VB/VBA for awhile. Next I'll have to convert it into C++. That will really excercise my knowledge of teh Object interface as it's more primitive. Can get right at IUnknown etc.

Tomorrow. - Here's a sample - it compiles it you comment out the wApp.Quit

private void button3_Click(object sender, EventArgs e)
{
//Initialize the envelope values.
string strTo = "(e-mail address removed)";
string strBCC = "(e-mail address removed)";
string strCC = "(e-mail address removed)";
string strSubject = "Outlook Automation";
string strBody = "c:\\HTMLPage1.htm";
object template = System.Reflection.Missing.Value;

//Automate the Word document.
wApp = new Word.Application();
wApp.Visible = false;
object newTemplate = System.Reflection.Missing.Value;
object documentType = Word.WdNewDocumentType.wdNewEmailMessage;
object visible = false;
wApp.Visible = false;

Word.Document doc = wApp.Documents.Add(
ref template,
ref newTemplate,
ref documentType,
ref visible);

//Automate the Outlook mail item.
Outlook.Application oApp;
oApp = new Outlook.Application();

Outlook.MailItem mItem;
mItem = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
mItem.To = strTo;
mItem.BCC = strBCC;
mItem.CC = strCC;
mItem.Subject = strSubject;
mItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
mItem.HTMLBody = GetString(strBody);

oApp.ItemSend +=new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(oApp_ItemSend);
wApp.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.
void oApp_ItemSend(object Item, ref bool Cancel)
{
oMiss = System.Reflection.Missing.Value;
wApp.Quit(ref oMiss, ref oMiss, ref oMiss);
}

--
Jim Vierra
I have the example from ms and have found it is loaded with errors. I now have it compiling clean and running with the excption of hooking up the quit event. If you want I will send you what I have,

--
Jim Vierra
oMissing is already defined. I just forgot to include that. the error happens at Outlook.MailItemClass mItem =
(Outlook.MailItemClass)oDoc.MailEnvelope.Item. I put a watch on this line and it reported that oDoc.MailEnvelope.Item 'did not exist'.
Start here - you have a ref but no objects. (yours is oMissing)

object template = System.Reflection.Missing.Value;
object newTemplate = System.Reflection.Missing.Value;
 
J

Jim Vierra

Just trying to fix the sample. Didn't know what you were trying to do. I found that I don't need the Quit but had to move the garbage collector to the "Send" event for the oApp object. This causes Outlook to exit if it is not being used for anything else. Comes along with the new 11.0 version that way. May be other new rules that I haven't discovered but I like the way 11 works from C# code and the CLR. Very smooth.

I don't know what the initializations were trying to do - to me, they look wrong in any version.

--
Jim Vierra
it will work since you now have an Outlook application object and associated MailItem object defined. i was trying to launch outlook interface from within Word. the Word automation part is not needed in the code you provided. i.e., it will work without the word automation code below. thanks.
Got it. I'll clean it up and post tomorrow. The code will work with 2003 of Office, It catches the send event as 2003 doesn't support item events like close. I still have to get the quit methood to work with the interop. It doesn't like the syntax in the KB article. Intellisense says it's good but the compiler complains about the signature. The sets to the mail fields do not rransfer but I think it's because I don't have correct access to Outlook. Word and Outlook start correctly and display the correct form.

Your code must be runing from a script??? The convention you are using is not WIndows FOrms from VS.NET or is it an older copy of VS. I am using 2005 with NET 1.0 Interop. The complier is better and the Intellisense is smarter that VS.NET.

I am doing this because I have been meaning to test automation with VS 2005 but have been ignoring it for a while. This was an excuse to get my hands dirty. Debugging someone elses code is a good way to learn a new API so when I saw that there were deficciencies in the example (half of the KB examples have never been tested) I decided it was trivial enough to go after. I learned a bunch in about 30 minutes with the compiler. It also feels good to get away from VB/VBA for awhile. Next I'll have to convert it into C++. That will really excercise my knowledge of teh Object interface as it's more primitive. Can get right at IUnknown etc.

Tomorrow. - Here's a sample - it compiles it you comment out the wApp.Quit

private void button3_Click(object sender, EventArgs e)
{
//Initialize the envelope values.
string strTo = "(e-mail address removed)";
string strBCC = "(e-mail address removed)";
string strCC = "(e-mail address removed)";
string strSubject = "Outlook Automation";
string strBody = "c:\\HTMLPage1.htm";
object template = System.Reflection.Missing.Value;

//Automate the Word document.
wApp = new Word.Application();
wApp.Visible = false;
object newTemplate = System.Reflection.Missing.Value;
object documentType = Word.WdNewDocumentType.wdNewEmailMessage;
object visible = false;
wApp.Visible = false;

Word.Document doc = wApp.Documents.Add(
ref template,
ref newTemplate,
ref documentType,
ref visible);

//Automate the Outlook mail item.
Outlook.Application oApp;
oApp = new Outlook.Application();

Outlook.MailItem mItem;
mItem = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
mItem.To = strTo;
mItem.BCC = strBCC;
mItem.CC = strCC;
mItem.Subject = strSubject;
mItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
mItem.HTMLBody = GetString(strBody);

oApp.ItemSend +=new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(oApp_ItemSend);
wApp.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.
void oApp_ItemSend(object Item, ref bool Cancel)
{
oMiss = System.Reflection.Missing.Value;
wApp.Quit(ref oMiss, ref oMiss, ref oMiss);
}

--
Jim Vierra
I have the example from ms and have found it is loaded with errors. I now have it compiling clean and running with the excption of hooking up the quit event. If you want I will send you what I have,

--
Jim Vierra
oMissing is already defined. I just forgot to include that. the error happens at Outlook.MailItemClass mItem =
(Outlook.MailItemClass)oDoc.MailEnvelope.Item. I put a watch on this line and it reported that oDoc.MailEnvelope.Item 'did not exist'.
Start here - you have a ref but no objects. (yours is oMissing)

object template = System.Reflection.Missing.Value;
object newTemplate = System.Reflection.Missing.Value;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top