zoukankan      html  css  js  c++  java
  • 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);
    }
    }

  • 相关阅读:
    关于游戏分布式或者多服管理的想法
    surfaceView
    ackerman递归
    netbeans环境的建立
    copy-浅及深的复制操作
    使用VMware安装CentOS6.8详细教程
    Python在线资源优先级排序
    Python导入模块,Python import用法
    编码
    Python清屏命令
  • 原文地址:https://www.cnblogs.com/Lihao2013/p/3544385.html
Copyright © 2011-2022 走看看