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 ...
随机推荐
- APP推广入门之AppStore数据分析
AppStore中有很多数据,但对于一个App推广者而言,最需要关注的无非这么几个,即下载量.评论.权重.榜单排名.搜索排名以及热词搜索等.至于这些数据具体代表着什么,下面就先由下载量开始,来简单介绍 ...
- java作业2
(一) 仔细阅读示例: EnumTest.java,运行它,分析运行结果? 你能得到什么结论?你掌握了枚举类型的基本用法了吗? 结论:枚举不属于原始数据类型,它的每个具体值都引用一个特定的对象.相同的 ...
- free命令
最近服务器总是出问题,研究一下free 以M的形式显示: 参数: Swap 是交换区信息, Swap空间的作用可简单描述为:当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当 ...
- ueditor上传图片到七牛云存储(form api,java)
转:http://my.oschina.net/duoduo3369/blog/174655 ueditor上传图片到七牛云存储 ueditor结合七牛传图片 七牛的试炼 开发前的准备与注意事项说明 ...
- UI学习笔记---第一天
一.iOS概述 iOS是Apple公司的移动操作系统,主要⽤用于iPhone.iPad.iPad Mini.iPod Touch等移动产品. 借助iOS,我们可以开发视频类.美图类.新闻类.⾳乐类.团 ...
- springmvc学习笔记--REST API的异常处理
前言: 最近使用springmvc写了不少rest api, 觉得真是一个好框架. 之前描述的几篇关于rest api的文章, 其实还是不够完善. 比如当遇到参数缺失, 类型不匹配的情况时, 直接抛出 ...
- Codeforces Round #313 (Div. 2) A B C 思路 枚举 数学
A. Currency System in Geraldion time limit per test 2 seconds memory limit per test 256 megabytes in ...
- 使用Jsoup函数包抓取网页内容
之前写过一篇用Java抓取网页内容的文章,当时是用url.openStream()函数创建一个流,然后用BufferedReader把这个inputstream读取进来.抓取的结果是一整个字符串.如果 ...
- jquery中prop()方法和attr()方法的区别
最近在用jquery的时候遇到一个问题,那就是attr()方法,发现这个方法有时候使用会有一些说不出原因的问题.翻翻自己之前笔记发现,还有个函数prop(). 这两个函数都可以用来获取属性. jque ...
- tty驱动程序框架
tty驱动程序框架 一.TTY概念解析 在Linux系统中,终端是一类字符型设备,它包括多种类型,通常使用tty来简称各种类型的终端设备. 1.1串口终端(/dev/ttyS*) 串口终端是使用计算机 ...