![](/img/trans.png)
[英]How to send a mail in Xamarin using System.Net.Mail.SmtpClient
[英]Send to folder like System.Net.Mail.SmtpClient
我正在使用 MailKit.Net.Smtp.SmtpClient 並且我試圖弄清楚是否有一種方法可以配置此客戶端,以便它將電子郵件放入文件夾,就像PickupDirectoryLocation屬性影響 System.Net 的方式一樣。 Mail.SmtpClient dotNet 實例。 這里需要進行開發和調試,以便我們可以安全地捕獲可能會發出的消息。
我願意接受另一種調試 MailKit 生成的電子郵件的技術,只要它不涉及任何外部服務,如 SMTP 服務器。
這樣做的方法是執行以下操作:
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.