硬编码文本和格式

时间:2013-10-28 13:56:39

标签: c# text-formatting mailmessage

我正在发送一封包含C#的电子邮件,并将所需的所有数据硬编码到我的身体中。

但是我需要更改某些段落的某些字体(签名)。我需要将签名的颜色更改为灰色并将字体大小更改为更小的尺寸。这可以用硬编码吗?

mm.Body = "TEXT TEXT TEXT"+
"\r\n different font and color";

4 个答案:

答案 0 :(得分:6)

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

msg = new MailMessage("xxxx@gmail.com",
                "yyyy@gmail.com", "Message from PSSP System",
                "This email sent by the PSSP system<br />" +
                "<b>this is bold text!</b>");

msg.IsBodyHtml = true;

Read this

还试试这个:

msg.BodyFormat = MailFormat.Html;

答案 1 :(得分:0)

您好,您需要将IsBodyHtml设置为true:

 MailMessage msg = new MailMessage(addressFrom, addressTo, subject, body);
            msg.IsBodyHtml = isBodyHtml;

body parameter should contain actual body of your mail with style applied

答案 2 :(得分:0)

使用htmlbody设置字体和颜色......

    namespace mysendemail
    {
     class Program
     {
      static void Main(string[] args)
      {
        SmtpMail oMail = new SmtpMail("TryIt");
        SmtpClient oSmtp = new SmtpClient();

        // Set sender email address, please change it to yours
        oMail.From = "test@emailarchitect.net";

        // Set recipient email address, please change it to yours
        oMail.To = "support@emailarchitect.net";

        // Set email subject
        oMail.Subject = "test html email from C#";

        // Set Html body
        oMail.HtmlBody = "<font size=5>This is</font> <font color=red><b>a test</b></font>";

        // Your SMTP server address
        SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net");

        // User and password for ESMTP authentication, if your server doesn't require
        // User authentication, please remove the following codes.            
        oServer.User = "test@emailarchitect.net";
        oServer.Password = "testpassword";

        // If your smtp server requires SSL connection, please add this line
        // oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

        try
        {
            Console.WriteLine("start to send HTML email ...");
            oSmtp.SendMail(oServer, oMail);
            Console.WriteLine("email was sent successfully!");
        }
        catch (Exception ep)
        {
            Console.WriteLine("failed to send email with the following error:");
            Console.WriteLine(ep.Message);
        }
    }
}

}

答案 3 :(得分:0)

mm.Body = "<p>TEXT TEXT TEXT</p>"+
"<p style='color: green; font-size:16px'>different font and color</p>";
mm.IsBodyHtml = true;