Searching Outlook Global Address List - c#

I'm pulling up the Global Address List from Outlook like so...
Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
AddressList gal = oApp.Session.GetGlobalAddressList();
...with the aim of eventually being able to search through this in my own application to retrieve contact information which I can then supply to a method that squirrels off an email. Unfortunately given that my own GAL has about 20K entries in (the customers much more) using a foreach or something simply doesn't work in an acceptable timeframe.
I want to pass a string like "Tom" to a method and have it return a list of possible contacts. Is this possible outside of actually opening up Outlook and creating the mail there?
Note: There are a couple of other questions similar to this but most seem to have no good answer. I'm hoping I have more luck.

Ok, after a LOT of Googling and stress I still haven't come up with a good way to do this. My work around is to search the local contacts folder of a user using this MSDN example. The local contacts folder of any of my users is generally well under a thousand (actually normally well under a hundred) and so there is no real overhead to searching it.
If the users local directory turns up nothing (or they try and send an email to an invalid address using my apps email functionality) then I get Outlook to provide me with with a non-modal "new email" window which has all the body, attachments, title and so on built for me, and the user can use Outlooks search functionality to find the address from the GAL.
Sort of like this...
if(CantFindAddressesLocally)
{
MailItem email = (MailItem)(oApp.CreateItem(OlItemType.olMailItem));
email.Subject = "MY SUBJECT";
email.Body = "MY BODY";
email.Attachments.Add(myAttachment);
email.Display(false) //popup an Outlook "New Email" window
}
Admittedly clumsy since it requires using the Outlook interface (and avoiding that was the whole point of incorporating email functionality in the first place) but at least it generates an email - the only thing left to the user is to input an address that is actually valid.

Related

Setting Outlook signature for multiple accounts

I am in the process of writing an application that sets a signature based on pre-acquired data for each Microsoft Outlook account(a user may have multiple Outlook accounts for various purposes).
I am able to set a signature for the default account, but I have yet to find how to set a signature for each Outlook account individually. I have done a lot of my own research and poked around the Microsoft.Office.Interop objects without much luck.
Is there a way to achieve this?
To choose the Outlook profile programmatically, you just use
Microsoft.Office.Interop.Outlook.Application App =
new Microsoft.Office.Interop.Outlook.Application();
NameSpace mapi = App.GetNamespace("MAPI");
mapi.Logon(profileName);
obviously setting the profileName to what is shown in the dropdown list upon starting Outlook (if you do not set a default profile in the control panel email settings).
This however is problematic in a number of ways since Outlook does not support multiple sessions even though the MAPI logon does:
http://msdn.microsoft.com/en-us/library/bb219914(v=office.12).aspx
Meaning: if Outlook is already running, you can even set NewSession to true, but it won't help. It will give you the currently-logged-in profile regardless of what name you set. If you have an Outlook zombie (I got that while testing, check with task manager), i.e. an Outlook without an UI showing up, the problem is the same.
If you can ensure Outlook does not run while doing stuff with signatures, you should be fine though.

Saving an auto-generated email? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to save MailMessage object to disk as *.eml or *.msg file
I am working on a C# program that emails people when certain conditions are met. I want to be able to save a copy of the email for record keeping and can't figure it out. I find it surprising there is just not a built it function like message.Save().
I have included a very basic email sample below:
MailMessage message = new MailMessage("from_email", "to_email");
message.Subject = "Email Alert";
message.Body = "This is a test email.";
SmtpClient Smtp = new SmtpClient("smtp server");
Smtp.Send(message);
I would like to save a copy of the email for a record. I didn't really consider all the choices of ways to store the message, sorry for that. I would like to have copy in case the recipient didn't receive the email I could forward them a copy from archive. I think a .msg would work well.
Another addition, I would like to be able to save the email and then send a batch at the end of the day. In case I receive updates that needed to be added I could have to program add new entries to the email so that recipient wouldn't be overloaded with multiple emails. However, there would be some cases where their would be escalation level when met an email would automatically be sent regardless of the time of day.
Why not BCC the email to an admin account?
Well, you weren't too specific about what you were looking for, so here are a few options:
BCC yourself. This will (privately) send yourself a copy of the email.
Implement a save yourself if you want to save to file. It's not that hard. Really all you want to do is save a few bits of text. We could implement it like this:
private void SaveEmailToDisk(MailMessage message, string saveTo)
{
var builder = new StringBuilder();
builder.AppendFormat("To: {0}\n", String.Join("; ", message.To.Select(m => m.Address).ToArray()));
builder.AppendFormat("From: {0}\n",message.From.Address);
builder.AppendFormat("Subject: {0}", message.Subject);
builder.AppendFormat("Body: {0}", message.Body);
File.WriteAllText(saveTo, builder.ToString());
}
Of course you can tweak it to whatever needs you have.
A couple of different ways to "back up" your email messages so they could be resent if necessary:
ProcMail. Depending on the MTA you're using, it would be easy enough to write a ProcMail recipe to archive messages as your MTA sends them. If you're using Exchange, the same can be done on the server side of things.
XML Serialization. After you create each instance of the MailMessage class, serialize it and store it, either in the file system, or in a database. Should be easy enough to rehydrate instance when needed.
Pickup Directory. The SmtpClient class can be configured to "send" messages to a "Pickup Directory." This is normally used in a configuration where the MTA (message transport agent) is configured to watch a particular directory. Sending mail then consists of dropping a file containing an RFC 2822-compliant message into the directory, where it will shortly get collected by the MTA and sent on its way. If no MTA is configured to watch the pickup directory, the mail message will just get dropped there and sit.
This is a useful way of testing the app that does the mailing without involving a real MTA. People tend to get grumpy when they get slammed with junk messages.
It's also a useful technique for archiving: Configure 2 SmtpClient instances in your program: one configured to talk to your MTA and the other configured to drop the message in a pickup directory. Post each MailMessage you create to both instances and you'll have your archive.
Any one of these techniques should work for you. If you actually need to re-send the email, XML serialization might be the best option for you, as rehydrating an object instance is pretty trivial to do via XML serialization.
The important question to ask here is: Save it to where?
That's why there is not a built-in Save() method. Emails are not typically something that is easily just saved to a file-system (that's not to say they cannot be). But there is a lot of information that is not simply stored, like the To/From address, the Subject line, different parts (ie. MIME alternate parts, attachments).
Use
MailAddress bcc = new MailAddress("youremail#domain.com");
message.Bcc.Add(bcc);
You'll get a copy of the message.
Why not write the data to a database table before sending the email? Then, you have a log of what email was sent.

