在 asp.net mvc4 smtp.Send(mail) 中发送邮件失败

2024-01-26

我在 stackoverflow 中找到了有关此问题的帖子数量,但我的问题仍然没有解决。我尝试使用以下代码发送邮件:

在控制器中:

try
         {
        MailMessage mail = new MailMessage();
        mail.To.Add("[email protected] /cdn-cgi/l/email-protection");
        mail.From = new MailAddress("[email protected] /cdn-cgi/l/email-protection");
        mail.Subject = "Feedback for Website";
        string Body = "Name:";
        mail.Body = Body;
        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        smtp.Credentials = new System.Net.NetworkCredential("[email protected] /cdn-cgi/l/email-protection", "topassword");
        smtp.Send(mail);

    }
    catch (Exception ex)
    {
       Response.Write(ex.Message);

    }

网络配置:

<system.net>
<mailSettings>
  <smtp deliveryMethod="PickupDirectoryFromIis">
    <network defaultCredentials="true" host="localhost" port="25"/>
  </smtp>
</mailSettings>

我也尝试过这个:

<smtp from="[email protected] /cdn-cgi/l/email-protection">
 <network enableSsl="true" host="smtp.gmail.com" port="587" userName="[email protected] /cdn-cgi/l/email-protection" password="mypassword" />

调试时在我的输出中:

Step into: Stepping over non-user code 'System.Net.Mail.MailMessage.MailMessage'
Step into: Stepping over non-user code 'System.Net.Mail.MailMessage.To.get'
Step into: Stepping over non-user code 'System.Net.Mail.MailAddress.MailAddress'
Step into: Stepping over non-user code 'System.Net.Mail.MailMessage.Subject.set'
Step into: Stepping over non-user code 'System.Net.Mail.MailMessage.Body.set'
Step into: Stepping over non-user code 'System.Net.Mail.MailMessage.IsBodyHtml.set'
Step into: Stepping over non-user code 'System.Net.Mail.SmtpClient.SmtpClient'
Step into: Stepping over non-user code 'System.Net.Mail.SmtpClient.Host.set'
Step into: Stepping over non-user code 'System.Net.Mail.SmtpClient.EnableSsl.set'
The thread '<Thread Ended>' (0x1f38) has exited with code 0 (0x0).
'iisexpress.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\11.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll'
Step into: Stepping over non-user code 'System.Net.NetworkCredential.NetworkCredential'
The thread '<No Name>' (0x23d8) has exited with code 0 (0x0).
Step into: Stepping over non-user code 'System.Net.Mail.SmtpClient.Send'
A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dll
The thread '<Thread Ended>' (0x1474) has exited with code 0 (0x0).

如何解决这个问题。请帮助我。


经过长时间的搜索,我终于解决了这个问题:

Host = "172.**.*.***",
Port = 25,
EnableSsl = false,

Changed host from smtp.gmail.com to "172.**.*.***"(my mail server IP)
port changed from 587 to 25
and set false to EnableSsl

它适用于本地..通过使用本地服务器主机。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 asp.net mvc4 smtp.Send(mail) 中发送邮件失败 的相关文章

随机推荐