Friday, January 9, 2009

Send mail from outlook using C#

 
Copy the below code and paste it as "OutlookMail.cs".
 
Using System;
Namespace OutLookMail
 
Public class OutlookMail
Private Outlook.Application objApp;
Private Outlook._NameSpace objNameSpace;
Private Outlook.MAPIFolder oOutboxFolder;
 
Public OutlookMail()
objApp = new Outlook.Application();
objNameSpace= objApp.GetNamespace("MAPI");
objNameSpace.Logon(null,null,true,true);
objOutbox = objNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox);
 
Public void SendMailOutlook(string toEmailId, string ccEmailId, string strAttachment, string StrSubject, string StrBody)
Outlook._MailItem objMail = (Outlook._MailItem)objApp.CreateItem(Outlook.OlItemType.olMailItem); 
ObjMail.To = toEmailId;
ObjMail.CC = ccEmailId;
ObjMail.Subject = StrSubject;
if (strAttachment!="")
ObjMail.StrAttachmentments.Add(strAttachment,1,1,"Email Test from Outlook using VB.NET");
ObjMail.Body = StrBody;
ObjMail.Send();
Don't forget to add reference to outlook 11.0 or higher which ever you are using.
Happy mailing!!!!!!!

No comments: