Connecting Tech Pros Worldwide Forums | Help | Site Map

C# and Outlook

Jeff
Guest
 
Posts: n/a
#1: Sep 14 '06
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 = "name@domain.com";
string strBCC = "name@domain.com";
string strCC = "name@domain.com";
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();



Ken Slovak - [MVP - Outlook]
Guest
 
Posts: n/a
#2: Sep 15 '06

re: C# and 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.


--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


"Jeff" <jeff_nospam@lindholm.orgwrote in message
news:EedOg.921$vJ2.799@newssvr12.news.prodigy.com. ..
Quote:
>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 = "name@domain.com";
string strBCC = "name@domain.com";
string strCC = "name@domain.com";
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();
>
>
Jeff
Guest
 
Posts: n/a
#3: Sep 15 '06

re: C# and Outlook


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)

"Ken Slovak - [MVP - Outlook]" <kenslovak@mvps.orgwrote in message
news:Oikn0NN2GHA.2196@TK2MSFTNGP06.phx.gbl...
Quote:
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.
>
>
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm
>
>
"Jeff" <jeff_nospam@lindholm.orgwrote in message
news:EedOg.921$vJ2.799@newssvr12.news.prodigy.com. ..
Quote:
>>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 = "name@domain.com";
>string strBCC = "name@domain.com";
>string strCC = "name@domain.com";
>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();
>>
>>
>

Ken Slovak - [MVP - Outlook]
Guest
 
Posts: n/a
#4: Sep 16 '06

re: C# and Outlook


Your options for the security are explained at
http://www.outlookcode.com/d/sec.htm.

Wait for the first Inspector.Activate event to fire for the item that's
being opened.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


"Jeff" <jeff_nospam@lindholm.orgwrote in message
news:BjBOg.1254$vJ2.317@newssvr12.news.prodigy.com ...
Quote:
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)
Closed Thread