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 ...
随机推荐
- HDOJ-三部曲-多重背包-1014-Cash Machine
通过这道题我基本了解了利用二进制对多重背包问题进行优化的思想. Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submiss ...
- Unity3D ShaderLab 静态贴图光照模型
Unity3D ShaderLab 静态贴图光照模型 其实在unity的光照模型中,我们可以把光照讯息烘培进入一个2D贴图,来实现着色器的光照效果. 下面是在unity中关闭灯光和打开灯光的对比效果. ...
- INNO:检测程序是否已经安装,是则弹出卸载提示。
INNO:检测程序是否已经安装,是则弹出卸载提示. 作者:少轻狂 | 发布:2010-08-05 | 更新:2013-09-05 | 分类:部署 | Disposition | 热度:2816 ℃ 实 ...
- FSM
一.状态机简单介绍 软件设计中的状态机概念,一般是指有限状态机(英语:finite-state machine,缩写:FSM)又称有限状态自动机,简称状态机,是表示有限个状态以及在这些状态之间的转移和 ...
- MINIX3 系统任务分析
MINIX3 系统任务分析 7.1 MINIX3 系统任务概要 MINIX3 怎么来给用户提供丰富的服务呢?除了中断,异常处理,除了时钟服务. 程序员总是希望一个操作系统给他提供足够的服务,使得他 ...
- git 恢复工作区删除的所有文件
/********************************************************************* * git 恢复工作区删除的所有文件 * 说明: * 今天 ...
- 常见的XMLHttpRequest.status状态码
XMLHttpRequest.status状态码 1xx-信息提示 这些状态代码表示临时的响应.客户端在收到常规响应之前,应准备接收一个或多个1xx响应. 100-继续. 101-切换协议. 2xx- ...
- 用Xshell访问 虚拟机里的kali
1.vim /etc/ssh/sshd_config 2.PermitRootLogin without-password复制 3.把一个PermitRootLogin without-passwor ...
- for each 循环
前言 在C++中,经常用到类似 for (int i=0; i<n; i++); 这样的循环控制结构. 然而,如果要求循环变量的改变方式不是简单的+1递增,而是依次取某个数组里面的元素,那么C+ ...
- html中的元素和节点
元素(Element)和结点(Node)的区别, 元素是一个小范围的定义,必须是含有完整信息的结点才是一个元素,例如<div>...</div>. 但是一个结点不一定是一个元素 ...