Send email by C# from my website

喜你入骨 提交于 2019-12-02 07:15:02

Assuming the SMTP server (smtp.datagts.net) is running fine, some items to check:

  1. Your code seems to be using UseDefaultCredentials=true, but on the next line your are providing credentials
  2. As mentioned in the comments check that Port 587 isn't blocked at your web host
  3. If you are hosted on a shared server (not a dedicated machine), it's likely ASP.Net is set for medium trust. IF so, you cannot use any port for SMTP other than Port 25.

Update:

To try and get to the error. In your LOCAL (development) machine, add this to your web.config:

<system.web>
...
    <securityPolicy>
      <trustLevel name="Medium" />
    </securityPolicy>
...

ASP.Net on your local machine runs in FULL TRUST. The above setting makes the current web site/application you are working on run in medium trust. You can remove/comment as necessary. The point of this exercise is to try and match what your web host settings are (it's obviously different if things work in your local machine then dies when published). It would be nice to just obtain the info from your web host..but until then....

Then try both Port 587 and 25.

  • It should fail on port 587 with a security exception (because of medium trust)
  • If your mail server only accepts SMTP connections on port 587, then of course, port 25 will not work either (but you should get a different error). The point being "...it still doesn't work..." in this case is that the SMTP server (smtp.datagts.net) only accepts connections on port 587

GMAIL is the same story. You cannot use Port 587 if your web host settings for ASP.Net is medium trust. I have been through this many times - it will "work" in my local machine, but as soon as I enable medium trust in my local development machine, it will fail.

You should ask your web host what their settings are for ASP.Net - if its some "custom" setting you can ask for a copy and use that in your dev box as well.

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