Send an email with format which is stored in a word document
1. Add a dll reference: Microsoft.Office.Interop.Word.dll
2. Add the following usings
using Word = Microsoft.Office.Interop.Word;
using System.Net.Mail;
using System.Text.RegularExpressions;
3. Paste the following code into your application and call it.
Note: Please modify the email info, and put a word file with your email body.
private void SendEmailWithFormat()
{
Word.Application myWord = new Word.Application();
Word.Document doc = new Word.Document();
object unknow = Type.Missing;
try
{
string fileLocation = Application.StartupPath +@"\EmailBody.docx";
doc.Activate();
doc = myWord.Documents.Open(fileLocation,ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,ref unknow, ref unknow, ref unknow, ref unknow, ref unknow);
doc.ActiveWindow.Selection.WholeStory();
doc.ActiveWindow.Selection.Copy();
doc.ActiveWindow.Visible = false;
IDataObject data = Clipboard.GetDataObject();
string fileContentInHtmlFormat = data.GetData(DataFormats.Html).ToString();
string body = "<html " + Regex.Split(fileContentInHtmlFormat, "<html", RegexOptions.IgnoreCase)[1];
MailMessage mail = new MailMessage();
mail.IsBodyHtml = true;
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your gmail account");
mail.To.Add("send to email account");
mail.Subject = "Test Mail";
mail.Body = body;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("your gmail account", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("Mail Has Been Sent Out Successfully!","Successfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
doc.Close(ref unknow, ref unknow, ref unknow);
myWord.Quit(ref unknow, ref unknow, ref unknow);
}
}
Send an email with format which is stored in a word document的更多相关文章
- How to Send an Email Using UTL_SMTP with Authenticated Mail Server. (文档 ID 885522.1)
APPLIES TO: PL/SQL - Version 9.2.0.1 to 12.1.0.1 [Release 9.2 to 12.1]Information in this document a ...
- How to Send an Email Using UTL_SMTP with Authenticated Mail Server
In this Document Goal Solution References APPLIES TO: PL/SQL - Version 9.2.0.1 to 12.1.0.1 [Re ...
- [转]How To Send Transactional Email In A NodeJS App Using The Mailgun API
https://www.npmjs.com/package/mailgun-js 本文转自:https://www.mailgun.com/blog/how-to-send-transactional ...
- word dde payload
payload: ctrl+F9 {DDEAUTO c:\\windows\\system32\\cmd.exe "/k calc.exe" } Since this techni ...
- Send email alert from Performance Monitor using PowerShell script (检测windows服务器的cpu 硬盘 服务等性能,发email的方法) -摘自网络
I have created an alert in Performance Monitor (Windows Server 2008 R2) that should be triggered whe ...
- 5 Ways to Send Email From Linux Command Line
https://tecadmin.net/ways-to-send-email-from-linux-command-line/ We all know the importance of email ...
- [Windows Azure] Building worker role B (email sender) for the Windows Azure Email Service application - 5 of 5.
Building worker role B (email sender) for the Windows Azure Email Service application - 5 of 5. This ...
- Sending e-mail with Spring MVC---reference
reference from:http://www.codejava.net/frameworks/spring/sending-e-mail-with-spring-mvc Table of con ...
- [Windows Azure] Building worker role A (email scheduler) for the Windows Azure Email Service application - 4 of 5.
Building worker role A (email scheduler) for the Windows Azure Email Service application - 4 of 5. T ...
随机推荐
- iOS-NSThread使用
NSThread: 优点:NSThread 比其他两个轻量级(Cocoa NSOperation.GCD) 缺点:需要自己管理线程的生命周期,线程同步.线程同步对数据的加锁会有一定的系统开销 Coco ...
- JS操作DOM常用API总结
<JS高程>中的DOM部分写的有些繁琐,还没勇气整理,直到看到了这篇博文 Javascript操作DOM常用API总结,顿时有了一种居高临下,一览全局的感觉.不过有时间还是得自己把书里面的 ...
- 用jquery将复选框改成单选框
前提是要包含jquery文件. 相关代码: jQuery(function($) { init_price_checkbox("by_price"); init_price_che ...
- easyUI——datebox验证和自定义取消按钮
来源:http://blog.csdn.net/liusong0605/article/details/42270463 1. datebox验证 验证结束时间<起始时间: 起始时 ...
- 使用apt-get方式为Kubuntu安装PHP+MYSQL+Apache
相信很多搭过动态网站的朋友都知道怎么搭web服务器, 本人是linux新手, 以前在windows是直接使用集成的wamp server, 所以在linux没有亲手搭过. 本系统: ubuntu 12 ...
- BIOS设置
主界面: 标准BLOS设置: 高级BLOS设置: 1.设置从光盘驱动 电脑启动时默认是从硬盘启动,但是在安装操作系统时或者是使用某些特殊软件时,可能需要从光盘.软盘或者U盘启动,这时就需要设置第一启 ...
- Git常用命令总结(超实用)
导读 Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目.一般来说,日常使用Git只要记住下图6个命令,就可以了.但是熟练使用,恐怕要记住60-100个命令. 下面是我整 ...
- yii2 文件上传
直接贴代码了 --------------------------------------------------------------------------------------------- ...
- 如何让Android字体自适应屏幕分辨率
在不同的分辨率下,Android字体大小怎么自适应分辨率的变化? 假设需要适应320x240,480x320分辨率.在res目录下新建文件夹values-320x240, values-480x320 ...
- JSON日期格式处理
protected static SerializeConfig mapping = new SerializeConfig(); private static String dateFormat; ...