Failure sending Mail in asp.net mvc4 smtp.Send(mail)

喜夏-厌秋 提交于 2020-01-04 11:10:29

问题


I find out number of posts regarding to this Issue in stackoverflow, But Still my problem was not solved. I tried to send a mail using the below code:

In Controller:

try
         {
        MailMessage mail = new MailMessage();
        mail.To.Add("mailto@gmail.com");
        mail.From = new MailAddress("mailfrom@gmail.com");
        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("mailTo@gmail.com", "topassword");
        smtp.Send(mail);

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

    }

web.config:

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

I tried this also:

<smtp from="mymail@gmail.com">
 <network enableSsl="true" host="smtp.gmail.com" port="587" userName="mymail@gmail.com" password="mypassword" />

While debugging In my output:

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).

How to solve this.. Please help me..


回答1:


Finally after long searches I solved the issue:

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

It was working for local.. By using local server host.




回答2:


Try next settings.

enable ssl in code:

smtp.EnableSsl = true;

set config as follows:

<mailSettings>
      <smtp deliveryMethod="Network" from="username@gmail.com">
        <network defaultCredentials="false" host="smtp.gmail.com" port="587" userName="mymail@gmail.com" password="mypassword" />
      </smtp>
</mailSettings>


来源:https://stackoverflow.com/questions/14686383/failure-sending-mail-in-asp-net-mvc4-smtp-sendmail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!