views:

3327

answers:

5

Is there a way, in C# code, to send an email without having to know the SMTP server configuration, etc on the server, or have any of that stuff set up?

The code I'm developing will be deployed to a live server, but I know nothing about the configuration, so I can't predict what the SMTP server will be.

+1  A: 

The best answer is if you know nothing until live, can you move all the settings into web.config? This will allow configuration up until the last minute. Below is some code to dump into your web.config file. I would question as to why you don't have access to this information though

<system.net>
    <mailSettings>
      <smtp from="[email protected]">
        <network host="SMTP SERVER ADDRESS" port="25"
         userName="USERNAME" password="PASSWORD">
      </smtp>
    </mailSettings>
  </system.net>
Ray Booysen
A: 

If your SMTP configuration is correct, just do this:

MailMessage mail = new MailMessage();

mail.To = "To";

mail.From = "From";

mail.Subject = "Subject";

mail.Body = "Body";

SmtpMail.SmtpServer = "localhost";

SmtpMail.Send(mail);

Rulas
To is "get" properties,you cannot set it like that.
Braveyard
+5  A: 

Add this to your web.config (MSDN reference here):

<system.net>
    <mailSettings>
        <smtp deliveryMethod="Network" from="[email protected]">
            <network host="localhost" port="25" />
        </smtp>
    </mailSettings>
</system.net>

Using SmtpClient without specifying configuration settings will use the values from the web.config:

MailMessage msg = new MailMessage(...);
// build message contents
SmtpClient client = new SmtpClient();
client.Send(msg);
John Sheehan
That one is great. Thanks.
Braveyard
+2  A: 

I answered a question similar to this not to long ago. You can view it here. Using papercut, you can test your application without knowing or using the actual production smtp server.

Then during testing you can just set the host to your local machine that is running papercut in the app/web config. Therefore it can be changed once moving to production.

Papercut will show you the emails that were sent and also the contents.

Dale Ragan
A: 

As an alternative: If you don't want to rely on the server configuration and do it programmatically you could always do this:

MailMessage mail = new MailMessage() {
    To = "someone@somewhere",
    From = "someone@somewhere",
    Subject = "My Subject",
    Body = "My message"
};

SmtpClient client = new SmtpClient("SMTP Server Address");
    // Naturally you change the "SMTP Server Address" to the
    // actual SMTP server address
client.Send(mail);

But I suggest you stick it in web.config file (which can be configured through ASP.NET Web Configuration tool as well).

Spoike
This won't work.I get a .NET exception:"Failure sending mail."Inner exception says: "{"The remote name could not be resolved: 'SMTP Server Address'"}"
jonathanconway
@johnathanconway: Well... you're supposed to put in the adress to the smtp server in the "SMTP Server Address" string.
Spoike