Don Rhummy
Don Rhummy

Reputation: 25860

Why does SmtpMail not send unless from/host are set in web.config?

I'm trying to send an email using SmtpMail. On my local server, I can do this even if the code has the host/server, password and from info set in the code. On my hosting service (1and1) it will only send if I set those things in the web.config file! Does anyone know what might cause this (1and1 has no idea).

Works Locally but NOT on hosting servcie

<%@ Page language="c#" %>
<%@ Import namespace="System.Net.Mail" %>
<%

    MailMessage mail = new MailMessage();

    //Set my from address
    mail.From = new MailAddress( "[email protected]");

    //Who I'm sending to
    mail.To.Add( new MailAddress("[email protected]") );

    mail.Subject = "A test";
    mail.Body = "Test message";

    //Set the mail server (default should be smtp.1and1.com)
    SmtpClient smtp = new SmtpClient( "smtp.1and1.com" );

    //Enter your full e-mail address and password
    smtp.Credentials = new NetworkCredential("[email protected]", "mypassword");

    //Send the message
    smtp.Send(mail);

%>

THIS IS HOW I GET IT TO WORK ON HOSTING (two files, the test page and web.config)

<%@ Page language="c#" %>
<%@ Import namespace="System.Net.Mail" %>
<%

    MailMessage mail = new MailMessage();

    //Set my from address
    mail.From = new MailAddress( "[email protected]");

    //Who I'm sending to
    mail.To.Add( new MailAddress("[email protected]") );

    mail.Subject = "A test";
    mail.Body = "Test message";

    //Create with no server or credentials (grabs from web.config)
    SmtpClient smtp = new SmtpClient();

    //Send the message
    smtp.Send(mail);

%>

(and web.config)

    <configuration>
    <system.net>
        <mailSettings>
            <smtp from="[email protected]">
                <network host="smtp.1and1.com" port="25" userName="[email protected]" password="mypassword"/>
            </smtp>
        </mailSettings>
    </system.net>
</configuration>

Upvotes: 0

Views: 1030

Answers (1)

Dragan Radivojevic
Dragan Radivojevic

Reputation: 2002

I see that you are not setting SMTP port when working with your local code but that you do set it in web.config. Have you tried manually adding SMTP port like this:

smtp.Port = 25;

Also, have you tried setting EnableSsL property manually?

Upvotes: 1

Related Questions