我一直在使用Smtp服务器127.0.0.1。我得到错误:
System.Net.Mail.SmtpException: Cannot get IIS pickup directory.at System.Net.Mail.IisPickupDirectory.GetPickupDirectory().
当从ASP网页发送电子邮件时发生此错误。但是从ASP.NET页面发送电子邮件时,未发生错误。 Plz帮助。
答案 0 :(得分:14)
不幸的是,在尝试确定IIS / SMTP拾取目录的位置时发生任何类问题时会引发此异常。常见原因是缺少IIS SMTP服务。
如果您使用System.Net.Mail.SmtpClient发送邮件,请尝试手动设置取件目录:
// C#
var client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
client.PickupDirectoryLocation = ...;
client.Send(...);
或者在ASP.NET的Web.config中设置它:
<configuration>
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory
pickupDirectoryLocation="..." />
<network defaultCredentials="false" />
</smtp>
</mailSettings>
</system.net>
</configuration>
或者,改为使用SmtpDeliveryMethod.Network
方法,并将Host
和Port
属性发送到您的SMTP服务器。
答案 1 :(得分:9)
拾取目录存储在II6元数据库中,因此如果您的网络应用程序运行的帐户无法访问所需的节点,则可能会抛出此错误(我自己)。元数据库权限与文件权限分开,因此您可以使用Metabase explorer进行探索:
http://www.microsoft.com/downloads/details.aspx?FamilyID=56fc92ee-a71a-4c73-b628-ade629c89499&displaylang=en(它是IIS资源工具包的一部分)
这些节点需要具有为您的网络应用用户提供的读取权限: \ LM \ SMTPSVC \ LM \ SMTPSVC \ 1
答案 2 :(得分:3)
我在Windows 7上遇到同样的错误,代码在XP上运行良好。经过多次试验和错误。我设置IIS将邮件存储在一个提取目录中。但我仍然有错误。
在我的代码中,我注释掉了这一行:
client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
删除这行代码有效,不确定原因。希望它也适合你,因为这个问题是一个真正的麻烦,麻烦。
我没有必要更改目录的任何权限。 我没有必要修改配置数据库。 我没有必要修改web.config(我真的不想这样做,因为我只想在我在本地机器上进行开发时将电子邮件放到目录中,而不是在生产中 - 我不想要两个不同的web.config文件来维护)。
答案 3 :(得分:0)
您也可以为您的unittest项目指定它:
public enum TestContextKeys { EmailPickupDirectory, ... };
[TestClass]
public class AssemblyInitializer
{
[AssemblyInitialize]
public static void Init(TestContext testContext)
{
string configPath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
XDocument xmlConfig = XDocument.Load(configPath);
var emailPickupDirectory = xmlConfig.Element("configuration")
.Element("system.net")
.Element("mailSettings")
.Element("smtp")
.Element("specifiedPickupDirectory")
.Attribute("pickupDirectoryLocation")
.Value;
testContext.Properties[TestContextKeys.EmailPickupDirectory.ToString()] = emailPickupDirectory;
}
您的测试代码:
[TestMethod]
public void TestEmailRegistration()
{
MyApp app = new MyApp();
app.RegisterUser("Johny Cash",...);
string emailPickupDirectory = (string)_testContext.Properties[TestContextKeys.EmailPickupDirectory.ToString()];
string[] allEmails = Directory.GetFiles(emailPickupDirectory);
string[] recentEmails = allEmails.Where(e => new FileInfo(e).CreationTime.AddMinutes(1) > DateTime.Now).ToArray();
//check that the registration email was sent
foreach (var email in recentEmails)
{
string content = File.ReadAllText(email);
if (content.Contains("Johny Cash") && content.Contains("successful") && content.Contains("registration"))
{
File.Delete(email);
return;//OK found
}
}
Assert.Fail("Registratoin email has not been sent to Johny Cash");
}
[TestMethod]
public void TestEmailPickupDirectoryConfiguration()
{
string emailPickupDirectory = (string)_testContext.Properties[TestContextKeys.EmailPickupDirectory.ToString()];
MailAddress mailFrom = new MailAddress("testemailpickupdirectory@example.com", "Tester");
MailAddress mailTo = new MailAddress("testemailpickupdirectory@testing.com", "Tester2");
string subject = "Test Message TestEmailPickupDirectory";
using (SmtpClient sc = new SmtpClient())
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(mailTo);
mail.Subject = subject;
mail.From = mailFrom;
mail.IsBodyHtml = true;
mail.BodyEncoding = System.Text.Encoding.GetEncoding("ISO-8859-9");
mail.Body = "<html><body>";
mail.Body += "TestEmailPickupDirectory";
mail.Body += "</body></html>";
sc.Send(mail);
}
string[] allEmails = Directory.GetFiles(emailPickupDirectory);
string[] recentEmails = allEmails.Where(e => new FileInfo(e).CreationTime.AddMinutes(1) > DateTime.Now).ToArray();
foreach (var email in recentEmails)
{
string content = File.ReadAllText(email);
if (content.Contains(mailFrom.Address) && content.Contains(mailTo.Address) && content.Contains(subject))
{
File.Delete(email);
return;//OK found
}
}
Assert.Fail("EmailPickupDirectory configuration may be wrong.");
}
如果不存在,请在您的unittest项目中创建app.config文件,或者将这些行与现有的app.config合并。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="d:\temp\Emails\" />
</smtp>
</mailSettings>
</system.net>
</configuration>