Problem sending mail to mlutiple users in C# - c#

Hi,
I'm trying to develop a wpf application for sending bulk email using Microsoft outlook app. I'm using a datagridview from which to, cc, and attachment will be extracted. Besides, for each mail, there'll be multiple to(s) and cc(s). If to and/or cc field contain single mail address, it works. But when multiple to or cc added, it gets the following error.
System.Runtime.InteropServices.COMException: 'Outlook does not recognize one or more names. '
I things it's issue with mail separation and put some efforts to resolve it.
Here's my datagridview data
Here's the code for sending mail.
private void BtnSendMail_Click(object sender, RoutedEventArgs e)
{
foreach (DataRowView rows in dgdData.ItemsSource)
{
try
{
Outlook.Application oApp = new Outlook.Application();
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
Outlook.Recipients oRecips = oMsg.Recipients;
List<string> sTORecipsList = new List<string>();
List<string> sCCRecipsList = new List<string>();
var to = rows.Row.ItemArray[1].ToString();
var cc = rows.Row.ItemArray[2].ToString();
var attachment = rows.Row.ItemArray[3].ToString();
//var status = rows.Row.ItemArray[0] ="Sending";
if (to.Contains(",") || to.Contains(";") || to.Contains(" "))
{
string[] tos = Regex.Split(to, ",; ");
// string[] lines = Regex.Split(value, "\r\n");
for (int i = 0; i < tos.Length; i++)
{
//mail.To.Add(new MailAddress(tos[i]));
sTORecipsList.Add(tos[i]);
}
}
else
{
sTORecipsList.Add(to);
}
if (cc.Contains(",") || cc.Contains(";") || cc.Contains(" "))
{
string[] ccs = Regex.Split(cc, ",; ");
// string[] lines = Regex.Split(value, "\r\n");
for (int i = 0; i < ccs.Length; i++)
{
//mail.To.Add(new MailAddress(tos[i]));
sCCRecipsList.Add(ccs[i]);
}
}
else
{
sCCRecipsList.Add(cc);
}
oMsg.Body = "Sample Text";///rtbBody.Text.ToString();
string sDisplayName = "MyAttachment";
int iPosition = oMsg.Body.Length + 1;
int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
Outlook.Attachment oAttach = oMsg.Attachments.Add(attachment, iAttachType, iPosition, sDisplayName);
oMsg.Subject = txtSubject.Text.ToString();
foreach (string t in sTORecipsList)
{
Outlook.Recipient recipTo = oMsg.Recipients.Add(t);
recipTo.Type = (int)Outlook.OlMailRecipientType.olTo;
}
foreach (string c in sCCRecipsList)
{
Outlook.Recipient recipCc = oMsg.Recipients.Add(c);
recipCc.Type = (int)Outlook.OlMailRecipientType.olCC;
}
oMsg.Recipients.ResolveAll();
Thread.Sleep(2000);
oMsg.Send();
rows.Row.ItemArray[0] = "Sent";
sTORecipsList = null;
sCCRecipsList = null;
//recipTo = null;
oRecips = null;
oAttach = null;
oMsg = null;
oApp = null;
Thread.Sleep(2000);
}
catch (Exception)
{
throw;
}
}
MessageBox.Show("All message sent!!");
}
Waiting for experts help. Thanks in advance.
Regards,
Subrata

