2016-01-24 163 views
1

我想知道,我怎樣才能將創建的PDF文件與Windows窗體數據附加到新的Outlook電子郵件消息中。用下面的代碼創建PDF。我可以在同其他括號寫:C#創建附帶創建的PDF文件的Outlook消息

else { 
CreatePDF(); 
//This is an example EmailMessage(); 
} 

private void EmailMessage() 
    { 
     try 
     {     
      Outlook.MailItem mailItem = (Outlook.MailItem) 
       this.CreateItem(Outlook.OlItemType.olMailItem); 
      mailItem.Subject = "This is the subject"; 
      mailItem.To = "[email protected]"; 
      mailItem.Body = "This is the message."; 
      mailItem.Importance = Outlook.OlImportance.olImportanceLow; 
      mailItem.Display(false); 
     } 
     catch (Exception eX) 
     { 
      throw new Exception("cDocument: Error occurred trying to Create an Outlook Email" 
           + Environment.NewLine + eX.Message); 
     } 
    } 

public void CreatePdf() 
{ 

    try 
    { 
     string imageFile = ""; 
     System.Drawing.Rectangle bounds = this.Bounds; 
     using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height)) 
     { 
      using (Graphics g = Graphics.FromImage(bitmap)) 
      { 
       g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size); 
       //pictureBox1.Image(); 
      } 
      //==================add firstname 
      if (textBox1.Text != null && textBox1.Text.Trim() != "") 
      { imageFile = "C:/Users/Marc/Desktop/" + DateTime.Now.ToString("dddd dd MMMM hh-mm-ss tt") + textBox1.Text; } 
      else 
      { imageFile = "C:/Users/Marc/Desktop/" + DateTime.Now.ToString("dddd dd MMMM hh-mm-ss tt") + "Rectangle"; } 


      bitmap.Save(imageFile + ".bmp", ImageFormat.Bmp); 
      formToImage(imageFile); 
     } 
    } 
    catch (Exception e) 
    { 
     MessageBox.Show(e.Message.ToString()); 
    } 
}` 
+0

CreatePDF的輸出是什麼?它是文件系統中的文件嗎?或內存中的一個字節[]? –

+0

你正在創建什麼類型的應用程序?一個Outlook插件或一個獨立的應用程序? –

+0

一個獨立的Windows C#應用程序開發與視覺工作室2015. – NinjaCoder

回答

0

將附件添加到MailItem,你可以使用Attachments.Add這樣的:

mailItem.Attachments.Add(filename); 

其中filename是的文件系統上的路徑文件,你想添加。

在你的情況下,這將是imageFile + ".bmp"

+0

我會試試這個,但你能告訴我,我可以下載程序集與以下命名空間?: 使用Outlook = Microsoft .Office.Interop.Outlook; – NinjaCoder

+0

如果您安裝了Outlook,您可以通過添加引用 - > COM –

+0

簡單地添加對Microsoft Outlook XX.0對象庫的引用。但是,您應該已經擁有此代碼,因爲您的代碼使用了「Outlook.MailItem」 –