Wednesday, June 29, 2011

How to send e-mail programmatically by using System.Web.Mail in Visual C#


using System;
using System.Web.Mail;

namespace WebMail
{
    class Class1
    {
        static void Main(string[] args)
        {
            try 
            {
                MailMessage oMsg = new MailMessage();
            
                oMsg.From = "sender@somewhere.com";
               
                oMsg.To = "recipient@somewhere.com";
                oMsg.Subject = "Send Using Web Mail";
                
             
                oMsg.BodyFormat = MailFormat.Html;
                
               
                oMsg.Body = "<HTML><BODY><B>Hello World!</B></BODY></HTML>";
                
               
              
                String sFile = @"C:\temp\Hello.txt";  
                MailAttachment oAttch = new MailAttachment(sFile, MailEncoding.Base64);
  
                oMsg.Attachments.Add(oAttch);

              
                SmtpMail.SmtpServer = "MySMTPServer";
                SmtpMail.Send(oMsg);

                oMsg = null;
                oAttch = null;
            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);
            }
        }
    }
} 
   
Add a reference to the System.Web.dll namespace

No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

#Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc

 #Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc Linux is an open-source operating system that is loved by millio...