C#-处理word文档中的信息后发送电子邮件

C#-处理word文档中的信息后发送电子邮件,c#,asp.net,email,C#,Asp.net,Email,我正在尝试读取/解析word文档以获取信息。在word文档中,它包含名称、电子邮件地址、主题、消息(如果它使事情复杂化,我们暂时省略附件)。我把每一条信息都放在单独的一行,让事情变得简单。阅读后,应将电子邮件发送至电子邮件地址,并附上相应的主题、消息和附件(如果复杂,请留下附件) 这也是一个控制台应用程序,processWord.dll将在其中处理处理和分派 对于初学者来说,这就是我目前在Program.cs中的内容,它循环遍历文档中的所有单词并将其打印到控制台: using Microsoft

我正在尝试读取/解析word文档以获取信息。在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);
            }
        }
    }
}
使用Microsoft.Office.Interop.Word;
使用Microsoft.Office.Interop.Excel;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间项目
{
班级计划
{
静态void Main(字符串[]参数)
{
Microsoft.Office.Interop.Word.Application Application=新的Microsoft.Office.Interop.Word.Application();
Document Document=application.Documents.Open(“C:\\Users\\name\\Desktop\\word.docx”);
int count=document.Words.count;

对于(inti=1;i,如果您的数据在单独的行中,您可以尝试此代码

这是拆分行并保存到
列表
。因此您可以使用此
列表
发送邮件

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 = "sender@somewhere.com";
                // 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);
            }

    }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Web.Mail;
使用Microsoft.Office.Interop.Word;
命名空间项目
{
班级计划
{
静态void Main(字符串[]参数)
{
Microsoft.Office.Interop.Word.Application Application=新的Microsoft.Office.Interop.Word.Application();
Document Document=application.Documents.Open(“C:\\Users\\name\\Desktop\\word.docx”);
int count=document.parations.count;
字符串totaltext=“”;
列表行=新列表();

对于(inti=1;i,如果您的数据在单独的行中,您可以尝试此代码

这是拆分行并保存到
列表
。因此您可以使用此
列表
发送邮件

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 = "sender@somewhere.com";
                // 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);
            }

    }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Web.Mail;
使用Microsoft.Office.Interop.Word;
命名空间项目
{
班级计划
{
静态void Main(字符串[]参数)
{
Microsoft.Office.Interop.Word.Application Application=新的Microsoft.Office.Interop.Word.Application();
Document Document=application.Documents.Open(“C:\\Users\\name\\Desktop\\word.docx”);
int count=document.parations.count;
字符串totaltext=“”;
列表行=新列表();

对于(inti=1;i那么,您希望我们为您编写代码吗

首先,我认为使用Interop是一个非常糟糕的主意。尝试使用Open XML。internet上有很多链接和教程。您的代码必须分析文档中的一些锚元素(例如名称、电子邮件和迄今为止的内容),并在找到它时获取附近的信息。 当然,将Word文档放在单独的库中,放在提供功能的界面后面是非常明智的

之后,请查看一些类似以下的链接,以了解如何使用C#中的电子邮件:


Stackoverflow不是人们为您工作的地方。它是人们帮助您处理困难情况并与您分享经验的地方。在所有其他情况下,请向自由职业者网站寻求帮助。

那么,您希望我们为您编写代码吗

首先,我认为使用Interop是一个非常糟糕的主意。尝试使用Open XML。internet上有很多链接和教程。您的代码必须分析文档中的一些锚元素(例如名称、电子邮件和迄今为止的内容),并在找到它时获取附近的信息。 当然,将Word文档放在单独的库中,放在提供功能的界面后面是非常明智的

之后,请查看一些类似以下的链接,以了解如何使用C#中的电子邮件:


Stackoverflow不是人们为你工作的地方。它是人们帮助你处理困难情况并与你分享经验的地方。在所有其他情况下,请向自由职业者网站寻求帮助。

那么你的问题是什么?根据我提供的信息,我将如何做?谷歌然后尝试代码,如果有的话ror,你可以把代码放在这里。那么你的问题是什么?我将如何根据我提供的内容来做?谷歌然后尝试代码,如果有错误,你可以把代码放在这里。当我尝试按你的方式做时,我得到一个“COMException was unhandled”异常。完整的消息是:“System.Runtime.InteropServices.COMException类型的未处理异常”发生在Project.exe中。它在这一行抱怨:string temp=document.parations[i+1].Range.Text.Trim();当我尝试按您的方式执行时,我收到一个“COMException was unhandled”异常。完整消息是:“Project.exe中发生了类型为“System.Runtime.InteropServices.COMException”的未处理异常。它在这一行抱怨:string temp=document.parations[I+1]。Range.Text.Trim();