views:

162

answers:

3

Hi

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="Password"/>
    <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();

        }

    }
A: 

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

Rubens Farias
I tried that as well did not work.
chobo2
A: 

Hi,

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.

Cheers!

Dave

dave wanta
Well as far as I know I am not getting an exception. First I am using Elmah to log any unhandled expections yet non are logged. Nor do I see any exceptions show up on the page that does the smtp sending. I have checked the spam folder and no messages are from me. So how do I do this System.Net.Mail logging?Thanks
chobo2
Hi, I tried adding the web.config switches you need, but the comments section doesn't allow enough characters. However, I have written a post about it here: http://systemnetmail.com/faq/4.10.aspx
dave wanta
A: 
  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.

sajoshi