开发者

Need help setting up SMTP

I am having a weird problem and I am not sure why.

I have this in my webconfig

  <add key="SMTP" value="mail.reliablesite.net"/>
    <add key="Email" value="Email"/>
    <add key="EMAIL_PASSWORD" value="Pas开发者_运维问答sword"/>
    <add key="FROM_PORT" value="2525"/>

Now when I test smtp through localhost all is well and sends my smtp and everything. I upload my webconfig plus all my other files and I try to test my smtp and no message is sent. Exact same code and everything.

So I don't understand why this does not work.

This is like a condensed version of my code.

public class MyTestController : Controller
    {
        //
        // GET: /MyTest/

        public void Index()
        {
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("From");
            mail.To.Add("To");
            mail.Subject = "subject";
            mail.IsBodyHtml = true;
            mail.Body = "body";

            NetworkCredential credential = new NetworkCredential("EmailFromMyHostingSite", "Password");

            SmtpClient smtp = new SmtpClient();
            smtp.Host = "mail.reliablesite.net";
            smtp.Port = 2525;
            smtp.Credentials = credential;

            smtp.Send(mail);
            mail.Dispose();

        }

    }


  1. I suggest using the System.Net settings instead of appsettings. This ensures you get the settings automatically when you invoke any method or property on SMTPClient.

  2. Now - coming to your question, as others pointed out 25 is the SMTP port. Sometimes, SMTP is configured to use a specific port at the server and you need to configure that in your config file. You can try following code for sending mail thru a GMAIL account from your code.

Use a try catch to wrap the smtp.send() and check if you are getting any exceptions.


As SMTP default port is 25, you probably just need to update this information in your configuration.


Just to be clear, the .Port property refers to the port of the mail server, not the port you are sending email from.

Now, that being said, mail.reliablesite.net does have a SMTP service running at port 2525 (I just checked, and you can telnet to it, and get the welcome response back).

The next question is "How do you know the email isn't sent?" You don't mention anything about an exception. Are you saying it just isn't showing up in your inbox? If so, check your spam folder.

If that doesn't work, then I would recommend enabling logging for System.Net.Mail, and checking the log file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