Pregunta

I see lot of examples on how to send email with a but I'm looking to run an action checking an email account.

Does anyone know if that can be done (im sure it can) and point me to some examples?

¿Fue útil?

Solución 2

I found this code online but "POP3_Client" isn't recognized and I don't see any refrences to add it

 POP3_Client Mailbox = new POP3_Client(new >>>>IntegratedSocket<<<<<("pop.yourisp.com", 110), "yourusername", "yourpassword");
            Mailbox.Connect();
            Debug.Print("Message count: " + Mailbox.MessageCount.ToString());
            Debug.Print("Box size in bytes: " + Mailbox.BoxSize.ToString());

            uint[] Id, Size;
            Mailbox.ListMails(out Id, out Size);
            for (int Index = 0; Index < Id.Length; ++Index)
            {
                string[] Headers = Mailbox.FetchHeaders(Id[Index], new string[] { "subject", "from", "date" });
                Debug.Print("Mail ID " + Id[Index].ToString() + " is " + Size[Index].ToString() + " bytes");
                Debug.Print("Subject: " + Headers[0]);
                Debug.Print("From: " + Headers[1]);
                Debug.Print("Date: " + Headers[2]);
                Debug.Print("======================================================================");
            }

            Mailbox.Close();

Otros consejos

There are a couple of ways you can get a gmail inbox.

OpenPop

If you do want to just use POP, and you do not mind using external libraries, this looks like the best/easiest way to go. OpenPop allows you to access a secure/unsecure email account and lets you choose the port. See this post to get started.

OpenPop is an open source C#.NET code bundle that implements mail fetching and parsing. As of this writing, it only uses Microsoft .NET framework libraries to do the required. But for accessing secure pop servers, openPop can be extended by using some SSL library.

For example, to access Gmail via Pop:

POPClient poppy = new POPClient();
poppy.Connect("pop.gmail.com", 995, true);
poppy.Authenticate(username@gmail.com, "password");
int Count = poppy.GetMessageCount();
if (Count > 0)
{
   for (int i = Count; i >= 1; i -= 1)
   {
     OpenPOP.MIMEParser.Message m = poppy.GetMessage(i, false);
     //use the parsed mail in variable 'm'
   }
}

TcpClient POP3:

To retrieve emails from any provider via Pop3, you could use a TcpClient. With Gmail, it is only slightly different, because Gmail uses SSL and port 995 for POP. There is an example of that here:

// create an instance of TcpClient 

TcpClient tcpclient = new TcpClient();     

// HOST NAME POP SERVER and gmail uses port number 995 for POP 

tcpclient.Connect("pop.gmail.com", 995); 

// This is Secure Stream // opened the connection between client and POP Server

System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream());

// authenticate as client  

 sslstream.AuthenticateAsClient("pop.gmail.com");

Gmail Atom Feed:

The first way is to use GmailAtomFeed, which is part of the C# .Net Gmail Tools. The website says:

The GmailAtomFeed class provides a simple object layer for programmatic access to gmails atom feed. In just a couple lines of code the feed will be retreived from gmail and parsed. After that the entries can be accessed through an object layer AtomFeedEntryCollection, plus access to the raw feed and the feeds XmlDocument is also available.

And this is an example of how you use it:

   // Create the object and get the feed 
   RC.Gmail.GmailAtomFeed gmailFeed = new RC.Gmail.GmailAtomFeed("username", "password"); 
   gmailFeed.GetFeed(); 

   // Access the feeds XmlDocument 
   XmlDocument myXml = gmailFeed.FeedXml 

   // Access the raw feed as a string 
   string feedString = gmailFeed.RawFeed 

   // Access the feed through the object 
   string feedTitle = gmailFeed.Title; 
   string feedTagline = gmailFeed.Message; 
   DateTime feedModified = gmailFeed.Modified; 

   //Get the entries 
   for(int i = 0; i &lt; gmailFeed.FeedEntries.Count; i++) { 
      entryAuthorName = gmailFeed.FeedEntries[i].FromName; 
      entryAuthorEmail = gmailFeed.FeedEntries[i].FromEmail; 
      entryTitle = gmailFeed.FeedEntries[i].Subject; 
      entrySummary = gmailFeed.FeedEntries[i].Summary; 
      entryIssuedDate = gmailFeed.FeedEntries[i].Received; 
      entryId = gmailFeed.FeedEntries[i].Id; 
   }

IMAP

Another way, if you are not LIMITED to POP, is to use IMAP. With IMAP, you can connect to a SSL server and choose a port along with that:

using (Imap imap = new Imap())
{
    imap.ConnectSSL("imap.gmail.com", 993);
    imap.Login("angel_y@company.com", "xyx***"); // MailID As Username and Password

    imap.SelectInbox();
    List<long> uids = imap.SearchFlag(Flag.Unseen);
    foreach (long uid in uids)
    {
        string eml = imap.GetMessageByUID(uid);
        IMail message = new MailBuilder()
            .CreateFromEml(eml);

        Console.WriteLine(message.Subject);
        Console.WriteLine(message.TextDataString);
    }
    imap.Close(true);
} 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top