Link to home
Start Free TrialLog in
Avatar of ecomaster
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.
ASKER CERTIFIED SOLUTION
Avatar of jhance
jhance

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Hamlet10
Hamlet10

Hi Wim,

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
Avatar of ecomaster

ASKER

Thanks Hamlet10, that would be a great help!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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:content-class\"";

                              strSql = strSql + ", \"DAV:href\" ";

                              strSql = strSql + ", \"urn:schemas:mailheader:content-class\" ";

                              strSql = strSql + ", \"DAV:displayname\" ";

                              strSql = strSql + ", \"urn:schemas:mailheader:subject\" ";

                              strSql = strSql + ", \"DAV:subject\" ";

                              strSql = strSql + ", \"urn:schemas:mailheader:date\" ";

                              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.adOpenUnspecified,

                                    ADODB.LockTypeEnum.adLockOptimistic, 1);

                       

 

                              oRs.MoveFirst();

                              while(!oRs.EOF )

                              {

                                    IFormatProvider culture = new CultureInfo("en-IE", true);

                                    string localDate = oRs.Fields["urn:schemas:mailheader:date"].Value.ToString() ;

 

                                    DateTime mailDate = DateTime.Parse(localDate,

                                          culture,

                                          DateTimeStyles.NoCurrentDateDefault);

                                    if(this.testStarted.CompareTo(mailDate) == -1)

                                    {

                                          log.Error("The error " + oRs.Fields["DAV:displayname"].Value.ToString() + " occoured please see " + oRs.Fields["DAV:href"].Value.ToString()) ;

 
                                          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) ;
                        }

}
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
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.