Using the OpenPOP .net client for getting messages from Gmail.
I'm wondering how I can get only the new messages?
Currently, I get the atom feed and then get as many emails as the feed has with the OpenPOP client (starting from the first).
GmailAtomFeed feed = new GmailAtomFeed("user", "pass");
feed.GetFeed();
int unread = feed.FeedEntries.Count;
POPClient client = new POPClient("pop.gmail.com", 995, "user", "pass", AuthenticationMethod.USERPASS, true);
for (int i = 0; i < unread; i++)
{
Message m = client.GetMessage(i, false);
// ...
}
Is there a better way to do this?
And how do I set the unread messages to be read?
I doubt you can do it with pop3. From my understanding POP3 doesn't support the notion of the unread\unseen email. It should be up to the client to track messages which were already shown to the user and which were not.
What you can do is switch to using IMAP protocol to access gmail. Check this link for how you can switch it on for your gmail account Getting started with IMAP for Gmail.
Now, if you're using c# there are some commercial libraries for IMAP and there free\opensource ones: like this one on codeproject: IMAP Client library using C#. What you have to do to get unseen messages is to specify "unseen" flag for the select command. Here's an example
You have to store the UIDL of each email in a local database. When you want to check for new mail, you retrieve the UIDLs on the server and see if you have if already in your local database; if not, it's a new mail.
Outlook uses the same strategy.
same Q How to retrieve only new emails using POP3 protocol
Related
I use mailkit pop3 to fetch emails, but I find it didn't fetch newest emails and each time I run it return different messages count number
using (Pop3Client client = new Pop3Client())
{
// Connect to the server
client.Connect(hostname, port, useSsl);
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate((username), password);
int messageCount = client.Count;
// var folder = client.;
// We want to download all messages
List<MimeMessage> allMessages = new List<MimeMessage>(messageCount);
for (int i = messageCount - 1; i > 0; i--)
{
var msg = client.GetMessage(i);
allMessages.Add(msg);
}
}
I supposed that all messages variable should be filled with all emails ordered from newest emails to old emails but I found all messages return old emails and not contains at all new emails at all, I searched a lot and didn't find the reason, Do you know why that happened and how can I fix that ?
I wish to find any help here and Thanks in advance
Are you connecting to GMail, by any chance?
This is a GMail behavior where, depending on your POP settings (as set in the GMail web app Settings page), will determine what messages the client sees.
This is not a MailKit issue.
From Google's own FAQ in the section titled "Emails aren't downloading correctly", it states:
After you set up POP in your Gmail settings, your emails become available
in batches. It might take a while to see all your emails.
Note: Gmail downloads a copy of every email you send or receive, except
for emails in Chats, Spam, and Trash. To avoid duplicates, Gmail doesn't
download emails sent within your mail client, but you can still see them
if you log in to Gmail.
If you continue to have problems downloading emails, try using recent
mode:
In your email client's POP settings page, find the "Email address" or
"User name" field.
Add recent: in front of your email address. For example,
recent:example#gmail.com.
If that doesn't fix the problem, try deleting your Gmail address from your email client, then re-adding it.
I can send mail with a valid Office365 account using my C# web application to a list of opt-in addresses. So it is our own data source with our customers in it. The problem is that it is probably not received by our customers. I have send a mail from the info account to my own office365 account and pasted the headers in mxtoolbox and got a message that my local pc is blacklisted. even when it is using the smtp of office365. We have dkim enabled and spf in the dns. What else should I do to troubleshoot this?
Here is my simple C# code for sending the mail (which works, but might needs additional anti-spam stuff?)
SmtpClient sc = new SmtpClient();
sc.Host = "smtp.office365.com";
sc.Credentials = new System.Net.NetworkCredential(SmtpUsername, SmtpPassword);
sc.Port = 587;
sc.EnableSsl = true;
Should I add my home IP and from the office to the SPF dns entry, even when I am using the smtp of office365?
this is the value for Authentication-Results
dkim=none (message not signed) header.d=none;ouroffice365domain.com; dmarc=none action=none
Is that the cause for getting on the blacklist? Please note that I am not trying to send spam. It is a valid data source where people opted in for. We are just migrated to office365 and need to send a mailing to our customers from our custom web application. We are aware of the limitations (10k per day) and 1 mail every 2 sec.
edit will change this: https://stackoverflow.com/a/23409351/169714 because I just used the mail address and not the first and last name of the customer.
Not having DKIM setup is not going to get you blackisted, having it set up improperly can. Your HOME PC should have nothing to do with SPF, it's the lasting sending IP which should be OFFICE 365 IP. Did you validate your DKIM and SPF by sending an email to mailtest#unlocktheinbox.com or check-auth#verifier.port25.com?
I also don't think your LOCAL PC is blacklisted, it's the IP of your INTERNET Provider that is blacklisted. Unless you have a static IP it will change.
I am trying to implement an application which can receive the mails sent to a specific email address. The scenario is that the user will send a .ppt file as an attachment to a specific email address, and my WPF application will listen to this email and once it receives the email, it will download the attached file - saving it to the hard drive.
I looked a bit, but all I found was the System.Net.Mail objects which only support sending emails through an application using the System.Net.Mail.SmtpClient class.
How can I do this in C#?
var client = new POPClient();
client.Connect("pop.gmail.com", 995, true);
client.Authenticate("admin#bendytree.com", "YourPasswordHere");
var count = client.GetMessageCount();
Message message = client.GetMessage(count);
Console.WriteLine(message.Headers.Subject);
A simple tip, that you can follow:
https://joshwright.com/tips/tips-sending-receiving-email-in-csharp/
You can use POP3 or IMAP to check for email messages and then process the email message for saving the attached .ppt file. Click here for a sample.
I would like to know how to open Outlook Express mail client for mailing through web application in Asp.Net?
I mean to say, can we call Outlook Express to send mail through web application?
For example, when there is need to send mail, I will click on a button which will open Outlook Express's New Message window. Now my message should go through Outlook Express. I will be making use of Outlook Express Address Book to store my email contacts. Now if I get any mail it will come in Outlook Express but popup message should come in my Web application that "you have an email message pending" like that somthing.
Waiting for the reply.....please
To answer you first question: Yes it is possible to send an e-mail from a web application. Try this (from the client side if you are using Silverlight):
HtmlPage.Window.Navigate(new Uri("mailto:somemailaddress#gmail.com", UriKind.Absolute));
Or just have a mailto link (in HTML):
- http://webdesign.about.com/od/beginningtutorials/a/aabegin100299.htm
However, implementing a web service to send the mail is probably better. Try these (they are for Silverlight, but you'll get the idea):
- http://deepumi.wordpress.com/tag/send-email-from-silverlight/
- http://www.michaelsnow.com/2010/06/10/silverlight-tip-of-the-day-30-sending-email-from-silverlight/
If your company does not allow you access to an SMTP server you can use Google as one. Just create a gmail account and set up the server like this (with your gmail account name and password). I think they limit the amount of mails sent out to 100 per day.
_mailClient = new SmtpClient();
_mailClient.Host = "smtp.gmail.com";
_mailClient.Port = 587;
_mailClient.EnableSsl = true;
_mailClient.UseDefaultCredentials = false;
_mailClient.Credentials = new NetworkCredential(username, password);
_mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
Take a look at this post. You can even edit the HTML message and then, through Javascript, 'navigate' to an anchor tag that points to a mailto: location. As for the pop-ups, you need to integrate either the navigator or the webpage with the POP/IMAP/whatever mailserver where the message will be stored/retrieved/received.
I'd like to write a service that periodically checks a POP3 account for new messages and based on custom business logic forwards the messages to an appropriate "To", and possibly changes the "From" as well. I might need to keep some messages on the server until certain conditions are ready for them to be forwarded.
I found a sample using Chilkat .NET components that might work:
http://www.example-code.com/csharp/pop3_forwarder.asp
My question is: Are there any other examples of this in the .NET space using any other components?
Thanks!
The following SO questions/answers might help finding components for the POP3 part of your porject:
Reading Email using Pop3 in C#
Free POP3 .NET library?
And you can use SmtpClient in System.Net.Mail for sending the mails:
Sending E-mail using C#
I implemented something very similar using MailBee's IMAP, POP and SMTP .NET components.
They're not free, I'm afraid, but I've found them to be pretty solid, and AfterLogic's support is fast.
There's also the free (including source code) LumiSoft Mail Server, that has POP3 relay support to collect messages from a POP3 server and manage them from there, you could adapt that? (It's written in C#, is nice to work with and upgrades cleanly to VS2008). I've had no problems with that either.
Try Mail.dll .NET email component. It has SSL support, POP3 and SMTP clients.
using(Pop3 pop3 = new Pop3())
{
pop3.Connect("mail.host.com"); // Connect to the server
pop3.Login("user", "password");
foreach(string uid in pop3.GetAll())
{
// Receive mail
IMail mail = new MailBuilder()
.CreateFromEml(pop3.GetMessageByUID(uid));
Console.WriteLine(mail.Subject);
}
pop3.Close(true);
}
You can download it here