0

I would like to catch incoming emails and send them a web service (rather than just to a mail server).

--

After some searching I found a way of getting new emails via polling - see below: This may be of some help to others. Is there a way to receive messages by SMTP? Perhaps by ISAPI ???

using Limilabs.Mail;
using Limilabs.Client.IMAP;

public ActionResult checkIMAPmail()
{
    string rval = "not a sausage";

    using (Imap imap = new Imap())
    {
        imap.Connect(<mail server>);
        imap.Login(<username>, <password>);

        imap.SelectInbox();
        List<long> uids = imap.Search(Flag.Unseen);

        foreach (long uid in uids)
        {
            byte[] ourBytes = imap.GetMessageByUID(uid);
            IMail email = new MailBuilder().CreateFromEml(ourBytes);

            rval = email.Subject + " [" + email.From + "][" + email.Text + "]";
        }
        imap.Close();
    }

    return Content(rval, "text/html");
}

See also http://stackoverflow.com/questions/670183/accessing-imap-in-c-sharp for other IMAP packages, although note the change to using byte[], above.

Given that Limilabs.Mail is a paid service, I finally used MailKit:

using MailKit;
public int checkIMAPmail()
{
    int numEmails = 0;

    try {
        using (var client = new MailKit.Net.Imap.ImapClient())
        {
            client.ServerCertificateValidationCallback = (s, c, h, e) => true;
            client.Connect(ourSmtpClient);

            // disable the XOAUTH2 authentication mechanism.
            client.AuthenticationMechanisms.Remove("XOAUTH2");

            client.Authenticate(ourSmtpAdminUser, ourSmtpAdminUserPwd);

            // The Inbox folder is always available on all IMAP servers...
            var inboxFolder = client.Inbox;
            var savedFolder = client.GetFolder("saved");

            inboxFolder.Open(FolderAccess.ReadWrite);

            for (int ii = 0; ii < inboxFolder.Count; ii++)
            {
                var query = MailKit.Search.SearchQuery.NotSeen;

                foreach (var uid in inboxFolder.Search(query))
                {
                    var thisMsg = inboxFolder.GetMessage(uid);

                    string thisDate = notNullString(thisMsg.Date);
                    string thisSubject = notNullString( thisMsg.Subject);
                    string thisBody = notNullString(thisMsg.GetTextBody(0));   // plain text

                    string thisFromName = "";
                    string thisFromEmail = "";

                    if ( thisMsg.From != null)
                    {
                        // just get the first
                        foreach( var mb in thisMsg.From.Mailboxes)
                        {
                            thisFromName = notNullString( mb.Name);
                            thisFromEmail = notNullString( mb.Address);
                            break;
                        }
                    }

                    numEmails += 1;

                    // move email to saved
                    inboxFolder.MoveTo(uid, savedFolder);
                }
            }
            client.Disconnect(true);
        }
    }
    catch (Exception exc)
    {
        log2file("checkIMAPmail Error: " + exc.ToString());
    }

    return numEmails;
}
jcsubmit
  • 146
  • 2
  • 9
  • Most SMTP servers have a way of running incoming emails through scripts, and there are dedicated tools for processing them, such as `procmail.` – Max Nov 06 '16 at 13:55
  • SmarterTools say: We have something similar called the Spool Proc folder. This can be enabled under Security -> Anti-Spam administration -> Options, you will need to check Enable spool proc folder. This will allow third party applications to run checks on the messages that will be placed in the Proc folder on the spool *C:\SmarterMail\Spool* by default, the application would then scan the messages, and then place them back into the main spool folder for SmarterMail to then deliver. – jcsubmit Nov 07 '16 at 21:48

0 Answers0