简体   繁体   English

如何从内存中制作Outlook邮件附件?

[英]How to make outlook mail attachment from memory?

Adding new System.Net.Mail.Attachment to Outlook.MailItem.Attachments via Attachments.Add() results in an System.ArgumentException: 'Sorry, something went wrong. You may want to try again.'通过Attachments.Add()将新System.Net.Mail.Attachment添加到Outlook.MailItem.Attachments导致System.ArgumentException: 'Sorry, something went wrong. You may want to try again.' System.ArgumentException: 'Sorry, something went wrong. You may want to try again.'

Trying to add a JPEG image encoded in Base64 as an attachment to a mail item in Outlook.尝试将 Base64 编码的 JPEG 图像作为附件添加到 Outlook 中的邮件项目。 I am storing encoded image as a variable, converting it to a memory stream, then to an attachment.我将编码图像存储为变量,将其转换为内存流,然后转换为附件。

public void CreateMessageWithAttachment() {
    Outlook.MailItem mailIttem = thisAddIn.Application.CreateItem(Outlook.OlItemType.olMailItem);
    string base64Attachment = "/...base64 gibberish";
    MemoryStream ms = new MemoryStream(Convert.FromBase64String(base64Attachment));
    ContentType ct = new ContentType(MediaTypeNames.Image.Jpeg);
    Attachment attachment = new Attachment(ms, ct);

    attachment.ContentDisposition.FileName = "look_at_dis.jpg";
    mailIttem.Subject = "Test e-mail message with attachment";
    mailIttem.To = "friend@company.com";
    mailIttem.Body = "This message has been generated programmatically";
    mailIttem.Attachments.Add(attachment); // This raises "Sorry..." expression
    mailIttem.Display(true);
}

Raises System.ArgumentException: 'Sorry, something went wrong. You may want to try again.'引发System.ArgumentException: 'Sorry, something went wrong. You may want to try again.' System.ArgumentException: 'Sorry, something went wrong. You may want to try again.' , which tells me nothing :-/ ,它什么也没告诉我:-/

Official docs gave me an impression that Attachments.Add is only supposed to really work with file paths, so saving MemoryStream to a temporary file and attaching it solved the problem. 官方文档给我的印象是, Attachments.Add只应该真正适用于文件路径,因此将MemoryStream保存到临时文件并附加它解决了问题。

string tempFilePath = Path.GetTempPath() + "look_at_dis.jpg";
FileStream fs = new FileStream(tempFilePath, FileMode.Create);

ms.CopyTo(fs);
fs.Close();

mailIttem.Attachments.Add(tempFilePath, Outlook.OlAttachmentType.olByValue, 1, "look_at_dis.jpg");

MailItem.Attachments.Add only allows to pass a string (a fully qualified path to a file) or another Outlook item (eg MailItem ) as the parameter. MailItem.Attachments.Add只允许传递一个字符串(文件的完全限定路径)或另一个 Outlook 项目(例如MailItem )作为参数。

On the Extended MAPI level (C++ or Delphi only), it is only taking an IStream (you are supposed to open PR_ATTACH_DATA_BIN as IStream using IAttach::OpenProperty ).在扩展 MAPI 级别(仅限 C++ 或 Delphi),它仅采用IStream (您应该使用IAttach::OpenPropertyPR_ATTACH_DATA_BIN作为IStream打开)。 If using Redemption (I am its author) is an option, it allows to pass a url, a file name, another Outlook item, IStream or IStorage COM interface, another attachment ( Outlook.Attachment or Redemption.RDOAttachment or IAttach MAPI interface) or an array (of Variant or of byte) to RDOMail .如果使用Redemption (我是它的作者)是一个选项,它允许传递 url、文件名、另一个 Outlook 项目、 IStreamIStorage COM 接口、另一个附件( Outlook.AttachmentRedemption.RDOAttachmentIAttach MAPI 接口)或一个数组(变体或字节)到RDOMMail Attachments .附件 Add

Before you attach the memory stream to the message, I believe you must reset its position to zero:在将内存流附加到消息之前,我相信您必须将其位置重置为零:

MemoryStream ms = new MemoryStream(Convert.FromBase64String(base64Attachment));
ms.Position = 0; // important
ContentType ct = new ContentType(MediaTypeNames.Image.Jpeg);
Attachment attachment = new Attachment(ms, ct);
// etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM