Ask or search…
⌃K
Links
📩

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.
For more information about the imap client, see it's page linked below!

Demo Code

To run the demo, you would need to authenticate it properly with your email server. This demo is using SASL-OAUTH for Google GMail. If you want to supply direct username/password authentication you can do that as well.
PerigeeApplication.ApplicationNoInit("SASL Google", (c) => {
​
var SASLGoogle = MailWatcher.SASL_GoogleAPIS("[email protected]", "mailbot_google_auth.json");
c.AddIMAPWatcher("EmailBot", "[email protected]", "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) { }
}
​
});
});