[英]Save and send a mail using System.Net.Mail
我正在嘗試使用C#代碼發送和保存發送電子郵件。 但是我做不到。 我既可以保存郵件,也可以發送郵件。 但是我不能兩者都做。
這就是我所擁有的:
public ActionResult Index()
{
MailMessage message = new MailMessage();
message.From = new MailAddress("test@mail.com");
message.To.Add(new MailAddress("mymail@gmail.com"));
message.Subject = "Test Subject";
message.Body = "This is a test message";
message.IsBodyHtml = true;
// Setup SMTP settings
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
NetworkCredential basicCredential = new NetworkCredential("mymail@gmail.com", "******");
smtp.UseDefaultCredentials = false;
smtp.Credentials = basicCredential;
smtp.Send(message);
// save
smtp.EnableSsl = false;
smtp.PickupDirectoryLocation = @"C:\Temp";
smtp.Send(message);
return View();
}
所以首先我嘗試發送電子郵件。 這樣可行。 然后,我試圖將電子郵件保存到我的硬盤上。 但是,它永遠不會被保存。 當我不發送電子郵件並嘗試立即將其保存到硬盤時,它確實可以工作。 但是我需要兩者都做。
任何人都知道我如何做到這一點? 我只需要記錄發送消息即可。
提取目錄中的郵件由本地SMTP服務器(如果有)自動發送,例如IIS。 ( SmtpClient.PickupDirectoryLocation )
如果要保存到文件系統,則需要將DeliveryMethod
設置為SmtpDeliveryMethod.SpecifiedPickupDirectory
:
client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
client.PickupDirectoryLocation = @"C:\Temp";
client.Send(message);
您必須將屬性DeliveryMethod
更改為SmtpDeliveryMethod.SpecifiedPickupDirectorynot
才能不發送電子郵件。
僅更改PickupDirectoryLocation
將不起作用,因為在DeliveryMethod
設置為Network
(默認值)時不使用該屬性。
參見MSDN 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.