Integrating Outlook into C# application(.Net2.0)

C

Chocks

Hi,

I am using Outlook object library(11.0) in my application for sending an
email. It works fine if I have one outlook profile on my local pc.

Q1. Once I send an email using a profile it remains even though I logoff
from Outlook namespace. The only way to get a different profile is to quit
the application and start again. Is there any timeout limit for the logoff?

Q2. If I have more than one outlook profiles and every time tries to logon
using a different profile it prompts for a login except for the default
profile. Am I missing something while setting up a profile?

Code***************************
strProfile = "Outlook"; //profile changes according to source
Outlook.Application oApp;
oApp = new Outlook.Application();
Outlook.NameSpace oNasp = oApp.GetNamespace("MAPI");
oNasp.Logon(strProfile, Missing.Value, true, true);
Outlook.MailItem oMail =
(Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMail.To = EmailTo;
oMail.Subject = EmailSubject;
oMail.Body = EmailMessage;
oMail.Send();
oNasp.Logoff();
oApp.Quit();
oNasp = null;
oMail = null;
oApp = null;
****************
Thanks
Chocks
 
K

Ken Slovak - [MVP - Outlook]

NameSpace.Logon and Logoff don't do anything for you in terms of switching
profiles or selecting which profile to use if there is more than one and
none is set as a default to use all the time. You have to actually shut down
Outlook to open it with another profile.
 
C

Chocks

Thanks Ken,

In the code, I have oApp.Quit(); to quit Outlook. I thought that will
shutdown Outlook but looks like it doesn't. Do you have any idea how to
shutdown Outlook programatically?

I have two outlook profiles and it is set as Prompt for a profile to be
used. Thats working fine if I use the default Outlook profile. When I choose
the other profile it prompts me to login. How to avoid this login prompt?

Thanks,
Chocks
 
K

Ken Slovak - [MVP - Outlook]

Quit will force Outlook to quit unless something is holding it open, such as
unreleased Outlook derived objects that you have in your code or another
application such as synch software is keeping Outlook open. In managed code
you not only have to release your objects by setting them to null but you
also probably should call Marshal.ReleaseComObject on each one plus then
call the GC to collect all objects and wait for finalization.

Do you get the logon prompt selecting that other profile from the UI?
 
C

Chocks

Thats a good catch Ken. I will try that GC call. Thanks for that.

Yes, I do get a logon prompt when I open the other profile on Outlook. But
it doesn't prompt for my default profile.
My local Profile setup
Profile 1. Outlook (connected to my mail id)
Profile 2. test (connected to another id)
Both are from the same exchange server.

Thanks
Chocks
 
K

Ken Slovak - [MVP - Outlook]

You'd probably need to either impersonate that other user or log in as that
user to avoid the prompts.
 
C

Chocks

Thanks Ken, I will try that option.


Ken Slovak - said:
You'd probably need to either impersonate that other user or log in as that
user to avoid the prompts.
 
C

Chocks

Hi Tulasi,

The impersonate option didn't work properly. Instead, What I have done was
moved to Outlook 2007 and used the Accounts options where you can create n no
of profiles and switch between accounts when you send an email. The below
code works fine.

public int SendMail(string sProfile)
{
try
{
//Check the account profile whether it exists or not
int iRet = ChkAccountProfile(sProfile);
if (iRet == 0)
{
Outlook.Account myAccount;
Outlook.Application oApp;
oApp = new Outlook.Application();
myAccount = oApp.Session.Accounts[sProfile];
Outlook.MailItem oMail =
(Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMail.SendUsingAccount = myAccount;
oMail.To = EmailTo;
oMail.CC = EmailCC;
oMail.BCC = EmailBCC;
oMail.Subject = EmailSubject;
oMail.Body = EmailMessage;
oMail.Recipients.ResolveAll();
oMail.Send();
oApp.Quit();
Marshal.ReleaseComObject(oMail);
Marshal.ReleaseComObject(oApp);
oMail = null;
oApp = null;
}
return iRet; //Success
}
catch(Exception ex)
{
//May record the errors into the db here.
return 2; //Failed for some reason
}
}



Chocks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top