简体   繁体   English

生成HTML并将其作为电子邮件发送

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

I have written code in C# that should transform an XML with the help of a XSL stylesheet, generate some HTML and save it locally where the XML and XSL are there, then send the HTML 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();
}

 }

I am not sure whether the following line saves the html locally or not: 我不确定以下行是否将html保存在本地:

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

I am also getting a new error: "The type or namespace name 'Mail' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)" 我还收到一个新错误:“名称空间'System.Web'中不存在类型或名称空间名称'Mail'(您是否缺少程序集引用?)”

The SmtpClient class is defined in the System.Net.Mail namespace, not System.Web.Mail . SmtpClient类是在System.Net.Mail命名空间而不是System.Web.Mail定义的。 Your code needs some modifications. 您的代码需要进行一些修改。 For example things like Response.Write(ex.Message); 例如,诸如Response.Write(ex.Message);类的东西Response.Write(ex.Message); in a console application hardly make sense. 在控制台应用程序中几乎没有任何意义。 Ensuring proper disposal of disposable resources is also important. 确保妥善处理一次性资源也很重要。

So try improving your code a little: 因此,尝试稍微改善一下代码:

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);
        }
    }
}

Also make sure that 同时确保

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM