Hi @Keagan Schultz , Welcome to Microsoft Q&A,
Make sure you use await
for asynchronous calls, configure ImapClient
connections and authentication correctly, improve error handling to provide more detailed output.
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MailKit.Net.Imap;
using MailKit;
using MailKit.Search;
using MimeKit;
using MailKit.Security;
public class Program
{
public static async Task Main(string[] args)
{
string email = "your-email@example.com";
string password = "your-password";
string host = "outlook.live.com";
int port = 993; // Use port 993 for IMAP over SSL
string savePath = "your-save-path";
var emailHandling = new EmailHandling(email, password);
await emailHandling.DownloadAttachmentAsync(host, port, savePath);
}
}
public class EmailHandling
{
private readonly string email;
private readonly string password;
public EmailHandling(string email, string password)
{
this.email = email;
this.password = password;
}
public async Task DownloadAttachmentAsync(string host, int port, string savePath)
{
try
{
using (var client = new ImapClient())
{
using (var cancel = new CancellationTokenSource())
{
await client.ConnectAsync(host, port, SecureSocketOptions.SslOnConnect, cancel.Token);
// Remove unnecessary authentication mechanisms
client.AuthenticationMechanisms.Remove("XOAUTH2");
await client.AuthenticateAsync(email, password, cancel.Token);
var inbox = client.Inbox;
await inbox.OpenAsync(FolderAccess.ReadOnly, cancel.Token);
var uniqueIds = await inbox.SearchAsync(SearchQuery.NotSeen, cancel.Token);
foreach (var uniqueId in uniqueIds.OrderByDescending(id => id.Id).Take(3))
{
var message = await inbox.GetMessageAsync(uniqueId, cancel.Token);
// Check if the email is from a specific address
if (message.From.Mailboxes.Any(m => m.Address.Equals("noreply@dhl.com", StringComparison.OrdinalIgnoreCase)))
{
foreach (var attachment in message.Attachments)
{
if (attachment is MimePart part && part.FileName.EndsWith(".zip", StringComparison.OrdinalIgnoreCase))
{
var fileName = Path.Combine(savePath, part.FileName);
using (var stream = File.Create(fileName))
{
await part.Content.DecodeToAsync(stream, cancel.Token);
}
Console.WriteLine($"Attachment saved: {fileName}");
}
}
}
}
await client.DisconnectAsync(true, cancel.Token);
}
}
}
catch (AuthenticationException ex)
{
Console.WriteLine("Authentication failed.");
Console.WriteLine(ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("An error occurred while downloading the attachment.");
Console.WriteLine(ex.Message);
}
}
}
Make sure to replace "your-email@example.com"
, "your-password"
, and "your-save-path"
with actual values.
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.