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

时间:2013-06-21 23:51:24

标签: c# smtp

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

实施例

来自:名称

电子邮件:电子邮件地址

消息:消息

我该怎么做?

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

2 个答案:

答案 0 :(得分:6)

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

您可以复制并使用以下方法。通过使用此方法,可以非常轻松地发送电子邮件。

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" )

答案 1 :(得分:2)

您只需要进行一些小的改动。

  1. IsBodyHtmltrue
  2. 将所有\n替换为<br/>
  3. 这里是最后的代码

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

    希望有所帮助。