如何阅读和解析(主题,正文,附件......)来自Outlook的电子邮件(添加C#) [英] How to read and parse (subject, body, attachements …) an email from Outlook (add in C#)
本文介绍了如何阅读和解析(主题,正文,附件......)来自Outlook的电子邮件(添加C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为了阅读和解析(主题,正文,附件......)来自Outlook的电子邮件(添加C#),我尝试编写此功能:
For read and parse (subject, body, attachments ...) an email from Outlook (add in C#), I tried to write this function :
using Outlook = Microsoft.Office.Interop.Outlook;
public void readEmail()
{
try
{
Outlook.Application myApp = new Outlook.ApplicationClass();
Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
MessageBox.Show(myInbox.Items.Count + "");
//Read all the email one by one
//for (int i = myInbox.Items.Count; i >= (myInbox.Items.Count-1); i--)
//{
//String strSubject = ((Outlook.MailItem)myInbox.Items[i]).Subject;
// Sender Email
//string senderEmailid = ((Outlook.MailItem)myInbox.Items[i]).SenderEmailAddress;
//string CreationTime=(( //Outlook.MailItem)myInbox.Items[i]).CreationTime.ToString();
//string strEmailBody=(( Outlook.MailItem)myInbox.Items[i]).Body;
//string strEmailSenderName=(( Outlook.MailItem)myInbox.Items[i]).SenderName;
//}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
但MyInbox.Items.Count为0,如果我尝试显示一个变量(例如:strEmailBody),它给出了一个例外:数组索引超出界限。
有人可以帮我吗???
but "MyInbox.Items.Count" is 0 and if I try to display one of the variables (ex : strEmailBody), it gives an exception : Array index out of bound.
Can someone help me please ???
推荐答案
Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.ApplicationClass();
Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
Microsoft.Office.Interop.Outlook.MAPIFolder myContacts = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts);
//login
mapiNameSpace.Logon(null, null, false, false);
mapiNameSpace.Logon("YourOutlookMailID", "Password", Missing.Value, true);
Microsoft.Office.Interop.Outlook.Items myItems = myContacts.Items;
// Console.WriteLine("Total : ", myItems.Count);
Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
// Console.Write(myInbox.Name);
Microsoft.Office.Interop.Outlook.Items inboxItems = myInbox.Items;
// Console.WriteLine("Total : ", inboxItems.Count);
Microsoft.Office.Interop.Outlook.Explorer myexp = myInbox.GetExplorer(false);
mapiNameSpace.Logon("Profile", Missing.Value, false, true);
if (myInbox.Items.Count > 0)
{
Console.WriteLine(string.Format("Total Unread message {0}:", inboxItems.Count));
int x=0;
foreach (Microsoft.Office.Interop.Outlook.MailItem item in inboxItems)
{
Console.WriteLine("{0} unread mail from your inbox", ++x);
Console.WriteLine(string.Format("from:- {0}", item.SenderName));
Console.WriteLine(string.Format("To:- {0}", item.ReceivedByName));
Console.WriteLine(string.Format("Subject:- {0}", item.Subject));
Console.WriteLine(string.Format("Message:- {0}", item.Body));
System.Threading.Thread.Sleep(1000);
}
Console.ReadKey();
}
<pre></pre>
快乐编码:) :) :)
happy coding :) :) :)
这篇关于如何阅读和解析(主题,正文,附件......)来自Outlook的电子邮件(添加C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文