Monday, September 6, 2010

Send Emails Using C#

Attention: This post describes the basics of sending emails. At the time of this post this was possible without the help of SSL, nowadays though this is required by most providers. Thus I explain the sending of emails with an SSL connection in this new post.

Hinweis: Dieser Post erklärt die Grundlagen des Sendens von Emails. Zur Zeit des Posts war dies noch ohne SSL möglich, heutzutage ist dies jedoch meist Voraussetzung. Daher erkläre ich das Senden von Emails unter Benutzung einer SSL-Verbindung in diesem neuen Post.

Surprisingly, emails can be send with only a few lines of code in C#.
The class System.Net.Mail provides the necessary functions and therefor has to be included in the project via using.
The new email can then be created as an instance of the class MailMessage.
The sender address is set by the property From, which expects an object of type MailAddress. The properties To, Body and Subject are of type string and describe the recipient address, the message text and subject of the message.
Zu send the mail an instance of the class SmtpClient is needed.
This expects the address of the outgoing mail server in the constructor (it has to be an SMTP server) as well as the port to be used.
Some mail servers expect a logon before the sending of emails. For that an instance of the class System.Net.NetworkCredential has to be created, in the constructor username and password are set.
The instance is then given to the property Credentials of the class MailMessage. The following program is a little console application, which creates an email from the inputted data and sends it:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail; // add

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            MailMessage Email = new MailMessage();

            Console.WriteLine("Sender:");
            MailAddress Sender = new MailAddress(Console.ReadLine());
            Email.From = Sender; // set sender

            Console.WriteLine("Receiver:");
            Email.To.Add(Console.ReadLine()); // add receiver

            Console.WriteLine("Subject:");
            Email.Subject = Console.ReadLine(); // add subject

            Console.WriteLine("Message text:");
            Email.Body = Console.ReadLine(); // add message text

            Console.WriteLine("Outgoing mail server:");
            string ServerName = Console.ReadLine();
            Console.WriteLine("Port:");
            string Port = Console.ReadLine();

            SmtpClient MailClient = new SmtpClient(ServerName, int.Parse(Port)); // set outgoing mail server

            Console.WriteLine("Credentials needed?"); // "Yes" or "No" possible

            bool LoginNecessary = (Console.ReadLine() == "Yes");
            if (LoginNecessary)
            {
                Console.WriteLine("User name:");
                string UserName = Console.ReadLine();
                Console.WriteLine("Password:");
                string Password = Console.ReadLine();
                System.Net.NetworkCredential Credentials = new System.Net.NetworkCredential(UserName, Password);

                MailClient.Credentials = Credentials; // set credentials
            }

            MailClient.Send(Email); // send emails

        }
    }
}

This method to send emails works with all email providers, which have an STMP outgoing mail server.
It is important, that as a sender the actual email address is given and the server information are correct (they are the same as in an email program, e.g. Outlook in account settings).
Most providers require credentials to send emails, therefor the property Credentials has to be set.

No comments:

Post a Comment