Office 365邮箱邮件获取方案咨询(EWS停用,IMAP4代码异常)
解决Office 365 IMAP4访问邮件时程序无输出直接退出的问题
看起来你的程序虽然返回了退出码0(通常表示正常退出),但实际没执行到邮件读取逻辑,大概率是服务器配置错误或者登录凭据无效导致连接失败,但第三方库没有抛出明显异常。咱们一步步来排查和修正:
首先,你的代码里有几个致命错误
- IMAP服务器地址错误:Office 365的IMAP服务器不是占位符
imap-server-name,正确地址是outlook.office365.com - 邮箱地址错误:你写的是
test@gmail.com,这是Gmail邮箱,不是Office 365的邮箱,必须换成你的Office 365账户(比如yourname@yourcompany.com或yourname@outlook.com) - 登录凭据问题:Office 365现在默认禁用普通密码登录IMAP(尤其是开启了MFA的账户),必须使用应用密码(在微软账户安全中心生成),而非你的账户密码
排查步骤
确认Office 365的IMAP服务已启用
登录Outlook网页版,依次进入「设置」→「邮件」→「同步电子邮件」,找到IMAP选项并确认它是开启状态。生成应用密码(如果开启了MFA)
登录微软账户安全中心,找到「高级安全选项」→「应用密码」,创建一个专门用于IMAP登录的密码,替代代码里的普通密码。添加更详细的日志调试
在代码的关键步骤添加日志,看看程序到底执行到哪一步就停了。比如在连接服务器前后、获取邮件列表前后都加输出,这样能快速定位问题点。
修正后的完整代码示例
using System; using System.IO; using System.Globalization; // 确保你已经引用了对应的邮件库(比如EAGetMail) using EAGetMail; class Program { // Generate an unique 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 { Console.WriteLine("Starting email retrieval process..."); // Create a folder named "inbox" under current directory to save emails string localInbox = Path.Combine(Directory.GetCurrentDirectory(), "inbox"); if (!Directory.Exists(localInbox)) { Directory.CreateDirectory(localInbox); Console.WriteLine($"Created local inbox folder at: {localInbox}"); } // Office 365 IMAP server configuration MailServer oServer = new MailServer( "outlook.office365.com", "your-office365-email@domain.com", // 替换成你的Office 365邮箱 "your-app-password", // 替换成应用密码(或普通密码,仅当未开启MFA且允许非安全应用) ServerProtocol.Imap4); // Enable SSL and set correct port oServer.SSLConnection = true; oServer.Port = 993; MailClient oClient = new MailClient("TryIt"); Console.WriteLine("Connecting to Office 365 IMAP server..."); oClient.Connect(oServer); Console.WriteLine("Successfully connected to server!"); // Retrieve unread emails only oClient.GetMailInfosParam.Reset(); oClient.GetMailInfosParam.GetMailInfosOptions = GetMailInfosOptionType.NewOnly; Console.WriteLine("Fetching unread email list..."); MailInfo[] infos = oClient.GetMailInfos(); Console.WriteLine($"Total {infos.Length} unread email(s)\r\n"); for (int i = 0; i < infos.Length; i++) { MailInfo info = infos[i]; Console.WriteLine($"Index: {info.Index}; Size: {info.Size}; UIDL: {info.UIDL}"); // Receive email from server Console.WriteLine($"Retrieving email {i+1}..."); Mail oMail = oClient.GetMail(info); Console.WriteLine($"From: {oMail.From.ToString()}"); Console.WriteLine($"Subject: {oMail.Subject}\r\n"); // Save email to local disk string fileName = _generateFileName(i + 1); string fullPath = Path.Combine(localInbox, fileName); oMail.SaveAs(fullPath, true); Console.WriteLine($"Saved email to: {fullPath}"); // Mark unread email as read if (!info.Read) { oClient.MarkAsRead(info, true); Console.WriteLine("Marked email as read."); } } // Quit and clean up oClient.Quit(); Console.WriteLine("Completed! Press any key to exit..."); Console.ReadKey(); } catch (Exception ep) { Console.WriteLine($"Error occurred: {ep.Message}"); Console.WriteLine($"Stack trace: {ep.StackTrace}"); Console.ReadKey(); } } }
额外注意事项
- 如果你用的不是EAGetMail库,需要对应调整API,但核心的服务器地址、凭据配置是通用的
- 确保你的项目正确引用了邮件库的DLL文件,版本要兼容你的.NET框架(或.NET Core/.NET 5+)
- 如果还是连接失败,可以尝试用Outlook客户端手动配置IMAP账户,确认凭据和服务器设置正确后再写到代码里
内容的提问来源于stack exchange,提问作者S.Sehgal
相关产品推荐
相关产品推荐

