EAGetMail, How to read all emails from POP3 Gmail server? - c#

I'm using EAGetMail library to read emails from Gmail.
This is my code:
private void readMails(){
MailServer oServer=new MailServer("pop.gmail.com", "something#gmail.com", "noneedtoseethis", ServerProtocol.Pop3);
MailClient oClient = new MailClient("Client");
oServer.SSLConnection = true;
oServer.Port = 995;
try {
oClient.Connect(oServer);
MailInfo[] infos = oClient.GetMailInfos();
Console.WriteLine(infos.Length);
for (int i = 0; i < infos.Length; i++){
MailInfo info = infos[i];
Mail oMail = oClient.GetMail(info);
Console.WriteLine("From: {0}", oMail.From.ToString());
//oClient.Delete(info);
}
oClient.Quit();
} catch (Exception ep) {
Console.WriteLine(ep.Message);
}
}
Though it appears the only thing that it gets are the new messages I receive which are 2 or 3 every five minutes.
But I want to read all emails I have in the inbox, not just the new coming messages
How can I do that?

Because Gmail POP3 server doesn't work like normal POP3 server, it hides old emails automatically even the email was not deleted, so I suggest that you use IMAP4 protocol, then you will read all emails.
just change:
server address to imap.gmail.com,
server port to 993,
ServerProtocol.Pop3 to ServerProtocol.Imap4
just like this:
private void readMails(){
MailServer oServer=new MailServer("imap.gmail.com", "something#gmail.com", "noneedtoseethis", ServerProtocol.Imap4);
MailClient oClient = new MailClient("Client");
oServer.SSLConnection = true;
oServer.Port = 993;
try {
oClient.Connect(oServer);
MailInfo[] infos = oClient.GetMailInfos();
Console.WriteLine(infos.Length);
for (int i = 0; i < infos.Length; i++){
MailInfo info = infos[i];
Mail oMail = oClient.GetMail(info);
Console.WriteLine("From: {0}", oMail.From.ToString());
//oClient.Delete(info);
}
oClient.Quit();
} catch (Exception ep) {
Console.WriteLine(ep.Message);
}
}

Related

pull all contacts out of Outlook

I'm trying to pull all my contacts out of Outlook.
I have tried many things but nothing works for me,
I would love to hear from your experience what is the trivial way to do this.
This is one way I tried to do:
Using SPIRE.EMAIL
// Create an imapclient with host, user and password
ImapClient client = new ImapClient();
client.Host = "outlook.office365.com";
client.Username = "#Your email address";
client.Password = "#Your email password";
client.ConnectionProtocols = ConnectionProtocols.Ssl;
client.Port = 143;
client.Connect();
client.Select("Inbox");
// Get a collection of messages
var addressSet = new HashSet<string>();
ImapMessageCollection msg = client.GetAllMessageHeaders();
for (int i = 0; i < msg.Count; i++)
{
string address = msg[i].From.Address;
addressSet.Add(address);
}

How to connect other user outlook mail using my PC

