Send Email Using Outlook
Send Email Using Outlook
Application oApp;
private Outlook._NameSpace oNameSpace;
private Outlook.MAPIFolder oOutboxFolder;
private Outlook.Attachment oAttachment;
/// <summary>
/// Constructor
/// </summary>
public OutlookMail()
{
//Return a reference to the MAPI layer
oApp = new Outlook.Application();
oNameSpace= oApp.GetNamespace("MAPI");
/***********************************************************************
* Logs on the user
* Profile: Set to null if using the currently logged on user, or set
* to an empty string ("") if you wish to use the default Outlook
Profile.
* Password: Set to null if using the currently logged on user, or set
* to an empty string ("") if you wish to use the default Outlook
Profile
* password.
* ShowDialog: Set to True to display the Outlook Profile dialog box.
* NewSession: Set to True to start a new session. Set to False to
* use the current session.
***********************************************************************/
oNameSpace.Logon(null,null,true,true);
//create attachments
//Add an attachment.
// TODO: change file path where appropriate
//String sSource = "C:\\setupxlg.txt";
//String sDisplayName = "MyFirstAttachment";
//int iPosition = (int)oMsg.Body.Length + 1;
//int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
//Outlook.Attachment oAttach = oMsg.Attachments.Add(sSource, iAttachType,
iPosition, sDisplayName);
/// <summary>
/// Adds an email to MS Outlook Outbox
/// </summary>
/// <param name="toValue">Email address of recipient</param>
/// <param name="subjectValue">Email subject</param>
/// <param name="bodyValue">Email body</param>
public void addToOutBox(string toValue, string subjectValue, string bodyValue)
{
//creates a new MailItem object
Outlook._MailItem oMailItem =
(Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMailItem.To = toValue;
oMailItem.Subject = subjectValue;
oMailItem.Body = bodyValue;
//sSource = "C:\\parammkcn.txt";
//sDisplayName = "MojDRUGIMailSaAttachmentom";
//iPosition = (int)oMailItem.Body.Length + 1;
//iAttachType = (int)Outlook.OlAttachmentType.olByValue;
//oAttachment = oMailItem.Attachments.Add(sSource, iAttachType, iPosition,
(object)sDisplayName);
oMailItem.SaveSentMessageFolder = oOutboxFolder;
oMailItem.Display(false);
//uncomment this to also save this in your draft
oMailItem.Save();