Send email by C# from my website

前端 未结 1 713
再見小時候
再見小時候 2021-01-26 00:32

i use the following code to send email :

        public static bool SendEmail(string To, string ToName, string From, string FromName, string Subject, string Bod         


        
相关标签:
1条回答
  • 2021-01-26 00:37

    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.

    0 讨论(0)
提交回复
热议问题