2016-07-27 4 views
1

私は自分のサイトをローカルで実行しており、「お問い合わせ」ページの機能をテストしています。電子メールは受信側で決して通過しませんが、エクスクエストがスローされません。ユーザーがフォームを送信すると、ここでは(私はhttp://www.mikesdotnetting.com/article/268/how-to-send-email-in-asp-net-mvcからこのコードのほとんどを適応)呼び出されるエンドポイントである:MVC MailMessageが送信されず、例外がスローされません。

[HttpPost] 
public async Task<ActionResult> contact(ContactUsViewModel inputModel) 
{ 
    try 
    { 
     string body = 
      "<h4>Email from <strong>{0}</strong> ({1})</h4>" + 
      "<p>Date: {2}</p>" + 
      "<p>Phone: {3}</p>" + 
      "<p>Message:</p><p style='padding-left: 20px;'>{4}</p>" + 
      "<br /><p style='font-size: 10px; color: gray;'>This email was generated by testsite.com</p>"; 
     string to = ConfigurationManager.AppSettings["ContactUsEmailAddress"]; 
     MailMessage message = new MailMessage(); 
     message.To.Add(new MailAddress(to)); 
     message.From = new MailAddress(inputModel.Email); 
     message.Subject = "Message from " + inputModel.Name; 
     message.Body = String.Format(body, new string[] 
      { 
       inputModel.Name, inputModel.Email, DateTime.Now.ToLongDateString(), inputModel.Phone, inputModel.UserMessage 
      } 
     ); 
     message.IsBodyHtml = true; 

     using (var smtp = new SmtpClient()) 
     { 
      var credential = new NetworkCredential 
      { 
       UserName = ConfigurationManager.AppSettings["EmailServerUserName"], 
       Password = ConfigurationManager.AppSettings["EmailServerPassword"] 
      }; 
      smtp.Credentials = credential; 
      smtp.Host = ConfigurationManager.AppSettings["EmailServerHost"]; 
      smtp.Port = Convert.ToInt32(ConfigurationManager.AppSettings["EmailServerPort"]); 
      smtp.EnableSsl = true; 
      await smtp.SendMailAsync(message); 
      return View(new ContactUsViewModel(true)); //return to the same page, but with form now emptied. 
                 //"true" param tells the model/Razor page to show a "Email sent" message 
     } 
    } 
    catch (Exception ex) 
    { 
     // handle exception 
    } 
} 

はここに私のweb.configファイルです。私はデフォルトのMVCテンプレートweb.configに変更を加えていません.eメール情報の参照に使用されるappSettingsセクションに5つの新しいキーを追加します。

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
    For more information on how to configure your ASP.NET application, please visit 
    http://go.microsoft.com/fwlink/?LinkId=301880 
    --> 
<configuration> 
    <configSections> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </configSections> 
    <connectionStrings> 
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-HomePuttingSolutionsMVC-20160424082834.mdf;Initial Catalog=aspnet-HomePuttingSolutionsMVC-20160424082834;Integrated Security=True" providerName="System.Data.SqlClient" /> 
    </connectionStrings> 
    <appSettings> 
    <add key="ContactUsEmailAddress" value="[email protected]"/><!--Where site-generated emails will be sent to--> 
    <add key="EmailServerUserName" value="[email protected]"/> 
    <add key="EmailServerPassword" value="password123"/> 
    <add key="EmailServerHost" value="smtp-mail.outlook.com"/> 
    <add key="EmailServerPort" value="587"/> 
    <add key="webpages:Version" value="3.0.0.0" /> 
    <add key="webpages:Enabled" value="false" /> 
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
    </appSettings> 
    <system.web> 
    <authentication mode="None" /> 
    <compilation debug="true" targetFramework="4.5.2" /> 
    <httpRuntime targetFramework="4.5.2" /> 
    <httpModules> 
    </httpModules> 
    </system.web> 
    <system.webServer> 
    <modules> 
     <remove name="FormsAuthentication" /> 
    </modules> 
    <validation validateIntegratedModeConfiguration="false" /> 
    </system.webServer> 
    <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
     <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /> 
     <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
     </dependentAssembly> 
    </assemblyBinding> 
    </runtime> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 
     <parameters> 
     <parameter value="mssqllocaldb" /> 
     </parameters> 
    </defaultConnectionFactory> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 
    <system.codedom> 
    <compilers> 
     <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" /> 
     <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" /> 
    </compilers> 
    </system.codedom> 
</configuration> 

これは私の最初のサイトで、電子メールを初めて実装したので、どんな助けでも大歓迎です。ありがとう。

EDIT:それはおそらく、言うまでもないが、私の側に、私は、web.configファイルのContactUsEmailAddressEmailServerUserName、およびEmailServerPasswordフィールド、私はここに含まれてきていないダミーの値に書き込まれた実際の資格情報を持っています。

答えて

1

、私は適切なsystem.web > mailSettings > smtp設定に私のweb.configファイルで手動appSettingsキーから切り替えることで、この作業を取得することができました。

<system.net> 
    <mailSettings> 
     <smtp from="[email protected]"> 
     <network host="smtp-mail.outlook.com" 
       port="587" 
       userName="[email protected]" 
       password="password123" 
       enableSsl="true" /> 
     </smtp>  
    </mailSettings> 
    </system.net> 
0

SSLをオフにします。

smtp.EnableSsl = false; 

ドメインがHTTPSを使用している場合のみ、SSLを使用する必要があります。これはlocalhostまたはhttpのみのドメインでは機能しません。 @MRebatiが彼の答えで共有記事を参照

read line 3

Check this article

+0

SSLをオフにすることは役に立ちませんでした。実際には、適切な例外がスローされます。 'SMTPサーバーには安全な接続が必要です。またはクライアントが認証されていません。サーバーの応答は:5.7.0 STARTTLSコマンドを最初に発行する必要があります。他のアイデア? –

関連する問題