Skip to main content

To get the email addresses from aliases names : Outlook Programming using C#

The Girl Who Kicked the Hornet's NestBreach of TrustBreach of Trust

To get the email addresses from aliases names : Outlook Programming using C#
IDE : Visual Studio 2008

Please ignore inappropriate content as put entire code since I am adding some more functionality other than finding the address through aliases names.

using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Outlook;
using System.Runtime.InteropServices;
using System.IO;
using ObjOutlook = Microsoft.Office.Interop.Outlook;



namespace Stmp_Client_2008
{
public partial class Form1 : Form
{
public String MailsSub;

public String Cc;
public String To;
public String Bcc;
public String SenderMailAddr;


private static ArrayList emailCollection = new ArrayList();
public static Boolean SuccMail = false;
public static Boolean MailFlag = false;

ObjOutlook.Application App = new ObjOutlook.ApplicationClass();
ObjOutlook.NameSpace NS;
ObjOutlook.MAPIFolder CheckFold;
ObjOutlook.MAPIFolder MovingFold;
ObjOutlook.MAPIFolder ErrMovingFold;



const int S_OK = 0;

[DllImport("MAPI32.DLL", CharSet = CharSet.Ansi, EntryPoint = "HrGetOneProp@12")]
private static extern void HrGetOneProp(IntPtr pmp, uint ulPropTag, out IntPtr ppProp);

[DllImport("MAPI32.DLL", CharSet = CharSet.Ansi, EntryPoint = "HrSetOneProp@8")]
private static extern void HrSetOneProp(IntPtr pmp, IntPtr pprop);

[DllImport("MAPI32.DLL", CharSet = CharSet.Ansi, EntryPoint = "MAPIFreeBuffer@4")]
private static extern void MAPIFreeBuffer(IntPtr lpBuffer);

[DllImport("MAPI32.DLL", CharSet = CharSet.Ansi)]
private static extern int MAPIInitialize(IntPtr lpMapiInit);

[DllImport("MAPI32.DLL", CharSet = CharSet.Ansi)]
private static extern void MAPIUninitialize();

const string IID_IMAPIProp = "00020303-0000-0000-C000-000000000046";
const uint PR_SMTP_ADDRESS = 972947486;


public struct SPropValue
{
///
/// Property tag for the property. Property tags are 32-bit unsigned integers consisting of the property's unique identifier in the high-order 16 bits and the property's type in the low-order 16 bits.
///

public uint ulPropTag;

///
/// Reserved for MAPI; do not use.
///

public uint dwAlignPad;

///
/// Union of data values, the specific value dictated by the property type.
///

public long Value;
}


public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}
private ObjOutlook.MAPIFolder GetFolder(ObjOutlook.Folders ReadFold, string name)
{

ObjOutlook.MAPIFolder TempStr = null;



foreach (ObjOutlook.MAPIFolder MovFold in ReadFold)
{
if (MovFold.Name == name)
{
TempStr = MovFold;
}

if (MovFold.Folders.Count > 0)
{
ObjOutlook.MAPIFolder TempStr2 = null;

TempStr2 = GetFolder(MovFold.Folders, name);
if (TempStr2 != null)
{
TempStr = TempStr2;
}

}
}

return TempStr;

}

//From the URL http://weblogs.asp.net/cumpsd/articles/89697.aspx

private static void AddAddress(string emailAddress)
{

emailAddress = emailAddress.Trim();

emailAddress = emailAddress.ToLower();

if (!emailCollection.Contains(emailAddress))
{

emailCollection.Add(emailAddress);

}

} /* AddAddress */

private string GetEmailAddressFromExchange(string emailName)
{
ObjOutlook.MailItem loDummyMsg = (ObjOutlook.MailItem)App.CreateItem(ObjOutlook.OlItemType.olMailItem);
ObjOutlook.Recipient loAddress = loDummyMsg.Recipients.Add(emailName);
loAddress.Resolve();
string SMTPAddress = GetMAPIProperty(loAddress.AddressEntry.MAPIOBJECT, PR_SMTP_ADDRESS);

return SMTPAddress;
}


private string GetMAPIProperty(object oMAPIObject, uint uiPropertyTag)
{
if (oMAPIObject == null)
{
return "";
}

string sProperty = "";
IntPtr pPropValue = IntPtr.Zero;

IntPtr IUnknown = IntPtr.Zero;
IntPtr IMAPIProperty = IntPtr.Zero;

try
{
MAPIInitialize(IntPtr.Zero);

IUnknown = Marshal.GetIUnknownForObject(oMAPIObject);

Guid guidMAPIProp = new Guid(IID_IMAPIProp);

if (Marshal.QueryInterface(IUnknown, ref guidMAPIProp, out IMAPIProperty) != S_OK)
{
return "";
}

try
{
HrGetOneProp(IMAPIProperty, uiPropertyTag, out pPropValue);

if (pPropValue == IntPtr.Zero)
{
return "";
}

SPropValue propValue = (SPropValue)Marshal.PtrToStructure(pPropValue, typeof(SPropValue));

sProperty = Marshal.PtrToStringAnsi(new IntPtr(propValue.Value));
}
catch (System.Exception ex)
{
throw ex;
}
}
finally
{
if (pPropValue != IntPtr.Zero)
{
MAPIFreeBuffer(pPropValue);
}

if (IMAPIProperty != IntPtr.Zero)
{
Marshal.Release(IMAPIProperty);
}

if (IUnknown != IntPtr.Zero)
{
Marshal.Release(IUnknown);
}

MAPIUninitialize();
}

return sProperty;
}



private void button1_Click(object sender, EventArgs e)
{
NS = App.GetNamespace("MAPI");
CheckFold = GetFolder(NS.Folders, "Sample");
//MovingFold = GetFolder(NS.Folders, "HK_MailRead");
//ErrMovingFold = GetFolder(NS.Folders, "HK_ErrMail");

ObjOutlook.Items olmailItems = CheckFold.Items;
int iter=0;

try
{
//ObjOutlook.MAPIFolder ChkFld = NS.GetDefaultFolder(ObjOutlook.OlDefaultFolders.olFolderInbox);

if (CheckFold.Items.Count > 0)
{
// foreach (ObjOutlook.MailItem ListItem in ChkFld.Items)
//{
for (int iteration = 1; iteration <= CheckFold.Items.Count; iteration++)
{
ObjOutlook.MailItem ListItem = (ObjOutlook.MailItem)CheckFold.Items[iteration];


// }


MailsSub = ListItem.Subject;



if (MailsSub.ToLower().StartsWith("RE") || MailsSub.ToLower().StartsWith("Ka"))
{
//Cc = ListItem.CC.ToString();
//To = ListItem.To.ToString();
//Bcc = ListItem.To.ToString();
//SenderMailAddr = ListItem.SenderEmailAddress.ToString();
}

ObjOutlook.Items ioItems = (ObjOutlook.Items)CheckFold.Items;


string emailname;
string emailWithDomainName;


foreach (ObjOutlook.MailItem olMail in olmailItems)
{
foreach (ObjOutlook.Recipient olRecipient in olMail.Recipients)
{
// string address = olRecipient.AddressEntry.GetExchangeUser().Address.ToString();
try
{

emailname = olRecipient.Name;
emailWithDomainName = GetEmailAddressFromExchange(emailname);
emailCollection[iter] = emailWithDomainName;
iter++;
// ObjOutlook.ContactItem oCt = (ObjOutlook.ContactItem)olmailItems;
//string emaill = oCt.Email1Address.ToString();
}
catch { }
try { AddAddress(olRecipient.Address ); }
catch { }

}
}



try { Cc = ListItem.CC.ToString(); }
catch { }

try { To = ListItem.To.ToString(); }
catch { }
try { Bcc = ListItem.To.ToString(); }
catch { }
try { SenderMailAddr = ListItem.SenderEmailAddress.ToString(); }
catch { }


}
}
}
catch { }

for (int i = 0; i < emailCollection.Count; i++)
{
listBox1.Items.Add(emailCollection[i].ToString());
}

}
}
}

