我设置了Outlook Office 365帐户,我想从Outlook邮箱中检索电子邮件,之前我们在代码中使用Exchange Service,但是由于EWS他不再使用Exchange Service,现在我们不再使用Exchange Service在Microsoft中可用。
请帮助我如何在Outlook Office 365帐户中检索电子邮件。
我将附加我创建的示例代码,但无法阅读电子邮件。
在下面的代码中,我尝试通过IMAP4协议服务器检索电子邮件。 运行代码时,出现此错误-ReceiveOutlookMail.exe已退出,代码为0(0x0)。
class Program
{
// Generate an unqiue email file name based on date time
static string _generateFileName(int sequence)
{
DateTime currentDateTime = DateTime.Now;
return string.Format("{0}-{1:000}-{2:000}.eml",
currentDateTime.ToString("yyyyMMddHHmmss", new CultureInfo("en-US")),
currentDateTime.Millisecond,
sequence);
}
static void Main(string[] args)
{
try
{
// Create a folder named "inbox" under current directory
// to save the email retrieved.
string localInbox = string.Format("{0}\\inbox", Directory.GetCurrentDirectory());
// If the folder is not existed, create it.
if (!Directory.Exists(localInbox))
{
Directory.CreateDirectory(localInbox);
}
// Hotmail/MSN IMAP4 server is "imap-mail.outlook.com"
//MailServer oServer = new MailServer("imap-server-name",
// "liveid@hotmail.com", "yourpassword", ServerProtocol.Imap4);
// For office 365 user, please change server to:
MailServer oServer = new MailServer("imap-server-name",
"test@gmail.com", "password", ServerProtocol.Imap4);
// Enable SSL connection.
oServer.SSLConnection = true;
// Set 993 SSL port
oServer.Port = 993;
MailClient oClient = new MailClient("TryIt");
oClient.Connect(oServer);
// retrieve unread/new email only
oClient.GetMailInfosParam.Reset();
oClient.GetMailInfosParam.GetMailInfosOptions = GetMailInfosOptionType.NewOnly;
MailInfo[] infos = oClient.GetMailInfos();
Console.WriteLine("Total {0} unread email(s)\r\n", infos.Length);
for (int i = 0; i < infos.Length; i++)
{
MailInfo info = infos[i];
Console.WriteLine("Index: {0}; Size: {1}; UIDL: {2}",
info.Index, info.Size, info.UIDL);
// Receive email from IMAP4 server
Mail oMail = oClient.GetMail(info);
Console.WriteLine("From: {0}", oMail.From.ToString());
Console.WriteLine("Subject: {0}\r\n", oMail.Subject);
// Generate an unqiue email file name based on date time.
string fileName = _generateFileName(i + 1);
string fullPath = string.Format("{0}\\{1}", localInbox, fileName);
// Save email to local disk
oMail.SaveAs(fullPath, true);
// mark unread email as read, next time this email won't be retrieved again
if (!info.Read)
{
oClient.MarkAsRead(info, true);
}
// if you don't want to leave a copy on server, please use
// oClient.Delete(info);
// instead of MarkAsRead
}
// Quit and expunge emails marked as deleted from IMAP4 server.
oClient.Quit();
Console.WriteLine("Completed!");
}
catch (Exception ep)
{
Console.WriteLine(ep.Message);
}
}
}
}