# 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="/pages/qdPoXhtlCvt7gD3f6kFE" %}
[IMAP](/core-modules/event-sources/watchers/imap.md)
{% 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) { }
        }

    });
});
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.perigee.software/examples-and-demos/imap-echo-bot.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
