How to Receive Email and Process It in a Web Application

How do I receive email and process it in a web application

I recently worked on a project that required parsing of email from gmail and updating database with certain values based on the contents of the email. I used the ezcMail (now) Zeta Components library to connect to the mail server and parse the emails.

The strategy I adopted was to filter all interesting incoming mail with a label "unprocessed". Run the PHP script via a crontab every 15 minutes. The script would connect to the mail server and open the IMAP unprocessed folder and parse each email. After inserting the interesting values into the database, the script moves the files to another IMAP folder "Proccessed".

I also found IMAP to be better than POP for this sort of processing.

Sending emails in web applications

My 2 cents:

  1. Once you have a user sign up, never roll back the registration if sending the E-Mail fails. For simple business reasons: They may not come back or re-register if it doesn't work out at the first try. Rather tolerate an incomplete registration and nag the user to confirm their E-Mail address as soon as possible.

  2. In most cases when sending an E-Mail goes wrong, your app will not get immediate feedback anyway - non-existent E-Mail addresses on valid servers will send back a "undeliverable" message with some delay; if the mail gets eaten by a spam filter, you'll get no feedback at all; in other scenarios, it may take several minutes (greylisting) to several days (mail server temporarily down) for an E-Mail to get delivered. A synchronous approach waiting for the delivery of the mail is therefore doomed IMO. Even an immediate failure (because the user entered a obviously fake address) should never result in the registration getting rolled back.

What I would do is, make account creation as easy as possible, allow the user access to the account before it is confirmed, and then nag the hell out of them to confirm their E-Mail (if necessary, limit access to certain areas until confirmation). I would prevent the creation of a second account with the same E-Mail, though, to prevent clutter.

Make sure you allow changing the E-Mail address even if the previous address hasn't been confirmed yet, and enable the user to re-request the confirmation message to a different address.

Processing incoming email

We have actually just implemented the same kind of thing.

We process the content of email messages and push the data in to our CRM via a web service. We use c# with .net 3.5

To process the mail we went with IMap . There are a few .net client libraries on CodeProject. I think we used the one from LumiSoft .

We tried WebDav but didn't have much luck. This left us with Pop3 or IMap. IMap supports folders, which we needed, so we went with that. I'm sure it would be easy to do the same thing with POP3 if your server does not support IMap.

Basically we connect our Exchange server every hour and pull down any new email and process them.
So far it's working well.

Edit: We also use SharpMimeTools to get the raw emails in to a more usable format.

Receiving emails in asp.net

SMTP is used for sending e-mail.

You need to hook into a POP3, IMAP or Exchange server to monitor for received messages.

Can you explain some more about the process that you're trying to automate? Are you trying to detect invalid e-mail addresses? If so, you can monitor a mailbox associated with the sending user for 'bounce' messages.

Receiving mails with a Servlet

Receiving mails with a servlet is like eating soup with a knife.

To receive mails, you need to have a mail server active; usually running either (or possibly both) SMTP and/or IMAP. This is the software that will end up accepting your mails from outside.

Under Unix systems, you generally the mail server push received mails into a Mail Transfer Agent (MTA) that does something mundane like writing your received mails into your mailbox, which may be either a file or a directory. However, you can configure this process to alternatively pipe (in a shell) the mail into a program of your choice. That program could be a C or Java program which then accesses your Web server doing a POST with the contents of the mail, and then you could process your incoming mail on your Web server.

But it would be much easier to process the mail in a program that is not your Web app server, perhaps an application that simply writes the mail's contents to a database, perhaps after some processing.


If you're alreading doing a lot of stuff with Java, you may find it most convenient to use and maybe modify a, no the Java mail server: It's called James. It may be easier to build mail-lets for James than to bolt some other processing on to a "standard" C mail server. However, I haven't heard much from James lately, so I don't know just how good a mail server it is, how actively it's developed, etc. You'd have to do some exploring on your own (or ask more questions here).

What does it take to build a web app that accepts an email, and posts it's contents online?

There are a couple of options for receiving email and a lot of it depends on what your host supports and how many emails you need to receive. I wrote a blog post describing the options for Rails apps. The options are pretty much identical for C# apps although if you're using windows you might have to substitute what you actually run in the command line.

The main options include polling an existing email server using POP/IMAP, making your email server execute a script when it receives a message or using a third party like CloudMailin to make and HTTP Post to your app with the message content.

Configure IIS to receive email

You need the IIS 6 manager for that - even if you have IIS 7-8-9-10 installed. The SMTP part is still only in IIS 6 :(

It's not problem to run them side-by-side and you will use IIS 6 manager only for SMTP

With this option you could send and receive emails.

The IIS 6 manager looks like this:

Sample Image

And with you Windows system you need to install the SMTP server:

Sample Image

which give you IIS 6 (SMTP only)

Sample Image

See also https://www.interserver.net/tips/kb/how-to-setup-and-configure-smtp-server-on-windows-server-2008-r2/

And http://www.vsysad.com/2017/05/install-and-configure-smtp-server-on-windows-server-2016/

and see also How to configure SMTP in IIS 7?



Related Topics



Leave a reply



Submit