The second argument to Regex.Split should be a regex pattern. So to match , or ; as a delimiter, you'd use a pattern like \s*[,;]\s* (the \s* allows optional whitespace around the delimiters):
string[] tos = Regex.Split(to, #"\s*[,;]\s*");
Note this uses the # symbol on the string to preserve the escapes.

Related

How to fetch attachment from outlook email in winforms?

Microsoft.Office.Interop.Outlook.Application Application = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MAPIFolder inBox = Application.ActiveExplorer().Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
Microsoft.Office.Interop.Outlook.Items inBoxItems = inBox.Items;
Microsoft.Office.Interop.Outlook.MailItem newEmail = null;
foreach (object collectionItem in inBoxItems)
{
newEmail = collectionItem as Microsoft.Office.Interop.Outlook.MailItem;
if (newEmail != null)
{
if (newEmail.Attachments.Count > 0)
{
for (int i = 1; i <= newEmail
.Attachments.Count; i++)
{
newEmail.Attachments[i].SaveAsFile
(#"C:\TestFileSave\" +
newEmail.Attachments[i].FileName);
}
}
}
}
I have copy mail from outlook and paste into flex-grid in window application but getting zero attachment count but in mail there are attachments like excel , doc , etc. files.
int extractFileParentId = pkUniqueId;
List<ZipExtracFile> extractFileList = lts;
MsgReader.Outlook.Storage.Message message = new MsgReader.Outlook.Storage.Message(fileNames);
foreach (var attachment in message.Attachments)
{
string fileName = string.Empty;
pkUniqueId = pkUniqueId + 1;
if (attachment.GetType() == typeof(MsgReader.Outlook.Storage.Attachment))
{
var attach = (MsgReader.Outlook.Storage.Attachment)attachment;
fileName = Path.Combine(tempPath, (attach).FileName);
File.WriteAllBytes(fileName, attach.Data);
extractFileList.Add(new ZipExtracFile { pkUniqueId = pkUniqueId, fileName = fileName, parentId = extractFileParentId });
if(Path.GetExtension(fileName).ToLower() == ".msg")
{
ExtractMsgFile(fileName, ref pkUniqueId, ref tempPath, lts);
}
}
}
message.Dispose();
This is used to fetch documents from ".msg" file documents , with multilevel ".msg" file and it can download at specific location. For this need to add only MsgReader.dll

Email to CSV C# - String isn't formatting correctly

So I am currently parsing emails that we receive from work, all of which are formatted almost exactly the same, and I am getting the data that I need out of all of them, however I am experiencing problems retrieving a "description", which comes in multiple lines. I've tried removing the "\n" out of these strings, but it doesn't always work. When I open the .csv files, the formatting is fine sometimes, but otherwise these extra returns in the strings ruin the entire thing.
Here is my code:
public static void Main(string[] args)
{
helper();
Console.WriteLine("Press any key to exit ... ");
Console.ReadKey();
} //end main method
public static void helper()
{
//initiate outlook
Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook._NameSpace ns = null;
Microsoft.Office.Interop.Outlook.MailItem item = null;
Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;
try
{
app = new Microsoft.Office.Interop.Outlook.Application();
ns = app.GetNamespace("MAPI");
ns.Logon(null, null, false, false);
inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
subFolder = inboxFolder.Folders["WorkOrders"]; //folder.Folders[1];
Console.WriteLine("Folder Name: {0}", subFolder.Name);
Console.WriteLine("Number of Emails: {0}", subFolder.Items.Count.ToString());
Console.WriteLine();
Console.WriteLine("-------------------------");
Console.WriteLine();
//Variables for loop
String sub;
String date;
String time;
String body;
String desc;
String storeNumber;
String workOrderNumber;
int indexOfDesc;
int indexOfSp;
int items = subFolder.Items.Count;
DataTable dt = new DataTable();
dt.Columns.Add("Work Order Number");
dt.Columns.Add("Date");
dt.Columns.Add("Store Number");
dt.Columns.Add("Description");
dt.Rows.Add("Work Order Number", "Date", "Store Number", "Description");
dt.Rows.Add("", "", "", ""); // just a blank row
for (int i = 1; i <= items; i++)
{
item = (Microsoft.Office.Interop.Outlook.MailItem)subFolder.Items[i];
sub = item.Subject;
date = item.SentOn.ToLongDateString();
body = item.Body;
desc = "";
storeNumber = sub.Substring(0, 5);
workOrderNumber = sub.Substring(sub.Length - 10);
indexOfDesc = body.IndexOf("Description Overview:");
indexOfSp = body.IndexOf("Safety Precautions:");
desc = body.Substring(indexOfDesc, indexOfSp - indexOfDesc);
desc = desc.Replace("\t", string.Empty);
desc = desc.Replace("\n", string.Empty);
// Add row in data table
dt.Rows.Add(workOrderNumber, date, storeNumber, desc);
// Process Date
createCSV(dt);
// *** Console Output for testing *** //
Console.WriteLine("Index: {0}", i.ToString());
Console.WriteLine("Store Number: {0}", sub.Substring(0, 5));
Console.WriteLine("Work Order Number: # " + workOrderNumber);
Console.WriteLine("Sent: {0}", date);
Console.WriteLine(desc);
Console.WriteLine();
Console.WriteLine("-------------------------");
Console.WriteLine();
}
PrintTable(dt);
}
catch (System.Runtime.InteropServices.COMException ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
ns = null;
app = null;
inboxFolder = null;
} //end finally
}
private static void PrintTable(DataTable dt)
{
DataTableReader dtReader = dt.CreateDataReader();
while (dtReader.Read())
{
for (int i = 0; i < dtReader.FieldCount; i++)
{
Console.Write("{0} = {1} ",
dtReader.GetName(i).Trim(),
dtReader.GetValue(i).ToString().Trim());
}
Console.WriteLine();
}
dtReader.Close();
}
/**
* createCSV(DataTable dt) takes each line from data
* table and appends it to a csv file created at the var path
* #param dt - DataTable
**/
public static void createCSV(DataTable dt)
{
String path = "C:\\Users\\WORK\\Desktop\\test.csv";
StringBuilder sb = new StringBuilder();
foreach(DataRow dr in dt.Rows){
foreach(DataColumn dc in dt.Columns){
sb.Append(FormatCSV(dr[dc.ColumnName].ToString()) + ",");
} // end foreach
sb.Remove(sb.Length-1,1); //remove comma
sb.AppendLine();
} // end foreach
File.WriteAllText(path, sb.ToString());
} // end createCSV()
public static string FormatCSV(string input)
{
try
{
if (input == null)
return string.Empty;
bool containsQuote = false;
bool containsComma = false;
int len = input.Length;
for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++)
{
char ch = input[i];
if (ch == '"')
containsQuote = true;
else if (ch == ',')
containsComma = true;
}
if (containsQuote && containsComma)
input = input.Replace("\"", "\"\"");
if (containsComma)
return "\"" + input + "\"";
else
return input;
}
catch
{
throw;
}
}
} //end class
And here is a screenshot of what the CSV file looks like. Mind you, all of the descriptions in the email have extra spaces and stuff in them, and I highlighted the rows that worked the way I want them too: no extra spaces, just one line of the proper information. You can see how the returns ruin the spreadsheet though.
So any help would be so greatly appreciated.
One problem when working with newlines is that sometimes it's not the standard CRLF (\r\n) that Windows uses. Sometimes it is just \r and sometimes it is just \n.
When you don't have control over the newline character it can be a decent safety net to replace all three.
var sub = string.Empty;
var foo = myString.Replace("\r\n", sub).Replace("\r", sub).Replace("\n", sub);
This fixes those cases where you are getting say
Some line item\r\n
Another line item\n
Last line item\r
Removing just \n may work just fine in one application, but another application displaying the same string may still put it on a new line.

