0% found this document useful (0 votes)
2 views2 pages

Send Email Using Outlook

The document outlines a class for sending emails using Microsoft Outlook's API. It includes a constructor that initializes the Outlook application and logs on the user, as well as a method to add an email to the Outbox with specified recipient, subject, and body. The method also allows for attachments to be added, although the attachment code is commented out in the provided example.

Uploaded by

edin_s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Send Email Using Outlook

The document outlines a class for sending emails using Microsoft Outlook's API. It includes a constructor that initializes the Outlook application and logs on the user, as well as a method to add an email to the Outbox with specified recipient, subject, and body. The method also allows for attachments to be added, although the attachment code is commented out in the provided example.

Uploaded by

edin_s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

private 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);

//gets defaultfolder for my Outlook Outbox


oOutboxFolder =
oNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox);

//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;

//string sSource = "C:\\paramcen.txt";


//string sDisplayName = "MojPrviMailSaAttachmentom";
//int iPosition = (int)oMailItem.Body.Length + 1;
//int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
//oAttachment = oMailItem.Attachments.Add(sSource, iAttachType, iPosition,
(object)sDisplayName);

//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();

//adds it to the outbox


oMailItem.Send();

You might also like