This is all the code needed to send email (not their is not hard code information about the SMTP server or any of its properties):
// Add an assembly reference to System.Net
using System.Net.Mail;
namespace MailSettingsApplication
{
public class Mail
{
public void Send(
string recipient,
string subject,
string message)
{
var mail = new MailMessage()
{
Subject = subject,
Body = message,
};
foreach (var recipient in recipients.Split(','))
{
mail.To.Add(recipient);
}
var client = new SmtpClient();
client.Send(mail);
}
}
}
{
public class Mail
{
public void Send(
string recipient,
string subject,
string message)
{
var mail = new MailMessage()
{
Subject = subject,
Body = message,
};
foreach (var recipient in recipients.Split(','))
{
mail.To.Add(recipient);
}
var client = new SmtpClient();
client.Send(mail);
}
}
}
The C# code above has not hard coded knowledge of the SmtpClient or the from address used in MailMessage. This fallows the Sergeant Schultz design patter, "I know nothing."
Below are the required settings (app.config or web.config) to send email via outlook.com for example if you signed up for email using premium.outlook.com:
<system.net>
<mailSettings>
<smtp from="sender email here"
deliveryMethod="Network">
<network
host = "smtp-mail.outlook.com"
port = "587"
enableSsl ="true"
userName="put username here"
password="put password here" />
</smtp>
</mailSettings>
</system.net>
if you are not using outlook.com email then the settings (host, port, enableSsl) can be configured accordingly.
The code for this blog can be found at https://github.com/softwarepronto/Blog under the MailSettingsMaster folder.