使用C#代码发送Html电子邮件

时间:2011-10-31 08:40:35

标签: c# .net html xml xslt

我已经使用以下代码使用xsl stylesheet将xml转换为html。现在我必须将生成的html作为电子邮件发送给选定的人。我不知道如何去做。请帮助!!!

    //import name spaces
    using System.Xml.Xsl;
    using System.Xml.XPath;
    using System.IO;
    using System.Xml;

    public static void Transform(string XmlPath, string XslPath){

    try{

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

        XslTransform XslTrans = new XslTransform() ;

        //load the Xsl 
        XslTrans.Load(C:\Dibya\svnlog.xsl) ;

        //create the output stream
        XmlTextWriter Writer = new XmlTextWriter
            ("CommitReport.html", null);

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

        Writer.Close() ;


    }
      catch(Exception ex)
    {

        Response.Write(ex.Message);
    }

    }

2 个答案:

答案 0 :(得分:0)

我想你可以打电话

string myXmlXsltString = Writer.ToString();

将xslt转换后的xml转换为String。然后您可以按照正常情况send the email将主体设置为上面返回的字符串,例如:message.Body = myXmlXsltString

答案 1 :(得分:0)

在.net中发送电子邮件非常简单,发送HTML电子邮件而不是普通测试只是一个单行开关

以下是使用Gmail帐户发送电子邮件的示例,请注意IsBodyHtml设置。

System.Net.NetworkCredential loginInfo = new System.Net.NetworkCredential("xxx@gmail.com", "yyyy");
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.From = new System.Net.Mail.MailAddress("xxx@gmail.com", "Admin");
msg.To.Add(new System.Net.Mail.MailAddress(sTo, sToDisplayName));
msg.Subject = sSubject;
msg.Body = sBody;
msg.IsBodyHtml = true;
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.gmail.com");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
client.Send(msg);