I want to read/connect other user inbox mail(office outloook 2010) from my pc using user credentials.
But I am getting below error.
The Autodiscover service couldn't be located
plz give me a solution.
public void ConnectToExchangeServer()
{
ExchangeService exchange = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
try
{
exchange.TraceEnabled = true;
exchange.Credentials = new WebCredentials("xyz", "xyz", "xyz.in");
exchange.AutodiscoverUrl("xyz#xyz.com", RedirectionUrlValidationCallback);
exchange.Url `enter code here`= new Uri("https://usercomputername.domainname");
EnableFolderPermissions(exchange);
}
catch (Exception ex)
{
}
}
How to read only unread mails from specific inbox folder using 'MailServer'?
Here my code:
if (!Directory.Exists(mailbox))
{
Directory.CreateDirectory(mailbox);
}
MailServer oServer = new MailServer("servername",
"username", "password", ServerProtocol.Imap4);
MailClient oClient = new MailClient("TryIt");
oServer.SSLConnection = true;
oServer.Port = 143;
try
{
oClient.Connect(oServer);
MailInfo[] infos = oClient.GetMailInfos();
for (int i = 0; i < infos.Length; i++)
{
MailInfo info = infos[i];
Console.WriteLine("Index: {0}; Size: {1}; UIDL: {2}; Flags: {3}",
info.Index, info.Size, info.UIDL, info.Flags);
// Receive email from POP3 server
Mail oMail = oClient.GetMail(info);
Console.WriteLine("From: {0}", oMail.From.ToString());
Console.WriteLine("To: {0}", oMail.To.ToString());
Console.WriteLine("Subject: {0}\r\n", oMail.Subject);
if (oMail.Attachments.Length > 0)
{
for (int j = 0; j <= oMail.Attachments.Length - 1; j++)
{
System.DateTime d = System.DateTime.Now;
System.Globalization.CultureInfo cur = new System.Globalization.CultureInfo("en-US");
string sdate = d.ToString("MMddyyyyHHmmss", cur);
string fileName = String.Format("{0}\\{1}{2}{3}.eml", mailbox, sdate, d.Millisecond.ToString("d3"), i);
// Save email to local disk
oMail.SaveAs(fileName, true);
// Mark email as deleted from POP3 server.
oClient.Delete(info);
}
}
}
// Quit and purge emails marked as deleted from POP3 server.
oClient.Quit();
}
catch (Exception ep)
{
Console.WriteLine(ep.Message);
}

How to read latest email from Yahoo mail using pop3 c#

I want to read email from my yahoo mail account. I am using "OpenPop.Pop3" to read email from my yahoo mail account, I am using below code :-
using OpenPop.Pop3;
public DataTable ReadEmailsFromId()
{
DataTable table = new DataTable();
try
{
using (Pop3Client client = new Pop3Client())
{
client.Connect("pop.mail.yahoo.com", 995, true); //For SSL
client.Authenticate("Username", "Password", AuthenticationMethod.UsernameAndPassword);
int messageCount = client.GetMessageCount();
for (int i = messageCount; i > 0; i--)
{
table.Rows.Add(client.GetMessage(i).Headers.Subject, client.GetMessage(i).Headers.DateSent);
string msdId = client.GetMessage(i).Headers.MessageId;
OpenPop.Mime.Message msg = client.GetMessage(i);
OpenPop.Mime.MessagePart plainTextPart = msg.FindFirstPlainTextVersion();
string message = plainTextPart.GetBodyAsText();
}
}
}
return table;
}
Same code is able to access other mails emails like gmail,outlook but while working with yahoo mail emails i am able to get Subject, Date but when came to message part that is:
OpenPop.Mime.Message msg = client.GetMessage(i);
OpenPop.Mime.MessagePart plainTextPart = msg.FindFirstPlainTextVersion();
Its give error "The stream used to retrieve responses from was closed".
Here is the "StackTrace":
at OpenPop.Pop3.Pop3Client.IsOkResponse(String response)
at OpenPop.Pop3.Pop3Client.SendCommand(String command)
at OpenPop.Pop3.Pop3Client.Disconnect()
at OpenPop.Pop3.Pop3Client.Dispose(Boolean disposing)
at OpenPop.Pop3.Disposable.Dispose()
Please let me know if i missing something or doing something wrong.
Also I have make yahoo mail emails to be accessed anywhere using POP.
First, based on your code snippet, you are downloading each message 4 times. That's going to be super slow.
As far as why you are getting the error, I do not know. I do not get an error using MailKit:
using MimeKit;
using MailKit;
using MailKit.Net.Pop3;
public DataTable ReadEmailsFromId()
{
DataTable table = new DataTable();
try
{
using (Pop3Client client = new Pop3Client())
{
client.Connect("pop.mail.yahoo.com", 995, true); //For SSL
client.Authenticate("Username", "Password");
for (int i = client.Count - 1; i >= 0; i--)
{
var msg = client.GetMessage (i);
table.Rows.Add(msg.Subject, msg.Date);
string msdId = msg.MessageId;
string message = msg.TextBody;
}
}
}
return table;
}

