viv*_*vek 10 c# email smtp mailkit asp.net-core
我使用SmtpClient到目前为止使用ASP.NET MVC 5.为了测试本地系统上的电子邮件发送功能,我正在使用 client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
现在,我想在ASP.NET Core中做同样的事情,直到现在还没有实现SmtpClient类.所有搜索结果都在MailKit上结束.我使用了他们的发送邮件代码,它与gmail一起正常工作.
我不想每次都发送测试电子邮件,在我的项目中可能有很多场景我需要发送电子邮件.如何使用MailKit的本地电子邮件发送功能.任何链接或小源代码都会有所帮助.谢谢
jst*_*ast 15
我不确定有关SmtpDeliveryMethod.SpecifiedPickupDirectory工作方式和工作原理的详细信息,但我怀疑它可能只是将邮件保存在本地Exchange服务器定期检查邮件发送的目录中.
假设是这样的话,你可以这样做:
void SaveToPickupDirectory (MimeMessage message, string pickupDirectory)
{
do {
// Note: this will require that you know where the specified pickup directory is.
var path = Path.Combine (pickupDirectory, Guid.NewGuid ().ToString () + ".eml");
if (File.Exists (path))
continue;
try {
using (var stream = new FileStream (path, FileMode.CreateNew)) {
message.WriteTo (stream);
return;
}
} catch (IOException) {
// The file may have been created between our File.Exists() check and
// our attempt to create the stream.
}
} while (true);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码片段Guid.NewGuid ()用作生成临时文件名的方法,但您可以使用您想要的任何方法(例如,您也可以选择使用message.MessageId + ".eml").
基于Microsoft的referenceource,当SpecifiedPickupDirectory使用它们时,它们实际上也在使用Guid.NewGuid ().ToString () + ".eml",所以这可能是要走的路.
| 归档时间: |
|
| 查看次数: |
1817 次 |
| 最近记录: |