C# 发送电子邮件时将正文设置为html#

C# 发送电子邮件时将正文设置为html#,c#,html,asp.net-mvc,email,C#,Html,Asp.net Mvc,Email,我正在调用一个函数,该函数在我的asp.net mvc项目中发送电子邮件,我希望主体能够格式化为html 以下是我发送电子邮件的功能: private void EnvoieCourriel(string adrCourriel, string text, string object, string envoyeur, Attachment atache) { SmtpClient smtp = new SmtpClient(); MailMessage

我正在调用一个函数,该函数在我的asp.net mvc项目中发送电子邮件,我希望主体能够格式化为html

以下是我发送电子邮件的功能:

 private void EnvoieCourriel(string adrCourriel, string text, string object, string envoyeur, Attachment atache)
    {
        SmtpClient smtp = new SmtpClient();
        MailMessage msg = new MailMessage
        {
            From = new MailAddress(envoyeur),
            Subject = object,
            Body = text,         
        };

        if (atache != null)
            msg.Attachments.Add(atache);

            msg.To.Add(adrCourriel);
            smtp.Send(msg);


        return;
    }

电子邮件是发送的,它就像一个符咒,但它只是显示纯html,所以我想知道我的邮件实例中有一个参数,你只需要将参数IsBodyHtml添加到邮件实例中,如下所示:

 private bool EnvoieCourriel(string adrCourriel, string corps, string objet, string envoyeur, Attachment atache)
    {
        SmtpClient smtp = new SmtpClient();
        MailMessage msg = new MailMessage
        {
            From = new MailAddress(envoyeur),
            Subject = objet,
            Body = corps,
            IsBodyHtml = true
        };

        if (atache != null)
            msg.Attachments.Add(atache);

        try
        {
            msg.To.Add(adrCourriel);
            smtp.Send(msg);
        }
        catch(Exception e)
        {
           var erreur = e.Message;
            return false;
        }

        return true;
    }

我还添加了一个try-catch,因为如果在尝试发送邮件时出现问题,您可能会显示错误,或者只是知道在应用程序崩溃的情况下没有发送电子邮件,我认为您正在寻找IsBodyHtml

 private void EnvoieCourriel(string adrCourriel, string text, string object, string envoyeur, Attachment atache)
    {
        SmtpClient smtp = new SmtpClient();
        MailMessage msg = new MailMessage
        {
            From = new MailAddress(envoyeur),
            Subject = object,
            Body = text,
            IsBodyHtml = true
        };

        if (atache != null)
            msg.Attachments.Add(atache);

            msg.To.Add(adrCourriel);
            smtp.Send(msg);


        return;
    }

几秒钟之内就把我打败了+1