使用 Outlook SMTP 从 mvc 应用程序发送电子邮件 asp.net



>我尝试使用以下代码通过我的 asp.net mvc应用程序发送电子邮件:

public ActionResult ResetPassword()
    {
        if (Session["id"] == null)
        {
            return RedirectToAction("Login", "Home");
        }
        bool status = false;
        B4URepository dalObj = new B4URepository();
        var customer = dalObj.GetCustomerDetails(Convert.ToInt32(Session["id"]));
        string email = customer.EmailId;
        string name = customer.Name;
        MailMessage mailMessage = new MailMessage();
        mailMessage.To.Add(new MailAddress(email, name));
        mailMessage.From = new MailAddress("sender-outlook-email");
        //mailMessage.Sender = new MailAddress("sender-outlook-email", "sender-name");
        mailMessage.Subject = "<h3>Password reset</h3>";
        mailMessage.Body = "Hello";
        mailMessage.IsBodyHtml = true;
        mailMessage.Priority = MailPriority.High;
        SmtpClient smtp = new SmtpClient();
        try
        {
            smtp.Send(mailMessage);
            status = true;
        }
        catch (Exception)
        {
            status = false;
        }
        if (status)
            return View("Success");
        else
            return View("Error");
    }

以及 Web.config 中的以下设置:

<system.net>
<mailSettings>
  <smtp from="sender-outlook-email">
    <network host="smtp-mail.outlook.com"
             port="587"
             userName="sender-outlook-email"
             password="sender-password"
             enableSsl="true"/>
  </smtp>
</mailSettings>
<!--<mailSettings>
  <smtp deliveryMethod="SpecifiedPickupDirectory">
    <specifiedPickupDirectory pickupDirectoryLocation="Z:MailDump"/>
  </smtp>
</mailSettings>-->

使用注释代码,即邮件设置,我检查了电子邮件是否至少成功创建,并发现它被创建并存储在指定的目录中。

但是在尝试使用上述代码发送邮件时,它会在"smtp.发送(邮件(" :

    [SocketException (0x274c): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 65.55.163.152:587]
   System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) +185
   System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) +506
[WebException: Unable to connect to the remote server]
   System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6) +6525976
   System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback) +302
   System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) +23
   System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) +328
   System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) +141
   System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) +236
   System.Net.Mail.SmtpClient.GetConnection() +45
   System.Net.Mail.SmtpClient.Send(MailMessage message) +1574
[SmtpException: Failure sending mail.]
   System.Net.Mail.SmtpClient.Send(MailMessage message) +1916
   B4UMVC.Controllers.CustomerController.ResetPassword() in C:Userszainab.TRNDesktopB4USol_integratedB4USol_new2B4USolB4UMVCControllersCustomerController.cs:755
   lambda_method(Closure , ControllerBase , Object[] ) +62
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +157
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
   System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +22
   System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
   System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +50
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +225
   System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +26
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9744373
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
您需要

指定client nameport number,然后将UseDefaultCredentials设置为 false,如果您使用的是您的凭据以外的其他凭据(登录凭据(

SmtpClient client = new SmtpClient("smtp-mail.outlook.com");
client.Port = 587;
client.UseDefaultCredentials = false;

这应该有效。

最新更新