C#: Exchange address to SMTP OR another way of getting SMTP mail from windows contacts?

I'm trying to find a way (in C#) to read a txt file in which there's a name written, and then the program should search in the address book (of Outlook or even the others address books of Windows) and resolve that name as an SMTP address.
Via OOM i can easily reach Exchange format mail addresses, but i don't know what to do with these, since i build my mail as a MailMessage object, that only supports SMTP addresses.
I've tried different ways:
1-Microsoft.Communications.Contact:
`ContactManager cm = new ContactManager();`
`List<Contact> contatti = (List<Contact>)cm.GetContactCollection();`
The second row cause me a NullReferenceException.
2-CDO library: i can't obtain anything, because it lacks some important classes such as AddressEntry.
3-MAPI library (CDO 1.2 downloaded from microsoft.com): is only full of interfaces, can't instantiate anything.
Any suggestions?
As Ben suggests, you might run into problems where the name you have is not unambiguous. You need to be prepared for this.
Apart from that issue you have two options here: Query AD for the information (using System.DirectoryServices or even better System.DirectoryServices.AccountManagement). From the entities returned, read the proxyAddresses property. This property contains the addresses of the user or contact.
If you are running Exchange 2007 or later, you can also use the EWS Managed API to resolve the name via Exchange. Use the ResolveNamed method: http://msdn.microsoft.com/en-us/library/exchangewebservices.exchangeservicebinding.resolvenames(v=exchg.140).aspx
EWS Managed API - Download: http://www.microsoft.com/download/en/details.aspx?id=13480
EWS Managed API - SDK: http://msdn.microsoft.com/en-us/library/dd633710(v=exchg.80).aspx

Wanting to email from C# with attachment like MS Word

This is really odd that I can't seem to find out how to do this, so I'm wondering if I'm missing something obvious.
I'm wanting to put a menu on our application like is found in Word and Excel, File -> Send To -> Mail Recipient (As Attachment)
Our requirements are to create and display the email with the attachment, just like Word and Excel do, not to send it automatically.
We used to be able to save the file to the temp folder and use:
Shell.Execute("mailto:my.email.com?subject=File&attachment="c:\temp.txt");
I've tried the &attach, &attachment in both VB.NET and C# with quotes, double quotes, etc. I've also tried System.Net.Mail but don't see anywhere that you can display the email, it only seems to be able to create and send.
We can't assume a default email client, it could be Outlook Express, Outlook version 2000, 2003, or 2007, or lotus notes, or ... Don't know. We have a commercial application so I don't think we can assume a specific application. Like MS Word, it needs to work for whatever is installed (or isn't installed).
I've done this using Outlook interop from Visual Studio Tools for Office:
using IntOut = Microsoft.Office.Interop.Outlook;
...
IntOut.Application app = new IntOut.Application();
IntOut.MailItem item = (IntOut.MailItem)app.CreateItem(
IntOut.OlItemType.olMailItem);
item.Subject = "Hello world";
item.Body = "Hello!";
item.Display(false); // set to true to make mail window modal
You can find some samples on MSDN here.
For the general case, there isn't a way to do this. Here's Microsoft's documentation: http://msdn.microsoft.com/en-us/library/aa767737(VS.85).aspx
If you can provide the mail client, you may get a better answer.

C# opening up a blank email with an attachment using Exchange

I'm new to the world of C# programming but was hoping someone could help me out.
I'm trying to use C# to open up a blank email in Outlook with a specified attachment.
In other words, open the email, the TO: and SUBJECT: fields are blank but the email has an attachment that is specified in my code. I want my user to be able to modify the email and send to whatever users s/he specifies. I know for sure that we have Exchange....so any ideas?
There are a number of way you can do this.
Create an Outlook addin that opens a new mail with the attachment you want via say a new toolbar button.
Do the same in Outlook VBA macro ..
Also you could create a new form with the attachment in it already and then just us that form.(but the attachment will be hard coded etc.)
Does the attachment change ? or is it the same one evey time ? what outlook version are you using ? What are you programing capabilties ?
76mel
I don't think exchange will help you much, you need to work with outlook on the users machine. You can add a reference to the Outlook interop assemblies, should be in the Com tab of your add references dialog.
Here's some links to jump start you.
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.aspx
http://www.microeye.com/resources/res_tech_vsnet.htm
Good Luck!

Categories

Resources