我正在尝试阅读/解析Word文档以获取信息。在Word文档中,它包含名称,电子邮件地址,主题,消息(如果它使事物复杂化,现在就删除附件)。我在单独的行上有每个信息,以使事情变得简单。阅读后,它应该将电子邮件派遣到电子邮件地址,并附上相应的主题,消息和附件(如果复杂的(如果复杂))到电子邮件地址。
这也是一个控制台应用程序,processWord.dll将处理和派遣。
对于初学者来说,这是我在program内部的内容。
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);
}
}
}
}
所以,您希望我们在您的书中写代码吗?
首先,我认为使用 Interop 是一个不好的主意。尝试使用打开XML 。互联网中有很多链接和教程。您的代码必须分析一些锚元素(例如名称,电子邮件和到目前为止)以及在找到附近信息时。当然,将与Word文档的工作放在单独的库中是非常明智的,这将提供功能。
之后,检查一些这样的链接,以了解C#中的电子邮件的工作:
- 在这里,在http://stackoverflow.com
- 或随机互联网教程
stackoverflow并不是人们为您工作的地方。在这里,人们帮助您处理困难的情况并与您分享经验。在所有其他情况下,寻求自由职业者网站的帮助。
如果您的数据在单独的行中可以尝试此代码。
这是分布线并保存到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 = "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);
}
}
}
}