ecomaster
asked on
Windows service retrieve/send mails via Exchange server (without POP3).
Hi,
I need to adapt a service to retrieve/send mails via an Exchange server (without POP3 communication). In a test application I used components in the Servers, which seemed to work fine. While incorporating this in the service I came across this site: "The Outlook Object Model is unsuitable to run in a Windows service" (http://support.microsoft.com/?kbid=237913). So my question now is what is the best way to handle this. Just incorporate the components into my service or use another solution? Thanks.
Regards,
Wim.
I need to adapt a service to retrieve/send mails via an Exchange server (without POP3 communication). In a test application I used components in the Servers, which seemed to work fine. While incorporating this in the service I came across this site: "The Outlook Object Model is unsuitable to run in a Windows service" (http://support.microsoft.com/?kbid=237913). So my question now is what is the best way to handle this. Just incorporate the components into my service or use another solution? Thanks.
Regards,
Wim.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Hamlet10, that would be a great help!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the code, but for the moment I can't rely on CDO, since it's not installed by default during MS Office installation. So at the moment I'm looking for alternatives.
Here is some webdav code for access an exchange server. It is in C sharp.
Hope this is helpful
try
{
ADODB.Connection oCn = new ADODB.Connection();
ADODB.Recordset oRs = new ADODB.Recordset();
ADODB.Fields oFields;
ADODB.Field oField;
// TODO:
string sFdUrl = "http://exh1/Exchange/cistest/Inbox";
oCn.Provider = "MSDAIPP.DSO";
oCn.Open(sFdUrl, "USERNAME", "PASSWORD", -1);
if(oCn.State == 1)
{
Console.WriteLine("Good Connection");
}
else
{
log.Error("Failed to connect to mail server : " + sFdUrl) ;
}
string strSql;
strSql = "";
strSql = "select ";
strSql = strSql + " \"urn:schemas:mailheader:c ontent-cla ss\"";
strSql = strSql + ", \"DAV:href\" ";
strSql = strSql + ", \"urn:schemas:mailheader:c ontent-cla ss\" ";
strSql = strSql + ", \"DAV:displayname\" ";
strSql = strSql + ", \"urn:schemas:mailheader:s ubject\" ";
strSql = strSql + ", \"DAV:subject\" ";
strSql = strSql + ", \"urn:schemas:mailheader:d ate\" ";
strSql = strSql + ", \"DAV:date\"";
strSql = strSql + " from scope ('shallow traversal of " + "\"";
strSql = strSql + sFdUrl + "\"') ";
strSql = strSql + " WHERE \"DAV:ishidden\" = false";
strSql = strSql + " AND \"DAV:isfolder\" = false";
oRs.Open(strSql, oCn,
ADODB.CursorTypeEnum.adOpe nUnspecifi ed,
ADODB.LockTypeEnum.adLockO ptimistic, 1);
oRs.MoveFirst();
while(!oRs.EOF )
{
IFormatProvider culture = new CultureInfo("en-IE", true);
string localDate = oRs.Fields["urn:schemas:ma ilheader:d ate"].Valu e.ToString () ;
DateTime mailDate = DateTime.Parse(localDate,
culture,
DateTimeStyles.NoCurrentDa teDefault) ;
if(this.testStarted.Compar eTo(mailDa te) == -1)
{
log.Error("The error " + oRs.Fields["DAV:displaynam e"].Value. ToString() + " occoured please see " + oRs.Fields["DAV:href"].Val ue.ToStrin g()) ;
Trace.Write("Showing Mail For : " + mailDate.ToShortDateString () + " " + mailDate.ToShortTimeString ());
}
else
{
Trace.Write("Not showing : " + mailDate.ToShortDateString () + " " + mailDate.ToShortTimeString ()) ;
}
oRs.MoveNext() ;
}
}
catch(Exception ex)
{
Assert.Fail("Mail Retrival Failed. " + ex.Message + " " + ex.StackTrace) ;
}
}
Hope this is helpful
try
{
ADODB.Connection oCn = new ADODB.Connection();
ADODB.Recordset oRs = new ADODB.Recordset();
ADODB.Fields oFields;
ADODB.Field oField;
// TODO:
string sFdUrl = "http://exh1/Exchange/cistest/Inbox";
oCn.Provider = "MSDAIPP.DSO";
oCn.Open(sFdUrl, "USERNAME", "PASSWORD", -1);
if(oCn.State == 1)
{
Console.WriteLine("Good Connection");
}
else
{
log.Error("Failed to connect to mail server : " + sFdUrl) ;
}
string strSql;
strSql = "";
strSql = "select ";
strSql = strSql + " \"urn:schemas:mailheader:c
strSql = strSql + ", \"DAV:href\" ";
strSql = strSql + ", \"urn:schemas:mailheader:c
strSql = strSql + ", \"DAV:displayname\" ";
strSql = strSql + ", \"urn:schemas:mailheader:s
strSql = strSql + ", \"DAV:subject\" ";
strSql = strSql + ", \"urn:schemas:mailheader:d
strSql = strSql + ", \"DAV:date\"";
strSql = strSql + " from scope ('shallow traversal of " + "\"";
strSql = strSql + sFdUrl + "\"') ";
strSql = strSql + " WHERE \"DAV:ishidden\" = false";
strSql = strSql + " AND \"DAV:isfolder\" = false";
oRs.Open(strSql, oCn,
ADODB.CursorTypeEnum.adOpe
ADODB.LockTypeEnum.adLockO
oRs.MoveFirst();
while(!oRs.EOF )
{
IFormatProvider culture = new CultureInfo("en-IE", true);
string localDate = oRs.Fields["urn:schemas:ma
DateTime mailDate = DateTime.Parse(localDate,
culture,
DateTimeStyles.NoCurrentDa
if(this.testStarted.Compar
{
log.Error("The error " + oRs.Fields["DAV:displaynam
Trace.Write("Showing Mail For : " + mailDate.ToShortDateString
}
else
{
Trace.Write("Not showing : " + mailDate.ToShortDateString
}
oRs.MoveNext() ;
}
}
catch(Exception ex)
{
Assert.Fail("Mail Retrival Failed. " + ex.Message + " " + ex.StackTrace) ;
}
}
Use of CDO Objects require Outlook to be installed on your machine and will use outlook configurations.
WebDav is kind of complicated to use. IMAP will be the best solution to retrieve emails
and Exchange SMTP to send emails - you can use System.Web.Mail in ASP.NET and specify smtp server as exchange to send emails.
For IMAP, if you want I can send you a simple Project, which has been written to retrieve emails thru IMAP.
Thanks,
Amit
WebDav is kind of complicated to use. IMAP will be the best solution to retrieve emails
and Exchange SMTP to send emails - you can use System.Web.Mail in ASP.NET and specify smtp server as exchange to send emails.
For IMAP, if you want I can send you a simple Project, which has been written to retrieve emails thru IMAP.
Thanks,
Amit
ASKER
I found just what I was looking for on this site: www.evocorp.com/Delphi/ADDExtendedMapi.htm. Thanks for the tips and pointing me in the right direction.
I recently had the same issues. I was for security reasons not allowed to access the Exchange server using POP3. IMAP4, or WebDAV.
Instead I used CDO objects which works fine. The windows service I have written takes emails tha arrive into the mail box are transfers the information to a SQL server.
I can include all of the code from the windows service if you would like.
Regards
Hamlet