我與「臨ASP.NET MVC 4(專業Apress出版)」和現在正在學習當我想編譯我的項目會收到以下錯誤:我無法發送電子郵件的SMTP錯誤
SmtpException was unhandled by user code
An exception of type 'System.Net.Mail.SmtpException' occurred in System.dll but was not handled in user code Additional information: Failure sending mail.
這是源代碼:
using System.Net.Mail;
using System.Text;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Entities;
using System.Net;
namespace SportsStore.Domain.Concrete
{
public class EmailSettings
{
public string MailToAddress = "[email protected]";
public string MailFromAddress = "[email protected]";
public bool UseSsl = true;
public string Username = "[email protected]";
public string Password = "abcd12";
public string ServerName = "smtp.wp.pl";
public int ServerPort = 465;
public bool WriteAsFile = false;
public string FileLocation = @"c:\sports_store_emails";
}
public class EmailOrderProcessor : IOrderProcessor
{
private EmailSettings emailSettings;
public EmailOrderProcessor(EmailSettings settings)
{
emailSettings = settings;
}
public void ProcessOrder(Cart cart, ShippingDetails shippingInfo)
{
using (var smtpClient = new SmtpClient())
{
smtpClient.EnableSsl = emailSettings.UseSsl;
smtpClient.Host = emailSettings.ServerName;
smtpClient.Port = emailSettings.ServerPort;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials
= new NetworkCredential(emailSettings.Username,
emailSettings.Password);
if (emailSettings.WriteAsFile)
{
smtpClient.DeliveryMethod
= SmtpDeliveryMethod.SpecifiedPickupDirectory;
smtpClient.PickupDirectoryLocation = emailSettings.FileLocation;
smtpClient.EnableSsl = false;
}
StringBuilder body = new StringBuilder()
.AppendLine("Nowe zamówienie")
.AppendLine("---")
.AppendLine("Produkty:");
foreach (var line in cart.Lines)
{
var subtotal = line.Product.Price * line.Quantity;
body.AppendFormat("{0} x {1} (wartość: {2:c}", line.Quantity,
line.Product.Name,
subtotal);
}
body.AppendFormat("Wartość całkowita: {0:c}", cart.ComputeTotalValue())
.AppendLine("---")
.AppendLine("Wysyłka dla:")
.AppendLine(shippingInfo.Name)
.AppendLine(shippingInfo.Line1)
.AppendLine(shippingInfo.Line2 ?? "")
.AppendLine(shippingInfo.Line3 ?? "")
.AppendLine(shippingInfo.City)
.AppendLine(shippingInfo.State ?? "")
.AppendLine(shippingInfo.Country)
.AppendLine(shippingInfo.Zip)
.AppendLine("---")
.AppendFormat("Pakowanie prezentu: {0}",
shippingInfo.GiftWrap ? "Tak" : "Nie");
MailMessage mailMessage = new MailMessage(
emailSettings.MailFromAddress, // od
emailSettings.MailToAddress, // do
"Otrzymano nowe zamówienie!", // temat
body.ToString()); // treść
if (emailSettings.WriteAsFile)
{
mailMessage.BodyEncoding = Encoding.ASCII;
}
smtpClient.Send(mailMessage);
}
}
}
}
任何想法如何解決這個問題?
這是excrption細節:
System.Net.Mail.SmtpException was unhandled by user code
HResult=-2146233088 Message=Failure sending mail. Source=System
StackTrace: at System.Net.Mail.SmtpClient.Send(MailMessage message) at SportsStore.Domain.Concrete.EmailOrderProcessor.ProcessOrder(Cart cart, ShippingDetails shippingInfo) in C:\Users\rafal\Desktop\Rozdzial07-11\SportsStore\SportsStore.Domain\Concrete\EmailOrderProcessor.cs:line 93 at SportsStore.WebUI.Controllers.CartController.Checkout(Cart cart, ShippingDetails shippingDetails) in C:\Users\rafal\Desktop\Rozdzial07-11\SportsStore\SportsStore.WebUI\Controllers\CartController.cs:line 39 at lambda_method(Closure , ControllerBase , Object[]) at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2 parameters) at System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeSynchronousActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8
1.b__7(IAsyncResult _) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End() at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag) at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.b__33() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.b__49() InnerException: HResult=-2147024893 Message=Could not find a part of the path 'c:\sports_store_emails\c4734a39-5267-4199-bb29-f28542b3c5ac.eml'. Source=mscorlib StackTrace: at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode) at System.Net.Mail.SmtpClient.GetFileMailWriter(String pickupDirectory) at System.Net.Mail.SmtpClient.Send(MailMessage message) InnerException:
這2個警告:
- warrnig
Warning CS0618 'FormsAuthentication.Authenticate(string, string)' is obsolete: 'The recommended alternative is to use the Membership APIs, such as Membership.ValidateUser. For more information, see http://go.microsoft.com/fwlink/?LinkId=252463 .' SportsStore.WebUI C:\Users\rafal\Desktop\Rozdzial07-11\SportsStore\SportsStore.WebUI\Infrastructure\Concrete\FormsAuthProvider.cs 10 Active
- Warrning
Warning Found conflicts between different versions of the same dependent assembly that could not be resolved. These reference conflicts are listed in the build log when log verbosity is set to detailed. SportsStore.WebUI
如何解決這個問題?
看看內部異常'找不到路徑「一部分C:\ sports_store_emails \ c4734a39-5267-4199-bb29-f28542b3c5ac.eml'.'檢查應用程序池的權限 - 可以了讀/寫該位置? – Jamiec
正確檢查路徑! – RajeeshMenoth
Thx這是權限問題 – jakzyc