Cũng như thông tin SpecifiedPickupDirectory
của các câu trả lời khác, nếu bạn muốn đảm bảo email của mình được gửi đến thư mục có liên quan đến gốc trang - tiện dụng trong thử nghiệm trên máy chủ xây dựng mà bạn không biết đường dẫn - bạn có thể thêm một kiểm tra nhanh chóng trong gửi mã email của bạn:
SmtpClient client = new SmtpClient();
...
// Add "~" support for pickupdirectories.
if (client.DeliveryMethod == SmtpDeliveryMethod.SpecifiedPickupDirectory && client.PickupDirectoryLocation.StartsWith("~"))
{
string root = AppDomain.CurrentDomain.BaseDirectory;
string pickupRoot = client.PickupDirectoryLocation.Replace("~/", root);
pickupRoot = pickupRoot.Replace("/",@"\");
client.PickupDirectoryLocation = pickupRoot;
}
Và kiểm tra của bạn sẽ giống như thế này (chắc chắn rằng bạn sử dụng App_Data để IIS có thể ghi vào thư mục):
// Arrange - get SitePath from AppDomain.Current.BaseDirectory + ..\
string pickupPath = Path.Combine(SitePath, "App_Data", "TempSmtp");
if (!Directory.Exists(pickupPath))
Directory.CreateDirectory(pickupPath);
foreach (string file in Directory.GetFiles(pickupPath, "*.eml"))
{
File.Delete(file);
}
// Act (send some emails)
// Assert
Assert.That(Directory.GetFiles(pickupPath, "*.eml").Count(), Is.EqualTo(1));
Nguồn
2012-12-08 16:19:29
"thay vì gửi". – Avram