sinild has asked for the wisdom of the Perl Monks concerning the following question:

Last few days, I am trying hard to find a solution for this, which I failed. Hope you can help me.

I am comfortable with PERL, but not a real expert. For a project, I need to develop a PERL code which monitors an email ID. If any email is recieved, pick the corresponding Subject, From address and Message body and then dump it into the Database.

Here is my email server details:

-This is my company exchange server (MS exchange server 2007).

-As an employee, I too have an email account on this server. I dont have any other access to this exchange server.

-For this new project, IT team created a new email account. My program should monitor this account.

-We have a public webmail, "webmail2.sonicwall.com", which we can use to access emails, when we are out of corporate netywork.

When, I checked with my IT team, they mentioned me that I can use "webmail2.sonicwall.com" in my code to recieve emails.

Here is how I plan to implement this project(Let me know, if there is a better option).

-On my Linux Box, I plan to create a daemon, which will run at startup and will call a PERL script at regular intervals.

-The perl script will have the program to fetch the emails and then to dump to DB

I created a PERL code to access my emails, but it is not working. Here is the code.

use Net::POP3; my $pop = Net::POP3->new("webmail2.sonicwall.com"); my $numInboxMsgs = $pop->login('sdevassy', 'xxxxxx'); unless ($numInboxMsgs) { print "Could not login to pop3 mailbox."; exit 1; } if ($numInboxMsgs eq '0E0') { $numInboxMsgs = 0; } print "Found $numInboxMsgs messages in the Inbox.";

But, this code fails with the error "Could not login to pop3 mailbox.".

Also, I tried the following:

#!/usr/local/bin/perl use Net::IMAP::Client; my $imap = Net::IMAP::Client->new( server => 'webmail2.sonicwall.com', user => 'sdevassy', pass => 'xxxxxx', ssl => 1, # (use SSL? default no) port => 993 # (but defaults are sane) ) or die "Could not connect to IMAP server"; # everything's useless if you can't login $imap->login or die( $imap->last_error);

But, this code fails with the error "LOGIN failed. at /tmp/2test.pl line 16..".

Also, I tried to prefix the domain name along with the username. It too failed. Can you please let me know, what is going wrong. How can I access the emails.

I'm eagerly waiting for your response.

Regards,

Sinil

Replies are listed 'Best First'.
Re: Continuously Monitor and Handle emails recieved from the 2007 exchange server
by tobyink (Canon) on Jan 12, 2012 at 13:01 UTC

    Check that your login credentials actually work, by using them with an IMAP e-mail client, such as Eudora or Thunderbird. In my experience, Exchange expects usernames of the form:

    DOMAIN\login\alias

    The slashes are literally in the username. "DOMAIN" is your Windows NT-style domain, "login" is a Windows login who is able to access that e-mail address, "alias" is the e-mail address's alias setting in Exchange (this if often the first part of the e-mail address - before the "@" sign).

Re: Continuously Monitor and Handle emails recieved from the 2007 exchange server
by flexvault (Monsignor) on Jan 12, 2012 at 10:35 UTC

    I don't see anything wrong with your first code. So you should try the login using telnet.

    telnet webmail2.sonicwall.com 110 . . . <response from server> user sdevassy +OK . . . <response from server> pass xxxxxx +OK . . . <response from server> quit
    If the above doesn't work, your perl code will never work! If it does work, then we can analysis the results and help you from there.

    Good Luck.

    "Well done is better than well said." - Benjamin Franklin

      Thanks a lot. Here is my telnet log:

      [root@localhost ~]# telnet webmail2.sonicwall.com 110 Trying 10.50.128.192... Connected to webmail2.sonicwall.com. Escape character is '^]'. +OK The Microsoft Exchange POP3 service is ready. user sdevassy +OK pass xxxxxx -ERR Logon failure: unknown user name or bad password.

      I tried with user SUX\sdevassy and user SUX\sdevassy\Sinil Devassy (domain name\user name\mail box) with no luck.

      I have sent an email to my Corporate IT infrastructure Mgmt Team. Waiting response from them.. Will update you then.....

      Once again, many many thanks for pointing me the right direction.

        You are using a wrong login name. MS Exchange needs an AD account for login. This ist usually written as <DOMAIN NAME>\<USER NAME> or user@FQDNofDomain UPN (userPrincipalName) which looks like email address and often equals to the email address of the user for the sake of simplicity (just ask you AD administrator).
Re: Continuously Monitor and Handle emails recieved from the 2007 exchange server
by Anonymous Monk on Jan 12, 2012 at 10:17 UTC

    Turn on debugging

    check for errors