【问题标题】:Sending Html emails using C# code使用 C# 代码发送 Html 电子邮件
【发布时间】:2011-10-31 08:40:34
【问题描述】:

我已经使用以下代码使用 xsl 样式表将 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);
    }

    }

【问题讨论】:

  • 此代码与您的要求没有任何关系。我认为你应该摆脱它。并查看SMTP Class

标签: c# .net html xml xslt


【解决方案1】:

我觉得你可以打电话

string myXmlXsltString = Writer.ToString();

将您的 xslt 转换为字符串。然后您可以像往常一样send the email,将正文设置为上面返回的字符串,例如:message.Body = myXmlXsltString

【讨论】:

  • 设置好消息正文后,需要设置 IsBodyHtml 属性:message.IsBodyHtml = true;
【解决方案2】:

在 .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);

【讨论】:

    猜你喜欢
    • 2014-12-19
    • 2016-11-23
    • 2012-01-06
    • 2011-09-26
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    相关资源
    最近更新 更多