2014-10-29 117 views
-1

如何在使用C#发送邮件之前定制消息?
我想在文本的某个地方断行,或者在消息中设置特定的字体。
任何人都可以帮助我吗?
PICE代码:
如何在发送邮件之前向邮件添加样式?

SEPNA.BLL.Mail mail = new BLL.Mail(); 
     RichTextBox rtb = new RichTextBox(); 
     Font boldfont = new Font("B Nazanin", 14, FontStyle.Bold); 
     rtb.RightToLeft = RightToLeft.Yes; 
     rtb.Font = boldfont; 
     SEPNA.DAL.DBMLs.COM_TbUser user = new SEPNA.DAL.Implementation.UsersDAL().GetUserByUserID(DisplayID); 
     if (mailType == Types.WorkFlowMail.Mail) 
     { 
      switch (step) 
      { 
       case SEPNA.Types.FlowStep.PackageUpload: 
        rtb.Text = MessageBody.Replace("[DisplayName]", user.DisplayName).Replace("[ApplicationName]", AppName).Replace("[ActivityID]", activity).Replace("\n", new StringBuilder().AppendLine().ToString()); 
        Subject = Subject.Replace("[ApplicationName]", AppName); 
        break; 

商祺!

+0

请告诉我们你的代码,你已经尝试过的东西。 – Dave 2014-10-29 14:00:33

回答

1

只需发送邮件为html:

MailMessage mail = new MailMessage(); 
      SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); 

      mail.From = new MailAddress("[email protected]"); 
      mail.To.Add("to_address"); 
      mail.Subject = "Test Mail - 1"; 

      mail.IsBodyHtml = true; 
      string htmlBody; 

      htmlBody = "Write some HTML code here"; 

      mail.Body = htmlBody; 

      SmtpServer.Port = 587; 
      SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password"); 
      SmtpServer.EnableSsl = true; 

      SmtpServer.Send(mail); 
      MessageBox.Show("mail Send"); 
+0

Credit for Google :) – 2014-10-29 14:04:14

+0

我真的很喜欢你的'mail.To.Add(“”)',我不知道它存在。我在每封电子邮件的末尾使用了一个带';'的字符串。 – 2014-10-29 14:06:28

0

MailMessage.IsBodyHtml

您可以设置为true

using (var message = new MailMessage(fromAddress, toAddress) 
{      
    Subject = subject, 
    Body = body, 
    IsBodyHtml = true // this property 
}) 
相关问题