How do i access an outlook account inbox using c#?

How do i access an outlook account inbox using c#? I've tried using the outlook object model and imap but it only displays the outlook account logged into my machine. I try logging into a different account with its username and password but it still logs into my local machine account. I read that logging out and closing outlook should fix this problem but it didn't change the results
Here is the code
using System;
using System.Reflection;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace ConsoleApplication1
{
public class Class1
{
public static int Main(string[] args)
{
try
{
Outlook.Application oApp = new Outlook.Application();
// Get the MAPI namespace.
Outlook.NameSpace oNS = oApp.GetNamespace("MAPI");
oNS.Logon("email address placeholder", "password placeholder", false, true);
//Get the Inbox folder.
Outlook.MAPIFolder oInbox = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
String user = oNS.CurrentUser.EntryID;
//Get the Items collection in the Inbox folder.
Outlook.Items oItems = oInbox.Items;
Console.WriteLine(oItems.Count);
Outlook.MailItem oMsg = (Outlook.MailItem)oItems.GetFirst();
Console.WriteLine(oMsg.Subject);
Console.WriteLine(oMsg.SenderName);
Console.WriteLine(oMsg.ReceivedTime);
Console.WriteLine(oMsg.Body);
int AttachCnt = oMsg.Attachments.Count;
Console.WriteLine("Attachments: " + AttachCnt.ToString());
if (AttachCnt > 0)
{
for (int i = 1; i <= AttachCnt; i++)
Console.WriteLine(i.ToString() + "-" + oMsg.Attachments[i].DisplayName);
}
for (int i = 0; i < oItems.Count; i++)
{
if (oItems.GetNext() is Outlook.MailItem)
{
oMsg = (Outlook.MailItem)oItems.GetNext();
Console.WriteLine(oMsg.Subject);
Console.WriteLine(oMsg.SenderName);
Console.WriteLine(oMsg.ReceivedTime);
Console.WriteLine(oMsg.Body);
AttachCnt = oMsg.Attachments.Count;
if (AttachCnt > 0)
{
for (int j = 1; j <= AttachCnt; j++)
Console.WriteLine(j.ToString() + "-" + oMsg.Attachments[j].DisplayName);
}
Console.WriteLine("Attachments: " + AttachCnt.ToString());
Console.WriteLine("CURRENT EMAIL # IS: " + i);
}
else
{
oItems.GetNext();
Console.WriteLine("NOT AN EMAIL");
Console.WriteLine("CURRENT EMAIL # IS: " + i);
}
}
oNS.Logoff();
oMsg = null;
oItems = null;
oInbox = null;
oNS = null;
oApp = null;
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught: ", e);
}
return 0;
}
}
}
You can try using this code:
public List<Outlook.MailItem> GetMails()
{
Microsoft,Office.Interop.Outlook.NameSpace nameSpace = OutLookName.Application.GetNameSpace("MAPI");
nameSpace.Logon("","",System.Reflection.Missing.Value,System.Reflection.Missing.Value);
Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = nameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
List<Outlook.MailItem> eMails = new List<Outlook.MailItem>();
foreach(Microsoft.Office.Interop.Outlook.MailItem mailItem in inboxFolder.Items)
{
if(mailItem.UnRead)
eMails.Add(mailItem);
}
return eMails;
}

