使用 MailKit 发送邮件到 SpecifiedPickupDirectory
Sending Email to SpecifiedPickupDirectory with MailKit
到目前为止,我一直在使用 SmtpClient 和 ASP.NET MVC 5。为了在本地系统上测试电子邮件发送功能,我使用的是 client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
现在,我想在 ASP.NET Core 中做同样的事情,它到现在还没有实现 SmtpClient class。对此的所有搜索都在 MailKit 结束。我已经使用了他们的发送邮件代码,它在 gmail 上运行良好。
我不想每次都发测试邮件,我的项目中可能有很多场景需要发邮件。如何使用 MailKit 的本地电子邮件发送功能。任何链接或少量源代码都会有所帮助。谢谢
我不确定 SmtpDeliveryMethod.SpecifiedPickupDirectory
的工作原理和具体功能的更详细信息,但我怀疑它可能只是将邮件保存在本地 Exchange 服务器定期检查邮件到的目录中发出去。
假设是这种情况,您可以这样做:
public static void SaveToPickupDirectory (MimeMessage message, string pickupDirectory)
{
do {
// Generate a random file name to save the message to.
var path = Path.Combine (pickupDirectory, Guid.NewGuid ().ToString () + ".eml");
Stream stream;
try {
// Attempt to create the new file.
stream = File.Open (path, FileMode.CreateNew);
} catch (IOException) {
// If the file already exists, try again with a new Guid.
if (File.Exists (path))
continue;
// Otherwise, fail immediately since it probably means that there is
// no graceful way to recover from this error.
throw;
}
try {
using (stream) {
// IIS pickup directories expect the message to be "byte-stuffed"
// which means that lines beginning with "." need to be escaped
// by adding an extra "." to the beginning of the line.
//
// Use an SmtpDataFilter "byte-stuff" the message as it is written
// to the file stream. This is the same process that an SmtpClient
// would use when sending the message in a `DATA` command.
using (var filtered = new FilteredStream (stream)) {
filtered.Add (new SmtpDataFilter ());
// Make sure to write the message in DOS (<CR><LF>) format.
var options = FormatOptions.Default.Clone ();
options.NewLineFormat = NewLineFormat.Dos;
message.WriteTo (options, filtered);
filtered.Flush ();
return;
}
}
} catch {
// An exception here probably means that the disk is full.
//
// Delete the file that was created above so that incomplete files are not
// left behind for IIS to send accidentally.
File.Delete (path);
throw;
}
} while (true);
}
上面的代码片段使用 Guid.NewGuid ()
作为生成临时文件名的方式,但您可以使用任何您想要的方法(例如,您也可以选择使用 message.MessageId + ".eml"
)。
基于微软的referencesource,当使用SpecifiedPickupDirectory
时,他们实际上也使用Guid.NewGuid ().ToString () + ".eml"
,所以这可能是要走的路。
到目前为止,我一直在使用 SmtpClient 和 ASP.NET MVC 5。为了在本地系统上测试电子邮件发送功能,我使用的是 client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
现在,我想在 ASP.NET Core 中做同样的事情,它到现在还没有实现 SmtpClient class。对此的所有搜索都在 MailKit 结束。我已经使用了他们的发送邮件代码,它在 gmail 上运行良好。
我不想每次都发测试邮件,我的项目中可能有很多场景需要发邮件。如何使用 MailKit 的本地电子邮件发送功能。任何链接或少量源代码都会有所帮助。谢谢
我不确定 SmtpDeliveryMethod.SpecifiedPickupDirectory
的工作原理和具体功能的更详细信息,但我怀疑它可能只是将邮件保存在本地 Exchange 服务器定期检查邮件到的目录中发出去。
假设是这种情况,您可以这样做:
public static void SaveToPickupDirectory (MimeMessage message, string pickupDirectory)
{
do {
// Generate a random file name to save the message to.
var path = Path.Combine (pickupDirectory, Guid.NewGuid ().ToString () + ".eml");
Stream stream;
try {
// Attempt to create the new file.
stream = File.Open (path, FileMode.CreateNew);
} catch (IOException) {
// If the file already exists, try again with a new Guid.
if (File.Exists (path))
continue;
// Otherwise, fail immediately since it probably means that there is
// no graceful way to recover from this error.
throw;
}
try {
using (stream) {
// IIS pickup directories expect the message to be "byte-stuffed"
// which means that lines beginning with "." need to be escaped
// by adding an extra "." to the beginning of the line.
//
// Use an SmtpDataFilter "byte-stuff" the message as it is written
// to the file stream. This is the same process that an SmtpClient
// would use when sending the message in a `DATA` command.
using (var filtered = new FilteredStream (stream)) {
filtered.Add (new SmtpDataFilter ());
// Make sure to write the message in DOS (<CR><LF>) format.
var options = FormatOptions.Default.Clone ();
options.NewLineFormat = NewLineFormat.Dos;
message.WriteTo (options, filtered);
filtered.Flush ();
return;
}
}
} catch {
// An exception here probably means that the disk is full.
//
// Delete the file that was created above so that incomplete files are not
// left behind for IIS to send accidentally.
File.Delete (path);
throw;
}
} while (true);
}
上面的代码片段使用 Guid.NewGuid ()
作为生成临时文件名的方式,但您可以使用任何您想要的方法(例如,您也可以选择使用 message.MessageId + ".eml"
)。
基于微软的referencesource,当使用SpecifiedPickupDirectory
时,他们实际上也使用Guid.NewGuid ().ToString () + ".eml"
,所以这可能是要走的路。