C# code for sending an email without knowing much about the server configuration?

12,819

Solution 1

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>

Solution 2

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);

Solution 3

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.

Share:
12,819

Related videos on Youtube

Jonathan
Author by

Jonathan

Hello, I'm Jonathan! 👋 Experienced in Front End Development (HTML, CSS, ES6, Angular, React), Back End Development (C#, Java, NodeJS) and Software Engineering (OOP, RDBMS, TDD and beginning to branch into FP). Additionally trained and experienced in Interaction Design (User Research, Sketching, Wireframing, Prototyping, Accessibility). Focused on user needs, with an emphasis on relationships, empathy, evidence and results. I enjoy working in diverse, multi-disciplinary teams, learning and sharing knowledge. Made significant and lasting contributions to several high-impact projects in Australia, for: Bupa (2010), Westpac (2013), Service NSW (2015) and the Digital Transformation Agency (2017). Worked remotely for 5+ clients (including production support and across time-zones), with high self-motivation, productivity and communication (over email, IM and pull-requests). • Continuously programming since 2000 •

Updated on April 16, 2022

Comments

  • Jonathan
    Jonathan about 2 years

    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.

  • Jonathan
    Jonathan over 15 years
    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'"}"
  • Troj
    Troj over 15 years
    @johnathanconway: Well... you're supposed to put in the adress to the smtp server in the "SMTP Server Address" string.
  • Tarik
    Tarik almost 15 years
    To is "get" properties,you cannot set it like that.