This project is read-only.

c# read an email with a specific subject from gmail and retrieve txt attachment using imap or pop3

Topics: Help requests
Dec 18, 2015 at 11:56 PM
Edited Dec 19, 2015 at 2:11 AM
Hi All

I have tried using an earlier version of IMAP and don't seem to be winning. I am using the following code behind a button.
I have set the account I am using for 'less secure' and am able to successfully login, but after that things just hang.
I am very new to C# and still have to figure out how to see some debugging info in a messagebox or text box.
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ImapX;
using System.Diagnostics;
using System.IO;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Text.RegularExpressions;
using System.Threading;
using ImapX.Collections;
using ImapX.Constants;
using ImapX.Enums;
using ImapX.Exceptions;
using ImapX.Parsing;

namespace email_receive_and_send_imapx
    public partial class Form1 : Form
        public Form1()

        private void button2_Click(object sender, System.EventArgs e)
            var client = new ImapClient("", true);

            if (client.Connect())

                if (client.Login("", "password"))
                    // login successful
                    MessageBox.Show("login successful");
                    MessageBox.Show("login success");
                    client.Behavior.MessageFetchMode = MessageFetchMode.Full;
                    FolderCollection folders = client.Folders;
                    var messages = client.Folders["INBOX"].Search("test email", MessageFetchMode.Full);
                    var folderPath = @"C:\attachments";
                    for (int i = 0; i < messages.Length; i++)
                        if (messages[i].Attachments.Length > 0)
                            Attachment[] atts = messages[i].Attachments;
                            for (int j = 0; j < atts.Length; j++)
                                if (atts[j].FileName.Contains("test") || atts[j].FileName.Contains("txt"))
                                    atts[j].Save(folderPath, atts[j].FileName);
                                    MessageBox.Show("successful save");
First of all, Please forgive the newbie request here.
Will I be able to do the same with the latest version or will I have to alter my code ?

I wonder if someone would be so kind as to provide a specific example of using IMAPX which shows how to c# read an email with a specific subject from gmail and retrieve txt and save the attachment to file and then delete the email.

Kind regards