翻譯|使用教程|編輯:李顯亮|2021-02-18 10:37:23.850|閱讀 315 次
概述:大多數內置的電子郵件編輯器不提供高級格式化選項。為了解決此限制,本文介紹如何使用Word文檔作為C#中的電子郵件正文來撰寫電子郵件。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
電子郵件正文的呈現是吸引讀者的重要因素之一。因此,電子郵件使用標題,子標題,表格,圖像等進行了很好的格式化。但是,大多數內置的電子郵件編輯器不提供高級格式化選項。為了解決此限制,本文介紹如何使用Word文檔作為C#中的電子郵件正文來撰寫電子郵件。
為了從Word文檔中導入內容,將使用Aspose.Words for .NET API。而在撰寫和發送電子郵件時,將利用Aspose.Email for .NET的功能。兩個API均可點擊名稱進入下載。
1、使用Aspose.Words.Document類加載Word文檔,并將其另存為MHTML到MemoryStream對象中。
// Load a Word document from disk Document wordDocument = new Document("Word.docx"); // Save document as MHTML into memory stream MemoryStream mhtmlStream = new MemoryStream(); wordDocument.Save(mhtmlStream, SaveFormat.Mhtml);
2、裝入MHTML從MemoryStream的對象到Aspose.Email.MailMessage對象和集受試者,至和從電子郵件的字段。
// Set position to 0 mhtmlStream.Position = 0; // Create email message from MHTML MailMessage message = MailMessage.Load(mhtmlStream, new MhtmlLoadOptions()); // Set email fields message.Subject = "Sending Invoice in Email"; message.From = "sender@gmail.com"; message.To = "recipient@gmail.com";
3、使用Aspose.Email.Clients.Smtp.SmtpClient類設置SMTP客戶端并發送電子郵件。
// Send email via SMTP SmtpClient client = new SmtpClient("smtp.gmail.com", 587, "sender@gmail.com", "pwd"); client.SecurityOptions = SecurityOptions.SSLExplicit; client.Send(message);
以下是使用C#將MS Word文檔作為電子郵件正文導入的完整源代碼。
// Load a Word document from disk Document wordDocument = new Document("Word.docx"); // Save document as MHTML into memory stream MemoryStream mhtmlStream = new MemoryStream(); wordDocument.Save(mhtmlStream, SaveFormat.Mhtml); // Set position to 0 mhtmlStream.Position = 0; // Create email message from MHTML MailMessage message = MailMessage.Load(mhtmlStream, new MhtmlLoadOptions()); // Set email fields message.Subject = "Sending Invoice in Email"; message.From = "sender@gmail.com"; message.To = "recipient@gmail.com"; // Send email via SMTP SmtpClient client = new SmtpClient("smtp.gmail.com", 587, "sender@gmail.com", "pwd"); client.SecurityOptions = SecurityOptions.SSLExplicit; client.Send(message);
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn