将带有附件的电子邮件附加到另一封电子邮件

发布于 2024-11-03 09:01:03 字数 317 浏览 0 评论 0原文

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

现在的问题是我需要将一个具有自己的附件的 MailMessage 添加到另一个 MailMessage 中。这将允许用户查看内容并获取预先制作的电子邮件,如果一切正常则发送。

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

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

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

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

任何想法都会很棒。

So I know how to send emails with attachments... thats easy.

The problem now is I need to add an MailMessage, that has an attachment of its own, to a different MailMessage. This will allow the user to review things and take the email that is pre-made and send it if everything is ok.

I am not sure this will be the final work flow, but I would like to know if easy.

I see a bunch of software out there that is for money, the users getting these emails will be using an outlook client.

This would be deployed to a cheap shared hosting solutions, must be able to run in Meduim Trust!

I would prefer not to have to lic a 3rd party software, No $ :(

Any ideas would be awesome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静谧幽蓝 2024-11-10 09:01:03

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

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

MailMessages cannot be attached to other MailMessages. What you will do is create an .msg file, which is basically a file that stores an e-mail and all of its attachments, and attach that to your actual MailMessage. MSG files are supported by Outlook.

For more information about the file extension, go here: http://www.fileformat.info/format/outlookmsg/

十六岁半 2024-11-10 09:01:03

正如 Justin 所说,API 中没有将一个 MailMessage 附加到另一个 MailMessage 的工具。我使用 SmtpClient 将内部消息“传递”到目录,然后将生成的文件附加到外部消息,从而解决了这个问题。这个解决方案并不是很有吸引力,因为它必须利用文件系统,但它确实完成了工作。如果 SmtpDeliveryMethod 有 Stream 选项,那就干净多了。

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

// message to be attached
MailMessage attachedMessage = new MailMessage("[email protected]"
    , "[email protected]", "Attached Message Subject"
    , "Attached Message Body");

// message to send
MailMessage sendingMessage = new MailMessage();
sendingMessage.From = new MailAddress("[email protected]", "Ted");
sendingMessage.To.Add(new MailAddress("[email protected]", "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();

As Justin said, there is no facility to attach one MailMessage to another in the API. I worked around this using the SmtpClient to "deliver" my inner message to a directory, and then attached the resulting file to my outer message. This solution isn't terribly appealing, as it has to make use of the file system, but it does get the job done. It would be much cleaner if SmtpDeliveryMethod had a Stream option.

One thing to note, the SmtpClient adds X-Sender/X-Receiver headers for the SMTP envelope information when creating the message file. If this is an issue, you will have to strip them off the top of the message file before attaching it.

// message to be attached
MailMessage attachedMessage = new MailMessage("[email protected]"
    , "[email protected]", "Attached Message Subject"
    , "Attached Message Body");

// message to send
MailMessage sendingMessage = new MailMessage();
sendingMessage.From = new MailAddress("[email protected]", "Ted");
sendingMessage.To.Add(new MailAddress("[email protected]", "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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文