remove space at the end of the line in text file

I have got text file with the contents like the below
wYFgemq4-IU372t5I-J0UIIdAd-gcojGR7z BA1111111
HoSOtYLI-90yntvqB-2rV/RLiG-BT69R0NV BA1111111
h1uLXWq4-IU2QUkVr-UYuqipiT-byAuoHn7 BG2222222
jL2MFmq4-IU1VLifN-LZmFc+bu-ibc/2IJp GC1111111
zhoZpmq4-IU27lkQ1-kqNLXTbT-ec28qGPR FG1111111
but unfortunately there is one more space is adding at the end of 5th line by this I am getting an error when I upload the file ...
How can I remove the space ant the end of the fifth line (i.e)
zhoZpmq4-IU27lkQ1-kqNLXTbT-ec28qGPR FG1111111(at here)
would any one please help on this
and this is my code
private bool ParseUploadedDoc(string strUpload)
{
bool blresult = true;
strUpload = strUpload.Replace("\r","");
char [] delimitedchars = {'\n'};
string[] splitwords = strUpload.Split(delimitedchars);
string[] column;
StringBuilder InvalidCert = new StringBuilder();
StringBuilder InvalidSerial = new StringBuilder();
foreach (string word in splitwords)
{
column = word.Split('\t');
column[1].Trim();
if (column[0].Length != 35)
{
InvalidCert.Append(column[0].ToString());
InvalidCert.Append(", ");
blresult = false;
}
/// getting error at here
if (column[1].Length != 9)
{
InvalidSerial.Append(column[1].ToString());
InvalidSerial.Append(", ");
blresult = false;
}
}
if (blresult == false)
{
string strErrCert = "Invalid Certificate Id(s): " + InvalidCert.ToString();
strErrCert = strErrCert.Substring(0, strErrCert.Length - 2);
LblInvalidCert.Text = strErrCert;
string strErrFru = "Invalid Serial Number(s): " + InvalidSerial.ToString();
strErrFru = strErrFru.Substring(0, strErrFru.Length - 2);
LblInvalidFru.Text = strErrFru;
}
return blresult;
}
Problem is at this line
column[1].Trim();
you should do
column[1] = column[1].Trim();

Accessing Outlook ost file

