2014-10-29 88 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 
}) 
相關問題