Happy Coding!!!

Comments

Kavita said…
this is kavita, i understood your code but i dont know how to use this.and i am getting 2 errors they are:
Error1: name'InitializeComponent' does not exist in the current context.
Error2: The name 'listBox1' does not exist in the current context.

so please can tell me clearly how it works, how i shold use
Ananda said…
Hi Kavita,
thanks for using my blog and please comment out those lines or methods, it will work. I just pasted the windows app code since I didn't put the designer code here,

It will work if you comment those means.

Happy Coding
:)
Anonymous said…
Where did you got those code, MSDN? plz provide the linkss

thanks in advance!

Popular posts from this blog

Using of global variables in C# - Drawbacks & Solutions

How using global variables can have implications on the design, maintainability, and test-ability of C# code: Harder to understand and reason about the code:       class Program     {         public static int globalCounter = 0;         static void Main()         {             globalCounter++;             Console.WriteLine(globalCounter);         }     }   In this example, the global variable globalCounter is accessible from anywhere in the program, including the Main method. It's not clear where the value of the globalCounter is updated, it could be updated in other methods or classes, making it harder to trace the flow of data and understand the source of bugs.   More prone to errors:       class Program     {         public static string globalString;         static void Main()         {             globalString = "Hello" ;             Method1();             Method2();         }         static void Method1()         {

Task Parallel Library (TPL) and Akka.NET Alternatives

Task Parallel Library (TPL) and Akka.NET are among the most commonly used libraries for parallel and concurrent programming in the .NET ecosystem. However, there are also several other options available, depending on your specific needs: Parallel Language Integrated Query (PLINQ) is a parallel programming feature of .NET that provides an easy and efficient way to perform operations on collections in parallel. LINQ (Language Integrated Query) is a powerful feature in .NET that allows developers to work with data in a more declarative and language-integrated manner. While LINQ queries are inherently sequential, PLINQ extends LINQ by providing parallel versions of the query operators, allowing some queries to execute faster by utilizing multiple processors or cores on a machine. PLINQ is great when you are working with large collections where operations might be CPU-intensive or I/O-bound and could potentially be sped up by parallel execution. Here is a simple example of a PLI

SOLID Principles with Real World examples in C#

  SOLID Principles with Real World examples in C#   SOLID principles are formed by using S Single Responsibility Principles (SRP) O Open Closed Principle (OCP) L Liskov’s Substitution Principle (LCP) I Interface Segregation Principle (ISP) D Dependency Inversion Principle (DIP)   S Single Responsibility Principles (SRP) There should never be more than one reason for a class to change, to be precise one class should have only one responsibility Single Responsibility Principles (SRP) Real world example, A perfect match for SRP is Microservices , a Microservice will not contain functionalities other than the one it is designated to do,  Example ·                   Order Processing Service, ·                   Shipment Management Service, ·                   User Authentication Service, ·                   Catalogue List Service       class OrderProcessor     {         public void Process(Order order)         {             // Check inven