# IMAP Echo bot

The IMAP bot will scan an email box and respond with the text you sent it, as well as info about attachments.&#x20;

For more information about the imap client, see it's page linked below!

{% content-ref url="../core-modules/event-sources/watchers/imap" %}
[imap](https://docs.perigee.software/core-modules/event-sources/watchers/imap)
{% endcontent-ref %}

## Demo Code

To run the demo, you would need to authenticate it properly with your email server. \
&#x20;  This demo is using SASL-OAUTH for Google GMail. If you want to supply direct username/password authentication you can do that as well.

```csharp
PerigeeApplication.ApplicationNoInit("SASL Google", (c) => {

    var SASLGoogle = MailWatcher.SASL_GoogleAPIS("fakeemailaddress@gmail.com", "mailbot_google_auth.json");
    c.AddIMAPWatcher("EmailBot", "fakeemailaddress@gmail.com", "FromMe", "smtp.gmail.com", 587, "smtp.gmail.com", 993, () => SASLGoogle, (ct, l, mail) => {

        try
        {
            if (!mail.IsAnswered)
            {
                var From = mail.FromAddresses().FirstOrDefault()?.Name ?? "";
                var SaidWhat = mail.GetOnlyInputTextBody();
                var attachments = mail.Message.Attachments.Count();

                mail.Reply(false, (b) => {

                    b.HtmlBody = $"Hello <b>{From}</b><br />  You said: {SaidWhat}<br />Attachments: {attachments}";
                    b.TextBody = $"Hello {From}\r\n  You said: {SaidWhat}\r\nAttachments: {attachments}";

                });

                mail.AddFlags(MailKit.MessageFlags.Answered | MailKit.MessageFlags.Seen);
                mail.AddLabels("bot");
            }
        }
        catch (Exception ex)
        {
            l.LogError(ex, "Uncaught exception in mail processor");
            try
            {
                mail.AddFlags(MessageFlags.Answered | MailKit.MessageFlags.Seen);
                mail.AddLabels("error");
            } catch (Exception) { }
        }

    });
});
```
