2010-02-03 15 views
9

Tôi đã sử dụng máy chủ SMTP 127.0.0.1 .Công lỗi tôi nhận được:Không thể có được IIS đón thư mục

System.Net.Mail.SmtpException: Cannot get IIS pickup directory.at System.Net.Mail.IisPickupDirectory.GetPickupDirectory().

Lỗi này xảy ra, khi Email gửi từ ASP web page.But EMail gửi từ ASP.NET trang, lỗi không xảy ra. Plz giúp đỡ.

Trả lời

14

Thật không may, ngoại lệ này được nâng lên khi bất kỳ loại nào xảy ra khi cố gắng xác định vị trí của thư mục đón/nhận thư của IIS/. Một nguyên nhân phổ biến là thiếu dịch vụ IIS SMTP.

Nếu bạn đang gửi thư sử dụng System.Net.Mail.SmtpClient, hãy thử đặt thư mục đón bằng tay:

// C# 
var client = new SmtpClient(); 
client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory; 
client.PickupDirectoryLocation = ...; 
client.Send(...); 

Hoặc thiết lập này trong ASP.NET của Web.config thay vì:

<configuration> 
    <system.net> 
     <mailSettings> 
      <smtp deliveryMethod="SpecifiedPickupDirectory"> 
       <specifiedPickupDirectory 
        pickupDirectoryLocation="..." /> 
       <network defaultCredentials="false" /> 
      </smtp> 
     </mailSettings> 
    </system.net> 
</configuration> 

Thay vào đó, hãy sử dụng phương thức SmtpDeliveryMethod.Network và gửi các thuộc tính HostPort đến máy chủ SMTP của bạn.

Thông tin thêm: http://forums.iis.net/p/1149338/1869548.aspx

+1

bạn có thể làm rõ thêm về PickupDirectoryLocation? những gì giá trị chúng ta phải cung cấp để sử dụng PickupDirectoryLocation? – ManirajSS

+1

PickupDirectoryLocation nên được đặt thành đường dẫn đến thư mục được dịch vụ IIS SMTP sử dụng (hoặc dịch vụ bên thứ ba tương thích) làm thư mục đón - tất cả các e-mail được lưu vào thư mục sẽ được gửi đi. Ví dụ: có thể là "C: \ Inetpub \ mailroot \ Pickup". –

9

Thư mục đón được lưu trữ trong II6 Metabase, vì vậy nếu tài khoản mà ứng dụng web của bạn chạy như không có quyền truy cập vào các nút cần thiết, lỗi này có thể được ném (có này riêng tôi). quyền metabase là riêng biệt từ quyền tập tin, vì vậy bạn khám phá nó với metabase explorer:

http://www.microsoft.com/downloads/details.aspx?FamilyID=56fc92ee-a71a-4c73-b628-ade629c89499&displaylang=en (phần mình của bộ tài nguyên IIS)

Những nút cần phải có sự cho phép đọc dành cho người sử dụng web-ứng dụng của bạn: \ LM \ SmtpSvc \ LM \ SmtpSvc \ 1

+0

Tôi nên đề cập đến rằng tôi cũng đã phải sửa chữa quyền truy cập tập tin để có được điều này để làm việc. – Peter

+0

Điều này thật tuyệt và bây giờ tôi có thể thấy/hiểu vấn đề trên hộp Windows 2008 của tôi Tôi đang sử dụng Ứng dụng Nhận dạng Hồ bơi và tôi không thể sử dụng GUI để đặt quyền cho tài khoản người dùng đó. Trước đây tôi đã phải thiết lập quyền truy cập tập tin từ dòng lệnh để thực hiện việc này. Bạn có biết nếu nó có thể làm tương tự cho các điều khoản này? – ProNotion

3

Tôi đã gặp lỗi này trên Windows 7 với mã hoạt động tốt trên XP. Sau nhiều lần thử và lỗi. Tôi thiết lập IIS để lưu trữ thư trong thư mục pickup. Nhưng tôi vẫn có lỗi.

Trong mã của tôi, tôi nhận xét ra dòng:

client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

Xóa dòng mã này đã hoạt động, không chắc chắn lý do. Hy vọng nó làm việc cho bạn quá bởi vì vấn đề này là một thời gian thực thải để troublshoot.

Tôi KHÔNG phải thay đổi bất kỳ quyền nào trên thư mục. Tôi KHÔNG phải sửa đổi metabase. Tôi không phải sửa đổi web.config (mà tôi thực sự không muốn làm vì tôi chỉ muốn các email được đặt vào một thư mục trong khi tôi đang phát triển trên máy cục bộ của mình, không phải trong sản xuất - tôi đã không muốn hai tệp web.config khác nhau duy trì).

+0

Chỉ cần nói tiếng chim ưng để nói rằng đây là những gì làm việc cho tôi. Tôi đã cập nhật dự án VS2004 cho VS2010 và gửi email từ chối làm việc cho đến khi dòng này bị xóa. – spamguy

0

Bạn cũng có thể xác định nó cho dự án của bạn 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;  
    } 

Và mã kiểm tra của bạn:

[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("[email protected]", "Tester"); 
    MailAddress mailTo = new MailAddress("[email protected]", "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."); 
} 

Tạo file app.config trong dự án unittest của bạn nếu không tồn tại hoặc hợp nhất những dòng này với app.config hiện có.

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <system.net> 
    <mailSettings> 
     <smtp deliveryMethod="SpecifiedPickupDirectory"> 
     <specifiedPickupDirectory pickupDirectoryLocation="d:\temp\Emails\" /> 
     </smtp> 
    </mailSettings> 
    </system.net> 
</configuration> 
Các vấn đề liên quan