2016-12-06 110 views
2

我想通過OutlookC#發送Mail但我的Attachments的位置存在問題。我有以下代碼:附件在RTF郵件中的位置

if (strBody.StartsWith(@"{\rtf")) 
{ 
    mailItem.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatRichText;      
    mailItem.RTFBody = Encoding.UTF8.GetBytes(strBody); 

    mailItem.Attachments.Add(strAttachment, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, int.MaxValue, null); 

} 
else 
{ 
    mailItem.Body = strBody; 
    mailItem.Attachments.Add(strAttachment, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, 1, null); 
} 

我strBody具有以下值:

{\ RTF1 \ ANSI \ ansicpg1252 \ deff0 \ deflang1031 {\ fonttbl {\ F0 \ fnil \ fcharset0宋體;}} {\ colortbl; \ red255 \ green0 \ blue128; \ red0 \ green128 \ blue255;} \ viewkind4 \ uc1 \ pard \ fs20 Sehr geehrte \ cf1 Damen \ cf0 und \ cf2 Herren \ cf0,\ par \ par hier ihre AB \ fs20 \ par }

但我的Mail看起來像這樣:

RTF Mail Body

我現在的問題是,

  1. 能否Attachments顯示爲當郵件沒有RTF格式像一個額外的行?
  2. 如果不是1.,那麼我怎樣才能讓我的Attachments顯示在最後?

回答

1

那麼你做的一切都沒錯。每個值> 1都會將附件放在郵件的末尾。在「hier ihre AB」之後,它被放置。看起來很蠢,但很好... 作爲一個小解決方法,我也是這樣使用它,放置一些新的線。儘可能多地將附件放在最後一句話之下。

或者您將郵件編寫爲HTML類型。問題較少。

編輯:

正如你可以看到,該文件被放置在郵件的末尾。

編輯II:

下面是一個方法的例子,發送您的電子郵件作爲HTML與附件行中的附件:

static void Main(string[] args) 
    { 
      Outlook.Application tmpOutlookApp = new Outlook.Application(); 
      Outlook.MailItem tmpMessage = (Outlook.MailItem)tmpOutlookApp.CreateItem(Outlook.OlItemType.olMailItem); 
      tmpMessage.HTMLBody = "Test"; 
      String sDisplayName = "Test"; 
      int iPosition = (int)tmpMessage.Body.Length + 1; 
      int iAttachType = (int)Outlook.OlAttachmentType.olByValue; 
      Outlook.Attachment oAttach = tmpMessage.Attachments.Add(@"C:\Test.txt", iAttachType, iPosition, sDisplayName); 
      tmpMessage.Subject = "Your Subject will go here."; 
      Outlook.Recipients oRecips = (Outlook.Recipients)tmpMessage.Recipients; 
      Outlook.Recipient tmpRecipient = (Outlook.Recipient)oRecips.Add("EMail"); 
      tmpRecipient.Resolve(); 
      tmpMessage.Send(); 
    } 
+0

我看到了這個念頭了'HTML'爲好,但我的文本來自'RichTextBox',所以它在'RTF'中。有沒有將'RTF'轉換爲'HTML'的好方法? –

+0

@ E-Nuff此文檔幫助了我很多:https://www.codeproject.com/kb/recipes/rtfconverter.aspx – Cataklysim

+0

緊緊抓住你。最後一個問題是附件是不是可以在正文中顯示,但是像普通郵件一樣作爲額外的行被嵌入? –