I have seen the difference between pst and ost files and currently working on accessing the outlook pst file through the following code given below.
Is there any way to use the same code for accessing ost file? Can someone refer me to this?
private DataTable GetInboxItems()
{
DataTable inboxTable;
//try
//{
filter = "[ReceivedTime] >= '" + dtpStartDate.Value.ToString("dd/MM/yyyy 12:00 AM") + "' and [ReceivedTime] <= '" + dtpEndDate.Value.ToString("dd/MM/yyyy 11:59 PM") + "'";
Outlook.Application outlookApp = GetApplicationObject();
Outlook.Folder root = outlookApp.Session.DefaultStore.GetRootFolder() as Outlook.Folder;
EnumerateFolders(root);
//string filter = "[ReceivedTime] > '" + dtpStartDate.Value.ToString("dd/MM/yyyy") + "'";
//inbox
Outlook.MAPIFolder inboxFolder = outlookApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
inboxTable = CreateTable();
int count = 0;
if (inboxFolder.Items.Count > 0)
{
var restrictedItems = inboxFolder.Items.Restrict(filter);
restrictedItems.Sort("[ReceivedTime]", true); //descending
//foreach (var item in inboxFolder.Items)
foreach (var item in restrictedItems)
{
var mail = item as Outlook.MailItem;
if (mail != null)
{
//try
//{
DataRow row = inboxTable.NewRow();
//row["sn"] = (++count).ToString();
row["sn"] = mail.EntryID + " " + mail.ReceivedByEntryID;
row["MailType"] = "Inbox";
row["SenderName"] = mail.SenderName;
row["SenderEmail"] = mail.SenderEmailAddress;
row["ReceivedDate"] = mail.ReceivedTime;
row["Subject"] = mail.Subject;
row["Body"] = mail.Body != null ? (mail.Body.Length > 25 ? mail.Body.Substring(0, 25) : mail.Body) : null;
//row["Body"] = mail.Body != null ? mail.Body : "";
row["MailSize"] = mail.Size.ToString();
string attachments = null;
if (mail.Attachments.Count > 0)
{
foreach (var attachment in mail.Attachments)
{
if (((Outlook.Attachment)attachment) != null)
//attachments = ((Outlook.Attachment)attachment).FileName + " " + ((Outlook.Attachment)attachment).Size.ToString() + ", ";
attachments += (((Outlook.Attachment)attachment).Size / 1024).ToString() + " KB, ";
}
}
row["AttachmentCount"] = mail.Attachments.Count;
if (attachments != null)
row["AttachmentSize"] = attachments.Substring(0, attachments.Length - 2);
inboxTable.Rows.Add(row);
}
//catch (Exception ex)
//{
// return null;
//}
}
}
return inboxTable;
}
After I figured out how to actually build the code provided by OP, I found it very useful to get started on Outlook, so I would like to share the completed code below.
using System;
using System.Collections.Generic;//List
using System.Linq;//Array
//using System.Text;
//using System.Threading.Tasks;
using System.Diagnostics;//Process
using System.Runtime.InteropServices;//Marshal
using System.Data;//DataTable
using System.Reflection;//Missing
using Microsoft.Office.Interop.Outlook;//.OST files, needs Microsoft.Office.Interop.Outlook.dll
//TO DO: If you use the Microsoft Outlook 11.0 Object Library, uncomment the following line.
using Outlook = Microsoft.Office.Interop.Outlook;
namespace WatchOutlookMails
{
class StoreFormat
{
public DataTable GetInboxItems(DateTime dtpStartDate, DateTime dtpEndDate)
{
DataTable inboxTable;
string filter = string.Format("[ReceivedTime] >= '{0:dd/MM/yyyy 12:00 AM}' and [ReceivedTime] <= '{1:dd/MM/yyyy 11:59 PM}'", dtpStartDate, dtpEndDate);
Outlook.Application outlookApp = GetApplicationObject();
#if false//only needed if you want to select another folder
Outlook.Folder root = outlookApp.Session.DefaultStore.GetRootFolder() as Outlook.Folder;
EnumerateFolders(root);
#endif
//inbox
Outlook.MAPIFolder inboxFolder = outlookApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
inboxTable = CreateTable();
if (inboxFolder.Items.Count > 0)
{
Items restrictedItems = inboxFolder.Items.Restrict(filter);
const bool SortDescending = true;
restrictedItems.Sort("[ReceivedTime]", SortDescending);
foreach (var item in restrictedItems)//item is a "COM Object" (?)
{
MailItem mail = item as Outlook.MailItem;
if (mail != null)
{
//try
//{
DataRow row = inboxTable.NewRow();
//row["sn"] = (++count).ToString();
row["sn"] = mail.EntryID + " " + mail.ReceivedByEntryID;
row["MailType"] = "Inbox";
row["SenderName"] = mail.SenderName;
row["SenderEmail"] = mail.SenderEmailAddress;
row["ReceivedDate"] = mail.ReceivedTime;
row["Subject"] = mail.Subject;
row["Body"] = mail.Body != null ? (mail.Body.Length > 25 ? mail.Body.Substring(0, 25) : mail.Body) : null;
//row["Body"] = mail.Body != null ? mail.Body : "";
row["MailSize"] = mail.Size.ToString();
int AttachmentSize = 0;
foreach (Outlook.Attachment attachment in mail.Attachments)
{
if (attachment != null)
AttachmentSize += attachment.Size;
}
row["AttachmentCount"] = mail.Attachments.Count;
row["AttachmentSize"] = AttachmentSize;
inboxTable.Rows.Add(row);
//catch (Exception ex)
//{
// break;
//}
}
}
}
return inboxTable;
}
private DataTable CreateTable()
{
DataTable T = new DataTable();
T.Columns.Add("sn", typeof(string));
T.Columns.Add("MailType", typeof(string));
T.Columns.Add("SenderName", typeof(string));
T.Columns.Add("SenderEmail", typeof(string));
T.Columns.Add("ReceivedDate", typeof(string));
T.Columns.Add("Subject", typeof(string));
T.Columns.Add("Body", typeof(string));
T.Columns.Add("MailSize", typeof(int));
T.Columns.Add("AttachmentCount", typeof(int));
T.Columns.Add("AttachmentSize", typeof(int));
return T;
}
private void EnumerateFoldersInDefaultStore()
{
Outlook.Application outlookApp = GetApplicationObject();
Outlook.Folder root = outlookApp.Session.DefaultStore.GetRootFolder() as Outlook.Folder;
EnumerateFolders(root);
return;
}
private void EnumerateFolders(Outlook.Folder folder)
{
Outlook.Folders childFolders = folder.Folders;
if (childFolders.Count > 0)
{
foreach (Outlook.Folder childFolder in childFolders)
{
// Write the folder path.
//Debug.WriteLine(childFolder.FolderPath);
// Call EnumerateFolders using childFolder.
// Uses recursion to enumerate Outlook subfolders.
EnumerateFolders(childFolder);
}
}
return;
}
/// <summary>
/// obtain an Application object that represents an active instance of Microsoft Outlook,
/// if there is one running on the local computer, or to create a new instance of Outlook,
/// log on to the default profile, and return that instance of Outlook
/// </summary>
/// <returns></returns>
private Outlook.Application GetApplicationObject()
{
// source: https://msdn.microsoft.com/en-us/library/ff462097.aspx
Outlook.Application application = null;
// Check whether there is an Outlook process running.
if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
{
// If so, use the GetActiveObject method to obtain the process and cast it to an Application object.
application = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
}
else
{
// If not, create a new instance of Outlook and log on to the default profile.
application = new Outlook.Application();
Outlook.NameSpace nameSpace = application.GetNamespace("MAPI");
nameSpace.Logon("", "", Missing.Value, Missing.Value);
nameSpace = null;
}
// Return the Outlook Application object.
return application;
}
}//end class
}//end ns
To fill in the missing pieces, I borrowed from https://support.microsoft.com/en-us/kb/310259, for EnumerateFolders: https://msdn.microsoft.com/en-us/library/office/ff184607.aspx
You need to educate yourself on what OST/PST is as the access to them is not much different, both are Store objects so they have the same interface.
Try this sources for a start and experiment yourself as it's the best way to understand how stuff works.
http://en.wikipedia.org/wiki/Personal_Storage_Table
http://msdn.microsoft.com/en-us/library/bb609139(v=office.14).aspx
http://msdn.microsoft.com/en-us/library/ff184648(v=office.14).aspx
http://msdn.microsoft.com/en-us/library/bb208208(v=office.12).aspx
http://www.c-sharpcorner.com/UploadFile/rambab/OutlookIntegration10282006032802AM/OutlookIntegration.aspx

Categories

Resources