Send E-mail Using SMTP - CSharp Network

CSharp examples for Network:Email

Description

Send E-mail Using SMTP

Demo Code

using System;/*from  w w  w .j  a  v  a 2 s  . c om*/
using System.Net;
using System.Net.Mail;
class MainClass
{
   public static void Main(string[] args)
   {
      SmtpClient client = new SmtpClient("mail.somecompany.com", 25);
      client.Credentials = new NetworkCredential("user@somecompany.com", "password");
      using (MailMessage msg = new MailMessage())
      {
         msg.From = new MailAddress("author@visual-csharp-recipes.com");
         msg.Subject = "Greetings from Visual C# Recipes";
         msg.Body = "this is a test.";
         msg.Attachments.Add(new Attachment("Main.cs", "text/plain"));
         msg.Attachments.Add(new Attachment("Main.exe", "application/octet-stream"));
         foreach (string str in args)
         {
            try
            {
               msg.To.Add(new MailAddress(str));
            }
            catch (FormatException ex)
            {
               Console.WriteLine("{0}: Error -- {1}", str, ex.Message);
               continue;
            }
         }
         client.Send(msg);
      }
   }
}

Result


Related Tutorials