Thursday, March 6, 2008

How to send email and check without real SMTP server.

To send email via SMTP server see next sample code

using System.Net.Mail;
using System.Text;
 
namespace email_functions
{
    class Program
    {
        static void Main(string[] args)
        {
            string host = "test.com";
            SmtpClient smtpClient;
            MailAddress sender;
            MailAddress recipient;
 
            smtpClient = new SmtpClient(host);
            smtpClient.Timeout = 3000;
 
            sender = new MailAddress("sender@test.com", "Sender");
            recipient = new MailAddress("recipient@test.com", "Recipient");
 
            MailMessage message = new MailMessage(sender, recipient);
            message.SubjectEncoding = Encoding.UTF8;
            message.Subject = "test Smtp";
 
            smtpClient.Send(message);
        }
    }
}

But if real SMTP server (in sample, "test.com") is not accessible, then next error will be shown:






To test sending email without real SMTP server it is suggested to add section to .config file:


Notes:

  • Folder "c:\testEmailsFolder" should be created before email sending.
  • After .config file changes a program should be restarted.

Now, when the sample will finished, then text file .eml will be creared in folder "c:\testEmailsFolder". The file can be opened in Windows Live Mail.

X-Sender: Sender 
X-Receiver: Recipient
MIME-Version: 1.0
From: Sender
To: Recipient
Date: 6 Mar 2008 13:16:24 +0200
Subject: =?utf-8?B?dGVzdCBTbXRw?=

Additional Links:

SmtpClient..::.DeliveryMethod Property

No comments: