【问题标题】:Attach An Email with an attachments to another email将带有附件的电子邮件附加到另一封电子邮件
【发布时间】:2011-04-25 14:05:45
【问题描述】:

所以我知道如何发送带有附件的电子邮件......这很容易。

现在的问题是我需要将具有自己附件的 MailMessage 添加到不同的 MailMessage。这将允许用户查看内容并获取预先制作的电子邮件并在一切正常的情况下发送。

我不确定这将是最终的工作流程,但我想知道是否容易。

我看到有一堆软件是为了赚钱,收到这些电子邮件的用户将使用 Outlook 客户端。

这将部署到廉价的共享主机解决方案,必须能够在 Meduim Trust 中运行!

我宁愿不必许可 3rd 方软件,没有 $ :(

任何想法都会很棒。

【问题讨论】:

    标签: asp.net c#-4.0 system.net.mail


    【解决方案1】:

    MailMessages 不能附加到其他 MailMessages。您要做的是创建一个 .msg 文件,该文件基本上是一个存储电子邮件及其所有附件的文件,并将其附加到您的实际 MailMessage 中。 Outlook 支持 MSG 文件。

    有关文件扩展名的更多信息,请访问此处:http://www.fileformat.info/format/outlookmsg/

    【讨论】:

    • 是的,但是如何保存没有第三方付费 dll 的 .msg?
    • 如果您查看我包含的链接,该站点描述了文件的整个格式。您可以使用您的应用程序自己创建 .msg 文件。
    【解决方案2】:

    正如 Justin 所说,在 API 中无法将一个 MailMessage 附加到另一个 MailMessage。我使用 SmtpClient 解决了这个问题,将我的内部消息“传递”到一个目录,然后将生成的文件附加到我的外部消息中。这个解决方案不是很吸引人,因为它必须使用文件系统,但它确实完成了工作。如果 SmtpDeliveryMethod 有 Stream 选项会更简洁。

    需要注意的是,SmtpClient 在创建消息文件时为 SMTP 信封信息添加了 X-Sender/X-Receiver 标头。如果这是一个问题,您必须在附加之前将它们从消息文件的顶部剥离。

    // message to be attached
    MailMessage attachedMessage = new MailMessage("bob@example.com"
        , "carol@example.com", "Attached Message Subject"
        , "Attached Message Body");
    
    // message to send
    MailMessage sendingMessage = new MailMessage();
    sendingMessage.From = new MailAddress("ted@example.com", "Ted");
    sendingMessage.To.Add(new MailAddress("alice@example.com", "Alice"));
    sendingMessage.Subject = "Attached Message: " + attachedMessage.Subject;
    sendingMessage.Body = "This message has a message attached.";
    
    // find a temporary directory path that doesn't exist
    string tempDirPath = null;
    do {
        tempDirPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
    } while(Directory.Exists(tempDirPath));
    // create temp dir
    DirectoryInfo tempDir = Directory.CreateDirectory(tempDirPath);
    
    // use an SmptClient to deliver the message to the temp dir
    using(SmtpClient attachmentClient = new SmtpClient("localhost")) {
        attachmentClient.DeliveryMethod
            = SmtpDeliveryMethod.SpecifiedPickupDirectory;
        attachmentClient.PickupDirectoryLocation = tempDirPath;
        attachmentClient.Send(attachedMessage);
    }
    
    tempDir.Refresh();
    // load the created file into a stream
    FileInfo mailFile = tempDir.GetFiles().Single();
    using(FileStream mailStream = mailFile.OpenRead()) {
        // create/add an attachment from the stream
        sendingMessage.Attachments.Add(new Attachment(mailStream
            , Regex.Replace(attachedMessage.Subject
                , "[^a-zA-Z0-9 _.-]+", "") + ".eml"
            , "message/rfc822"));
    
        // send the message
        using(SmtpClient smtp = new SmtpClient("smtp.example.com")) {
            smtp.Send(sendingMessage);
        }
        mailStream.Close();
    }
    
    // clean up temp
    mailFile.Delete();
    tempDir.Delete();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-05
      • 1970-01-01
      • 2014-06-25
      相关资源
      最近更新 更多