Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 237
Jonathan Avatar asked Jan 20 '09 21:01

Jonathan


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


2 Answers

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);
like image 110
John Sheehan Avatar answered Sep 28 '22 05:09

John Sheehan


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.

like image 38
Dale Ragan Avatar answered Sep 28 '22 06:09

Dale Ragan