簡體   English   中英

生成HTML並將其作為電子郵件發送

[英]Generate HTML and send it as E-mail

我已經用C#編寫了代碼,該代碼應借助XSL樣式表轉換XML,生成一些HTML並將其保存在XML和XSL所在的本地位置,然后將HTML作為電子郵件發送。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mail;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;

public class SendMail
{
   static void Main(string[] args)
   {
     {       
       try{

         //load the Xml doc
         XPathDocument XPathDoc = new XPathDocument(@"C:\Test\svnlog.xml") ;

         XslTransform XslTrans = new XslTransform() ;

         //load the Xsl 
         XslTrans.Load(@"C:\Test\svnlog.xsl") ;

         //create the output stream
         XmlTextWriter Writer = new XmlTextWriter(@"C:\Test\CommitReport.html", null);

        //do the actual transform of Xml
        XslTrans.Transform(XPathDoc,null, Writer);        

        Writer.Close() ;

          }
      catch(Exception ex)
    {

        Response.Write(ex.Message);
    }


  using (StreamReader reader = File.OpenText(@"C:\Test\CommitReport.html"))
{                                                         
   MailMessage Mail = new MailMessage();
   Mail.To = ("pqr@dna.com "); 
   Mail.From = new MailAddress("abc@bac.com");
   Mail.Subject = ("Commit Error Report"); 
   Mail.IsBodyHtml = true;  //defines that your email is in Html form 
   Mail.BodyFormat = (@"C:\Test\CommitReport.html"); 

  Mail.Body = reader.ReadToEnd(); 
}

 //create instance of smtpclient 
 SmtpClient smtp = new SmtpClient(); 
 smtp.EnableSsl = true; 
 smtp.Send(mail);

 } 

 }

  private static void MailAddress(string p)
{
        throw new NotImplementedException();
}

 }

我不確定以下行是否將html保存在本地:

XmlTextWriter Writer = new XmlTextWriter(@"C:\Test\CommitReport.html", null); 

我還收到一個新錯誤:“名稱空間'System.Web'中不存在類型或名稱空間名稱'Mail'(您是否缺少程序集引用?)”

SmtpClient類是在System.Net.Mail命名空間而不是System.Web.Mail定義的。 您的代碼需要進行一些修改。 例如,諸如Response.Write(ex.Message);類的東西Response.Write(ex.Message); 在控制台應用程序中幾乎沒有任何意義。 確保妥善處理一次性資源也很重要。

因此,嘗試稍微改善一下代碼:

using System;
using System.IO;
using System.Net.Mail;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;

class Program
{
    static void Main()
    {
        try
        {
            var xPathDoc = new XPathDocument(@"C:\Test\svnlog.xml");
            var xslTrans = new XslCompiledTransform();
            xslTrans.Load(@"C:\Test\svnlog.xsl");
            using (var writer = XmlWriter.Create(@"C:\Test\CommitReport.html"))
            {
                xslTrans.Transform(xPathDoc, null, writer);
            }

            var mail = new MailMessage();
            mail.To.Add(new MailAddress("pqr@dna.com"));
            mail.From = new MailAddress("abc@bac.com");
            mail.Subject = "Commit Error Report";
            mail.IsBodyHtml = true;
            mail.Body = File.ReadAllText(@"C:\Test\CommitReport.html");

            using (var smtpClient = new SmtpClient("smtp.yourhost.com"))
            {
                smtpClient.EnableSsl = true;
                smtpClient.Send(mail);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

同時確保

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM