C# 使用 smtp 时如何格式化电子邮件中的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17245895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:57:12  来源:igfitidea点击:

How to format text in email when using smtp

c#smtp

提问by Hyman28

I'm using the following method to send an email. I want to be able to format the email with bold text.

我正在使用以下方法发送电子邮件。我希望能够用粗体文本格式化电子邮件。

Ex.

前任。

From:name

来自:名字

Email:email address

电子邮件:电子邮件地址

Message:message

留言:留言

How would I do this?

我该怎么做?

    protected void SendMail()
    {
        var fromAddress = "[email protected]";
        var toAddress = "[email protected]";
        const string fromPassword = "mypassword";

        string subject = "New Email from Website";
        string body = "From: " + name.Text + "\n";
        body += "Email: " + email.Text + "\n";
        body += "Message: \n" + message.Text + "\n";

        var smtp = new System.Net.Mail.SmtpClient();
        {
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
            smtp.Timeout = 20000;
        }
        // Passing values to smtp object
        smtp.Send(fromAddress, toAddress, subject, body);
    }

采纳答案by Kas

Set isBodyHtml to true, the following code describes it, To send a bold text you can use "<b> My bold text </b>". To send an italicized text you can use "<i> Italic text </i>". To send an underlined text you can use "<u> underlined text </u>".

将 isBodyHtml 设置为 true,下面的代码对其进行了描述,要发送粗体文本可以使用“ <b> My bold text </b>”。要发送斜体文本,您可以使用“ <i> Italic text </i>”。要发送带下划线的文本,您可以使用“ <u> underlined text </u>”。

You can copy and use the following method. By using this method, it will be very easy to send emails.

您可以复制并使用以下方法。通过使用这种方法,发送电子邮件将非常容易。

public static bool SendEmail(string To, string ToName, string From, string FromName, string Subject, string Body, bool IsBodyHTML)
{
    try
    {
        MailAddress FromAddr = new MailAddress(From, FromName, System.Text.Encoding.UTF8);
        MailAddress ToAddr = new MailAddress(To, ToName, System.Text.Encoding.UTF8);
        var smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 25,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new System.Net.NetworkCredential("your email address", "your password")
        };

        using (MailMessage message = new MailMessage(FromAddr, ToAddr)
        {
            Subject = Subject,
            Body = Body,
            IsBodyHtml = IsBodyHTML,
            BodyEncoding = System.Text.Encoding.UTF8,

        })
        {
            smtp.Send(message);
        }
        return true;
    }
    catch
    {
        return false;
    }
}

When you call this method call it like this

当你调用这个方法时,像这样调用它

 SendEmail("Here address to" , "Here to name" , "Here from", "here from name", "here subject" , here Body, " Here whether HTML or Plain" )

回答by Bilal Fazlani

You need only few minor changes.

您只需要进行一些细微的更改。

  1. say IsBodyHtmlis true
  2. replace all \nwith <br/>
  1. IsBodyHtmltrue
  2. 全部替换\n<br/>

and here's the final the code

这是最后的代码

protected void SendMail()
{
    var fromAddress = "[email protected]";
    var toAddress = "[email protected]";
    const string fromPassword = "mypassword";

    string subject = "New Email from Website";
    string body = "From: " + name.Text + "<br/>";
    body += "Email: " + email.Text + "<br/>";
    body += "Message: <br/>" + message.Text + "<br/>";

    var smtp = new System.Net.Mail.SmtpClient();
    {
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.EnableSsl = true;
        smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
        smtp.Timeout = 20000;
    }
    // Passing values to smtp object
    smtp.Send(fromAddress, toAddress, subject, body,IsBodyHtml:true);
}

Hope that helps.

希望有帮助。