如何将列表作为电子邮件发送?

时间:2014-04-13 12:28:04

标签: c# html

在我的wpf应用程序中,我需要发送一封电子邮件和列表的正文我需要发送一个项目列表。现在一切正常,除了正文不是我在我的代码中写的格式。 这是发送电子邮件的功能:

public static void CreateTimeoutTestMessage(string ParentAdress, List<KidHistory> list,Parent p,Enfant e)
    {

        var fromAddress = new MailAddress("famissima.mic@gmail.com", "famissima.mic");
        var toAddress = new MailAddress(ParentAdress);
        const string fromPassword = "xxxxxxxxxx";
        const string subject = "Subject";
        var smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Timeout = 20000,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
        };
        var message = new MailMessage(fromAddress, toAddress);
        message.Subject = subject;
        if (p.Civilite == "Mr")
            message.Body += "Bonjour Mr. " + " " + p.Nom + " " + p.Prenom + " ,l'historique de navigation de votre enfant " + e.pseudo + " est: ";
        else
            message.Body += "Bonjour M. " + " " + p.Nom + " " + p.Prenom + " ,l'historique de navigation de votre enfant " + e.pseudo + " est: ";
        message.Body += "<table width='100%' style='border:Solid 1px Black;'>";
        foreach (var item in list)
        {
            message.Body += "<tr>";
            message.Body += "<td stlye='color:blue;'>" + item.url + "</td>" + "<td stlye='color:blue;'>" + item.StartDate + "</td>" + "<td stlye='color:blue;'>" + item.FinishDate + "</td>";
            message.Body += "</tr>";
        }
        message.Body += "</table>";
        smtp.Send(message);
    }

这是输出(收到的邮件): the result

1 个答案:

答案 0 :(得分:1)

IsBodyHtml设置为true可以在邮件正文中使用HTML标记:

message.IsBodyHtml = true;