Skip to content

When using a pickup directory for MailKit, return the eml file GUID #359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/Senders/FluentEmail.MailKit/MailKitSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ public async Task<SendResponse> SendAsync(IFluentEmail email, CancellationToken?
{
if (_smtpClientOptions.UsePickupDirectory)
{
await this.SaveToPickupDirectory(message, _smtpClientOptions.MailPickupDirectory);
var messageId = await this.SaveToPickupDirectory(message, _smtpClientOptions.MailPickupDirectory);
response.MessageId = messageId;
return response;
}

Expand Down Expand Up @@ -156,20 +157,24 @@ await client.ConnectAsync(
/// </summary>
/// <param name="message">Message to save for pickup.</param>
/// <param name="pickupDirectory">Pickup directory.</param>
private async Task SaveToPickupDirectory(MimeMessage message, string pickupDirectory)
private async Task<string> SaveToPickupDirectory(MimeMessage message, string pickupDirectory)
{
// Note: this will require that you know where the specified pickup directory is.
var path = Path.Combine(pickupDirectory, Guid.NewGuid().ToString() + ".eml");
var messageId = Guid.NewGuid().ToString();
var path = Path.Combine(pickupDirectory, messageId + ".eml");

if (File.Exists(path))
return;
return null;

try
{
//create the directory since it might not exist
Directory.CreateDirectory(pickupDirectory);

using (var stream = new FileStream(path, FileMode.CreateNew))
{
await message.WriteToAsync(stream);
return;
return messageId;
}
}
catch (IOException)
Expand Down