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;
}
Related
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);
}
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?
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.
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);
}
}
I am using Exchange Server 2007 SP3
and I am able to connect exchange web service using following code
ExchangeServiceBinding esb = new ExchangeServiceBinding();
esb.RequestServerVersionValue = new RequestServerVersion();
esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2007_SP1;
esb.Credentials = new NetworkCredential("<user_id>", "<password>", "<domain>");
esb.Url = #"https://<server_fqdn>/ews/Exchange.asmx";
I am looking for code to download unread attachment.
Could you please help me?
I have found this:
private static void ProcessItems(ExchangeService exchangeService)
{
var offset = 0;
const int pageSize = 100;
FindItemsResults<Item> result;
do
{
var view = new ItemView(pageSize, offset)
{
SearchFilter = new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false)
};
result = exchangeService.FindItems(WellKnownFolderName.Inbox, view);
foreach (var item in result)
{
ProcessItem(item);
}
offset += pageSize;
} while (result.MoreAvailable);
}
then from the method ProcessItem you do whatever you want to do like download attachments and so on...
source: Processing items in an Exchange folder using EWS Managed API