2017-02-16 53 views
0

我正在嘗試讀取/解析信息的文檔文檔。在word文檔中,它包含名稱,電子郵件地址,主題,消息(如果它使事情變得複雜,我們暫時不要附件)。我將每個信息放在一個單獨的行上,以使事情變得簡單。閱讀完畢後,應該將電子郵件發送到具有相應主題,消息和附件的電子郵件地址(如果複雜,請留下附件)到電子郵件地址。C# - 處理來自word文檔的信息後發送一封電子郵件

這也是一個控制檯應用程序,其中processWord.dll將處理處理和調度。

對於初學者來說,這是我到目前爲止,我的Program.cs內,通過在文檔中的所有單詞循環和它打印到控制檯:

using Microsoft.Office.Interop.Word; 
using Microsoft.Office.Interop.Excel; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace Project 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application(); 
      Document document = application.Documents.Open("C:\\Users\\name\\Desktop\\word.docx"); 

      int count = document.Words.Count; 
      for (int i = 1; i <= count; i++) 
      { 
       string text = document.Words[i].Text; 
       Console.WriteLine("Word {0} = {1}", i, text); 
      } 
     } 
    } 
} 
+0

那麼你的問題是什麼? –

+0

我如何根據我提供的內容來做到這一點? – canucksfan96

+0

** Google然後嘗試**代碼,如果有任何錯誤,您可以在此放置代碼。 –

回答

1

如果您在不同的行數據就可以試試這個代碼。

這是拆分行並保存到list。因此,您可以使用此list發送郵件。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Web.Mail; 
using Microsoft.Office.Interop.Word; 

namespace Project 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application(); 
     Document document = application.Documents.Open("C:\\Users\\name\\Desktop\\word.docx"); 

     int count = document.Paragraphs.Count; 

     string totaltext = ""; 
     List<string> rows = new List<string>(); 

     for (int i = 1; i <= count; i++) 
     { 
      string temp = doc.Paragraphs[i + 1].Range.Text.Trim(); 
      if (temp != string.Empty) 
       rows.Add(temp); 
     } 
     //WE HAVE ROWS IN WORD DOCUMENT. NOW WE CAN SEND. 
     string mailTo= rows[0]; 
     string name= rows[1]; 
     string subject= rows[2]; 
     string messageBody = rows[3]; 
     string attachmentPath=rows[4]; 
     try 
      { 
       MailMessage oMsg = new MailMessage(); 
       // TODO: Replace with sender e-mail address. 
       oMsg.From = "[email protected]"; 
       // TODO: Replace with recipient e-mail address. 
       oMsg.To = name+"<"+mailTo+">"; 
       oMsg.Subject = subject; 

       // SEND IN HTML FORMAT (comment this line to send plain text). 
       oMsg.BodyFormat = MailFormat.Html; 

       // HTML Body (remove HTML tags for plain text). 
       oMsg.Body = messageBody; 

       // ADD AN ATTACHMENT. 
       // TODO: Replace with path to attachment. 
       String sFile = attachmentPath; 
       MailAttachment oAttch = new MailAttachment(sFile, MailEncoding.Base64); 

       oMsg.Attachments.Add(oAttch); 

       // TODO: Replace with the name of your remote SMTP server. 
       SmtpMail.SmtpServer = "MySMTPServer"; 
       SmtpMail.Send(oMsg); 

       oMsg = null; 
       oAttch = null; 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine("{0} Exception caught.", e); 
      } 

    } 
} 
} 
+0

當我嘗試按照自己的方式進行操作時,出現'COMException was unhandled'異常。完整的信息是:'在Project.exe中發生'System.Runtime.InteropServices.COMException'類型的未處理異常。它抱怨這一行:string temp = document.Paragraphs [i + 1] .Range.Text.Trim(); – canucksfan96

1

那麼,你希望我們寫出代碼嗎?

首先,我認爲使用Interop是一個相當糟糕的主意。嘗試使用打開XML。互聯網上有很多鏈接和教程。您的代碼必須分析文檔中的某些錨點元素(例如名稱,電子郵件以及迄今爲止),並在查找時獲取附近的信息。 當然,將Word文檔放置在某個界面後面的獨立庫中是非常明智的,它將提供功能。

之後,檢查一些像這樣的鏈接,瞭解與電子郵件的工作在C#:

#2是不是人們爲你工作的地方。這是一個人們幫助你處理困難情況並與你分享經驗的地方。在所有其他情況下,請尋求自由職業者網站的幫助。

相關問題