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 ...
随机推荐
- linux命令:mv
1.命令介绍: mv是move的缩写,用来移动文件或重命名文件 2.命令格式: mv [选项] 源文件 目标文件 3.命令参数: -b :若需覆盖文件,则覆盖前先行备份. -f --force:fo ...
- Linux下查看每个目录所占用空间大小的命令
cd到上级目录,然后输入一条命令即可查询每个子目录所占用的空间大小 du -h --max-depth=1 可以更改--max-depth参数的值,该参数表示查询子目录的层级,当前为1层
- C#_WinForm接收命令行参数
C#_WinForm接收命令行参数 2014-08-03 10:17 534人阅读 评论(0) 收藏 举报 首先,我要仔细的声明下,本文讲的是接受命令行参数,让程序启动.而不是启动那个黑黑的框...我 ...
- 第二个Sprint冲刺第七天
讨论地点:宿舍 讨论成员:邵家文.李新.朱浩龙.陈俊金 讨论:整理已完成的功能
- Qt Clipboard剪贴板简单使用
设置剪贴板的内容 QClipboard *clipboard = QApplication::clipboard(); clipboard->setText("contents&quo ...
- ORA-12545: 因目标主机或对象不存在, 连接失败
ORA-12545: 因目标主机或对象不存在, 连接失败 1. 问题描述 XP系统下同时安装了AX1应用程序和升级版AX2,连接同一个在本机Oracle客户端上配置的连接实例,其中AX2显示链接成功, ...
- 解决SimpleCursorAdapter不能自动更新的问题
假设场景是这样的:你使用SimpleCursorAdapter显示数据,并监听数据的变化:在数据发生变化的时候,调用cursor的requery,期待UI显示也跟着变化. 但是,你可能会发现,UI并没 ...
- hdu 5206 Four Inages Strategy
题目大意: 判断空间上4个点是否形成一个正方形 分析: 标称思想 : 在p2,p3,p4中枚举两个点作为p1的邻点,不妨设为pi,pj,然后判断p1pi与p1pj是否相等.互相垂直,然后由向量法,最后 ...
- Java-->实现断点续传(下载)
--> 断点续传: 就像迅雷下载文件一样,停止下载或关闭程序,下次下载时是从上次下载的地方开始继续进行,而不是重头开始... --> RandomAccessFile --> poi ...
- 转载:LBP的初步理解
转自http://blog.csdn.net/ty101/article/details/8905394 本文的PDF版本,以及涉及到的所有文献和代码可以到下列地址下载: 1.PDF版本以及文献:ht ...