翻譯|使用教程|編輯:李顯亮|2021-02-18 10:37:23.850|閱讀 315 次
概述:大多數(shù)內(nèi)置的電子郵件編輯器不提供高級(jí)格式化選項(xiàng)。為了解決此限制,本文介紹如何使用Word文檔作為C#中的電子郵件正文來(lái)撰寫電子郵件。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
電子郵件正文的呈現(xiàn)是吸引讀者的重要因素之一。因此,電子郵件使用標(biāo)題,子標(biāo)題,表格,圖像等進(jìn)行了很好的格式化。但是,大多數(shù)內(nèi)置的電子郵件編輯器不提供高級(jí)格式化選項(xiàng)。為了解決此限制,本文介紹如何使用Word文檔作為C#中的電子郵件正文來(lái)撰寫電子郵件。
為了從Word文檔中導(dǎo)入內(nèi)容,將使用Aspose.Words for .NET API。而在撰寫和發(fā)送電子郵件時(shí),將利用Aspose.Email for .NET的功能。兩個(gè)API均可點(diǎn)擊名稱進(jìn)入下載。
1、使用Aspose.Words.Document類加載Word文檔,并將其另存為MHTML到MemoryStream對(duì)象中。
// 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的對(duì)象到Aspose.Email.MailMessage對(duì)象和集受試者,至和從電子郵件的字段。
// 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類設(shè)置SMTP客戶端并發(fā)送電子郵件。
// 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文檔作為電子郵件正文導(dǎo)入的完整源代碼。
// 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);
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn