C# and Outlook

J

Jeff

I am trying to send emails from my application (not using a plugin, that
will come later), I can create an email and add attachments. I can fill in
the body etc. Word works for the editor. So mostly what I want.
My issue is I would like to retain the signature block that exists when I
insert my body into the message.

Any help would be appreciated. Here is the code I am using:

//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 = "HTMLPage1.htm";

//Automate the Word document.
wApp = new Word.Application();
wApp.Visible = false;
object template = System.Reflection.Missing.Value;
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.
Microsoft.Office.Interop.Outlook.MailItem mItem =
(Outlook.MailItem)doc.MailEnvelope.Item;
mItem.To = strTo;
mItem.BCC = strBCC;
mItem.CC = strCC;
mItem.Subject = strSubject;

// Attach a file
String sSource = "C:\\TEMP\\org.sql";
String sDisplayName = "MyFirstAttachment";
int iPosition = 1;
int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
Outlook.Attachment oAttach = mItem.Attachments.Add(sSource, iAttachType,
iPosition, sDisplayName);

// Set the body
// Comment out and the signature stays
mItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
mItem.HTMLBody =
"<html><head><title></title></head>"
+"<body><p>This is a text from my application</p>"
+"<br /><b>Some bold stuff</b>"
+"<br /></body></html>";

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

Ken Slovak - [MVP - Outlook]

mItem.HTMLBody = mItem.HTMLBody & whatever

You're overwriting what's there already. Either concatenate your text to the
existing HTMLBody or concatenate the existing HTMLBody to your text.
 
J

Jeff

Ok when I do that though (access the body) I get the outlook warning message
that a possible malicious program is accessing the outlook program? Any
ideas oh how to get past that? (also this does not work, since the body is
actually not there when the show is called it almost looks like I need to
wait for some respons that the window has actually been opened)
 

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