【问题标题】:Hardcoding Text and formatting硬编码文本和格式
【发布时间】:2013-11-07 08:05:12
【问题描述】:

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

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

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

【问题讨论】:

  • mm的类是什么?
  • 您需要设置HTML正文。将MailMessage.IsBodyHtml 设置为真。然后,您可以使用内联样式和样式标签进行简单的 HTML 格式化。
  • 对不起。 mm 类是邮件消息。

标签: c# text-formatting mailmessage


【解决方案1】:

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;

【讨论】:

    【解决方案2】:

    您好,您需要将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
    

    【讨论】:

      【解决方案3】:

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

      }

      【讨论】:

        【解决方案4】:
        mm.Body = "<p>TEXT TEXT TEXT</p>"+
        "<p style='color: green; font-size:16px'>different font and color</p>";
        mm.IsBodyHtml = true;
        

        【讨论】:

          猜你喜欢
          • 2010-11-07
          • 2011-03-09
          • 1970-01-01
          • 2012-03-20
          • 1970-01-01
          • 1970-01-01
          • 2012-01-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多