8

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

前任。

出自: 姓名

电子邮件: 电子邮件地址

留言: 留言

我该怎么做?

    protected void SendMail()
    {
        var fromAddress = "myemail@gmail.com";
        var toAddress = "myotheremail@gmail.com";
        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);
    }
4

2 回答 2

9

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

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

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;
    }
}

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

 SendEmail("Here address to" , "Here to name" , "Here from", "here from name", "here subject" , here Body, " Here whether HTML or Plain" )
于 2013-06-22T04:00:47.863 回答
3

你只需要很少的小改动。

  1. IsBodyHtmltrue
  2. 全部替换\n<br/>

这是最后的代码

protected void SendMail()
{
    var fromAddress = "myemail@gmail.com";
    var toAddress = "myotheremail@gmail.com";
    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);
}

希望有帮助。

于 2013-06-22T06:42:25.147 回答