how to aunthenticate password when retrieve email over SSL from POP3 server-c#

using System;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
// Add EAGetMail namespace
using EAGetMail;
namespace receiveemail
{
class Program
{
static void Main(string[] args)
{
// Create a folder named "inbox" under current directory
// to save the email retrieved.
string curpath = Directory.GetCurrentDirectory();
string mailbox = String.Format("{0}\\inbox", curpath);
// If the folder is not existed, create it.
if (!Directory.Exists(mailbox))
{
Directory.CreateDirectory(mailbox);
}
MailServer oServer = new MailServer("imap#gmail.com", "sample#gmail.com", "password", ServerProtocol.Imap4);
MailClient oClient = new MailClient("TryIt");
// Enable SSL connection
oServer.SSLConnection = true;
// Set 995 SSL POP3 port
oServer.Port = 995;
try
{
oClient.Connect(oServer);
MailInfo[] infos = oClient.GetMailInfos();
for (int i = 0; i < infos.Length; i++)
{
MailInfo info = infos[i];
Console.WriteLine("Index: {0}; Size: {1}; UIDL: {2}",
info.Index, info.Size, info.UIDL);
// Receive email from POP3 server
Mail oMail = oClient.GetMail(info);
Console.WriteLine("From: {0}", oMail.From.ToString());
Console.WriteLine("Subject: {0}\r\n", oMail.Subject);
// Generate an email file name based on date time.
System.DateTime d = System.DateTime.Now;
System.Globalization.CultureInfo cur = new
System.Globalization.CultureInfo("en-US");
string sdate = d.ToString("yyyyMMddHHmmss", cur);
string fileName = String.Format("{0}\\{1}{2}{3}.eml",
mailbox, sdate, d.Millisecond.ToString("d3"), i);
// Save email to local disk
oMail.SaveAs(fileName, true);
// Mark email as deleted from POP3 server.
oClient.Delete(info);
}
// Quit and pure emails marked as deleted from POP3 server.
oClient.Quit();
}
catch (Exception ep)
{
Console.WriteLine(ep.Message);
}
}
}
}
Okay, so this is my code to retrieve emails. I wanted help regarding how I could authenticate the password I'll be passing as a parameter in:
MailServer oServer = new MailServer("imap#gmail.com", "sample#gmail.com", "password", ServerProtocol.Imap4).
Could someone help me out?

How to get all the messages from the Imapclient folder?

I intended to get all my messages in the Inbox folder and put them into the datagridview component.But the sentence "var message = client.Inbox.GetMessage(uids.Count - i - 1);" throws an exception:The IMAP server did not return the requested message. Is there anything wrong with my code?
//get a imapclient and connect to the server
string loginemail = UserInfo.LoginEmail;
string password = UserInfo.LoginPassword;
var client = new ImapClient();
client.Connect("imap.qq.com", 993, SecureSocketOptions.SslOnConnect);
client.Authenticate(loginemail, password);
client.Inbox.Open(FolderAccess.ReadOnly);
var uids = client.Inbox.Search(SearchQuery.All);
//get all the messages from the specified folder
for (int i = 0; i < uids.Count; i++)
{
dataGridView1.Rows.Add(1);
var message = client.Inbox.GetMessage(uids.Count - i - 1);
dataGridView1.Rows[i].Cells[0].Value = message.From.ToString();
if (message.Subject != null) { dataGridView1.Rows[i].Cells[1].Value = message.Subject.ToString(); }
else { dataGridView1.Rows[i].Cells[1].Value = ""; }
dataGridView1.Rows[i].Cells[2].Value = message.Date.ToString();
}
The only way to figure out the problem is to follow the directions in the MailKit FAQ to get a protocol log and look to see what the server is sending as a reply.

Categories