开发者

Emailing a fixed document through Outlook

I've added functionality to an application that prints out a bunch of information to a FixedDOcument and sends this off to the printer. This works just fine, however the request is that there be an in application function that emails the document using OUtlook and its here that I come unstuck. I'd very much like to just reuse the class that makes the fixed document for printing to generate the text for email, but I'm struggling to do this.

I've tried the following...

Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
MailItem email = (MailItem)(oApp.CreateItem(OlItemType.olMailItem));
email.Recipients.Add("someone@somewhere.com");
email.Subject = "Hello";
email.Body = "TEST";
FixedDocument doc = CreateReport(); //make my fixed document

//this doesn't work, and the parameters it takes suggest it never will
email.Attachments.Add(doc, OlAttachmentType.olByValue, 1, null); 
email.Send();

I can't开发者_Go百科 help but think I'm on completely the wrong tack here, but I don't really want to have to write a bunch of new text formatting (since email.Body only takes a string) when I've already got the content formatted how I want it.

Note that the content is all textual, so I don't really care if it gets sent as an attachment or as text in the emails body. Ideally if its sent as an attachment it won't be saved anywhere permanently.

Any pointers?


Save the Fixed document to the users temporary folder, than put the full path name of the document in the attachment, send it, and then delete the document from temporary folder.

Attachment Signature

Attachment Add (
    [InAttribute] Object Source,
    [OptionalAttribute] [InAttribute] Object Type,
    [OptionalAttribute] [InAttribute] Object Position,
    [OptionalAttribute] [InAttribute] Object DisplayName
)

Source Parameter:

Source
The source of the attachment. This can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment.

Saving Document:

String TempFoder = System.Environment.GetEnvironmentVariable("TEMP");
String tempFileName = TempFoder + @"\TempFixedDocument.doc";
FixedDocument.Save(tempFileName);

Sending Email:

Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
MailItem email = (MailItem)(oApp.CreateItem(OlItemType.olMailItem));
email.Recipients.Add("someone@somewhere.com");
email.Subject = "Hello";
email.Body = "TEST";
FixedDocument doc = CreateReport(); //make my fixed document

//this doesn't work, and the parameters it takes suggest it never will
email.Attachments.Add(tempFileName, OlAttachmentType.olByValue, 1, null); 
email.Send();

Deleting Temp File:

System.IO.File.Delete(tempFileName);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