簡體   English   中英

如何使用C#在正文中使用Microsoft Publisher發布電子郵件

[英]How to send email using microsoft publisher in body using c#

嗨,我有一個新的要求。 如何通過郵件發送正文中的新聞通訊。

該新聞通訊是由Microsoft Publisher應用程序制作的。

請讓我知道是否需要更多信息。

謝謝

Lachlan Roche有一個很好的答案。 我只想補充一下,您可能會考慮將新聞稿輸出到Adobe Acrobat,圖像文件或html。

新聞通訊針對的大多數人可能不會安裝Publisher。 因此向他們發送.pub文件可能不會達到預期的效果。

我想您的客戶希望能夠在Publisher中調用Macro或Office App來將他們撰寫的新聞通訊作為電子郵件發送給人員列表。

Lachlans代碼將使您能夠發送電子郵件,我建議增加一個步驟以將新聞通訊導出為更通用的格式。 我確信您可以利用代碼中Publisher中的內置函數進行導出。

要在.NET中發送電子郵件,請使用SmtpClient以及MailMessageAttachment類。

MailMessage類表示郵件的內容。 SmtpClient類將電子郵件傳輸到您指定用於郵件傳遞的SMTP主機。 您可以使用Attachment類創建郵件附件。

假設您有一個帶有獨立樣式表和圖像的HTML新聞,則需要創建一個具有HTML正文內容的MailMessage並將外部文件添加為附件。 您將需要設置每個附件的ContentId屬性,並更新HTML中的引用以使用此屬性。

正文HTML中的href附件使用cid:方案。 對於ID為“ xyzzy”的附件,href為“ cid:xyzzy”。

要使用HTML正文構造MailMessage,請執行以下操作:

        string content; // this should contain HTML

        // Create a message and set up the recipients.
        MailMessage message = new MailMessage(
           "from@example.com",
           "to@example.com");

        message.Subject = "The subject.";
        message.Body = content;
        message.IsBodyHtml = true;

要構造帶有附件的MailMessage:

        string file = "data.xls";

        // Create a message and set up the recipients.
        MailMessage message = new MailMessage(
           "from@example.com",
           "to@example.com");

        message.Subject = "The subject.";
        message.Body = "See the attached file";

        // Create  the file attachment for this e-mail message.
        Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
        data.ContentId = Guid.NewGuid().ToString();
        // Add time stamp iformation for the file.
        ContentDisposition disposition = data.ContentDisposition;
        disposition.CreationDate = System.IO.File.GetCreationTime(file);
        disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
        disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
        // Add the file attachment to this e-mail message.
        message.Attachments.Add(data);

要使用SmtpClient發送MailMessage:

        //Send the message.
        SmtpClient client = new SmtpClient(server);
        // Add credentials if the SMTP server requires them.
        client.Credentials = CredentialCache.DefaultNetworkCredentials;
        client.Send(message);

暫無
暫無

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

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