簡體   English   中英

將帶有附件的電子郵件附加到另一封電子郵件

[英]Attach An Email with an attachments to another email

所以我知道如何發送帶有附件的電子郵件...那很容易。

現在的問題是,我需要將具有自己附件的MailMessage添加到其他MailMessage。 這將使用戶可以查看內容並接受預制的電子郵件,如果一切正常,則將其發送。

我不確定這將是最終的工作流程,但我想知道是否容易。

我看到有一堆賺錢的軟件,收到這些電子郵件的用戶將使用Outlook客戶端。

這將被部署到廉價的共享托管解決方案中,必須能夠在Meduim Trust中運行!

我希望不必使用第三方軟件lic,No $ :(

任何想法都很棒。

MailMessage不能附加到其他MailMessage。 您將要做的是創建一個.msg文件,該文件基本上是一個存儲電子郵件及其所有附件的文件,並將其附加到實際的MailMessage中。 Outlook支持MSG文件。

有關文件擴展名的更多信息,請訪問: http : //www.fileformat.info/format/outlookmsg/

正如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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM