2012-08-28 58 views
2

我试图使用C#代码发送并保存发送电子邮件。但我无法完成这件事。我可以保存邮件,也可以发送邮件。但我无法完成这两件事。使用System.Net.Mail保存并发送邮件

这是我有:

public ActionResult Index() 
{ 
    MailMessage message = new MailMessage(); 

    message.From = new MailAddress("[email protected]"); 
    message.To.Add(new MailAddress("[email protected]")); 
    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("[email protected]", "******"); 

    smtp.UseDefaultCredentials = false; 
    smtp.Credentials = basicCredential; 
    smtp.Send(message); 

    // save 
    smtp.EnableSsl = false; 
    smtp.PickupDirectoryLocation = @"C:\Temp"; 
    smtp.Send(message); 

    return View(); 
} 

所以首先我尝试发送电子邮件。这样可行。然后我试图将电子邮件保存到我的硬盘。但它永远不会被保存。当我不发送电子邮件并立即将其保存到我的硬盘时,它确实有效。但我需要这样做。

任何人有任何想法我可以做到这一点?我只需要记录发送消息。

回答

1

你要更改的属性DeliveryMethodSmtpDeliveryMethod.SpecifiedPickupDirectorynot不发送电子邮件。

只是改变了PickupDirectoryLocation将无法​​正常工作,因为当DeliveryMethod设置为Network(这是默认值)不使用的属性。

请参阅MSDN

相关问题