I'm trying to find some code that will allow me to suck out every email in my Gmail account, it seems that the code currently out there using Atom reader only reads unread messages.
I want to read everything, subjects, body and attachments.
Is it possible, does anyone have some working code.
Dave
What you have want to do is not a simple application that we can help you out to write a mail client application, It needs a lots of effort to read many articles about how POP3 or IMAP mail clients work also you have to understand RFC 1939 and RFC 1081 documents related to these protocols. Anyway You have to use IMAP or POP3 protocols to implement you mail client application there are many articles outside which you can refer to them.
SMTP and POP3 Mail Server
A POP3 Client in C# .NET
And RFC Documents :
Post Office Protocol
You can do this via e.g. IMAP. It should be enabled at the account settings though.
There are numerous C# tutorials about using/implementing IMAP, just google for them.
Your best bet, as others have noted, is using the IMAP protocol. Note, however, that the Google IMAP implementation requires a secure connection, so it is not just a matter of implementing IMAP.
There is a C# implementation here, which also includes the secure connection stuff, but beware that there are quite a few bugs in it, concerning things like header-encoding and others, so be prepared to fix a few bugs if you decide to use it.
Learn how to read information from XML and you can get every information you want on Gmail from this feed https://mail.google.com/mail/feed/atom. I have one sample code below which reads the number of unread messages and reads the title and summary, but you can get another information like from who, attachments, ect. No extra libraries needed :)
try
{
System.Net.WebClient objClient = new System.Net.WebClient();
string response;
string title;
string summary;
//Creating a new xml document
XmlDocument doc = new XmlDocument();
//Logging in Gmail server to get data
objClient.Credentials = new System.Net.NetworkCredential("Email", "Password");
//reading data and converting to string
response = Encoding.UTF8.GetString(objClient.DownloadData(#"https://mail.google.com/mail/feed/atom"));
response = response.Replace(#"<feed version=""0.3"" xmlns=""http://purl.org/atom/ns#"">", #"<feed>");
//loading into an XML so we can get information easily
doc.LoadXml(response);
//nr of emails
nr = doc.SelectSingleNode(#"/feed/fullcount").InnerText;
//Reading the title and the summary for every email
foreach (XmlNode node in doc.SelectNodes(#"/feed/entry"))
{
title = node.SelectSingleNode("title").InnerText;
summary = node.SelectSingleNode("summary").InnerText;
}
}
}
catch (Exception exe)
{
MessageBox.Show("Check your network connection");
}
Related
I am looking to have C# connect to an e-mail account and process undelivered mail or failures. And I would like the failed address or the original e-mail to be written to a text file.
I'm sure I could figure out how to identify failed message. Example: WHERE subject CONTAINS 'failure', 'returned', etc.
What I do not know how to do is collect the failed address from the e-mail.
I was using a program called popmonger for a client at one point, but now it is collecting the wrong e-mail addresses. It is collecting mailer-daemon#gateway03.websitewelcome.com as an example instead of the original or failed e-mail. I thought it might be easier if I wrote a C# service.
The e-mail account I am connecting to is at hostgator. Does anyone know where to start on this?
Thanks,
Jake
Since the mail client being used is called popmonger, it sounds like you will be dealing with POP3 protocol and although .Net provides a way to send email via SMTP easily, it doesn't really do the same justice when checking emails.
You will have to use a 3rd party DLL that has a POP3 client to retrieve the emails. Most importantly you will need to inspect one of the failure email notifications to see what what your application will be expecting. There should be something common in the subject, body, originator address, etc. to tip off which emails are failure notifications. Then you need to see where in the body of the email it specifies the error information you need to retrieve to write to the text file. Basically your steps should go something like this:
Check for and retrieve email messages
Add each failure email object to a collection (List<T> works just fine)
Read/parse each email to extract the information you need
Write the extracted data to a file
As long as everything goes well and you get no exceptions, delete the email from the POP server with your POP client object (that way you don't read the same failure notices every time and fill your text file with redundant data)
I've used a POP3 class from this codeproject article and it does include code for attachments but I never use it for attachments because it's a bit buggy with them. For reading email body text, it hasn't failed me yet (knocking on wood).
Once you have the failure notice you can extract the failed address per your prescribed format with a simple regular expression pattern and matching it. Let's assume the body text is already retrieved from the server and you stored it in a stringed named bodyText
using System.Text.RegularExpressions;
// ...
string failedAddressPattern = #"The mail system <(?<address>.+)>";
string capturedAddress = null;
Match match = Regex.Match(bodyText, failedAddressPattern, RegexOptions.IgnoreCase);
if(match.Groups["address"].Success)
{
capturedAddress = match.Groups["address"].Value.Trim();
}
if(capturedAddress == null)
{
// do some form of debug logging here because the pattern no longer works, etc.
}
Beside text scan there is standar for that too, it's called DSN(delivery status notifications, defined in RFC 3464). You can dedect such messages by content-type header "multipart/report; report-type=delivery-status". You can check http://www.lumisoft.ee/lsWWW/download/downloads/Examples/ - it has all you need POP3/IMAP client with examples. It also has MIME message parser what you need, it has also DSN message support. Full soruce code is provided, so getting started should be easy.
I've been following this site with a lot of admiration especially on how questions are professionally answered so I decided to be fully involved.
Please, I need urgent help on a project that I have been working on for a long time but it's almost stalled now just because of a critical issue.
An aspect of the program automates email sending to clients using the free email server systems. Due to the high frequency of email sending, I observed that the email server we're sending to drops larger parts of the emails sent out and literally blocks delivery of major emails to the recipients.
I have tried to reduce the rate of sending email out but to no avail. My fear now is my IP address might have been blocked or may be blocked soon if this continue. The program is not spamming but have to be developed in order to contact a large database of recipients at a goal within short time - like about 1000 or more recipients.
I am using Webbrowser control in C# to automate the process of logging in to the mail server and sending the email out.
Now, what I want is a sample code to use publicly available web proxy servers for each email sent out such that the source IP address appears dynamic and different to the target email server each time a message is sent out to it.
I mean, I want to dynamically get and use free public proxy servers with the Webbrowser control to send out the emails. In this way I believe the email servers would not be able to reject the emails base on the IP address source. I want to know how to dynamically get literally one web proxy server for each email sent, if possible each time.
This project is very critical and this feature is a determinant. I have googled endlessly without any straight forward solution to this issue. I would, therefore, appreciate any useful help, sample codes or resources that could help me to solve this nagging problem once and for all.
Thank you!
Your problem is "free email server systems": they consider you a spammer, and the idea you suggest (spoofing IPs) will, if detected, ruin your reputation.
If you explain what you are trying to accomplish, perhaps someone here can offer a better design.
Are you trying to give people with free email accounts (like Hotmail) bulk-emailing capabilities?
First of all (if I understood your answer right), you don't have to use WebBrowser control - you can use specified .NET solutions that allows you to efficiently sending mails:
MailMessage msg = new MailMessage("from", "to", "subject", "body text");
SmtpClient client = new SmtpClient("smtp server");
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("user", "password");
client.UseDefaultCredentials = false;
client.Credentials = cred;
Client.Send(msg);
Unfortunately, if you want to send e-mails to many recipients and you want to be sure, that these messages reach the recipients - you have to do it using your own e-mail server or do it by purchase the service on a paid e-mail servers - then they will not treat you as a spammer.
But if you anyway want to send e-mails by rotation proxy servers or similar sollution - you can define your proxy:
SmtpClient client = new SmtpClient("my.proxy_server.com", 8080);
First you have to collect any list of available proxy servers which allows you to do it in reasonable time (servers switching can significantly increase total process time because conection time can be different for each proxy server)
Proxy servers list ordered by access time:
http://www.publicproxyservers.com/proxy/list_avr_time1.html
I am trying to send mail with large size attachment upto (1MB,2MB).
But sending mail fails.(Sending to Google Apps) as:
MailItemEntry[] entries = new MailItemEntry[1];
String EmlPath = "C:\\testemail.eml";
String msg = File.ReadAllText(EmlPath);
entries[0] = new MailItemEntry();
entries[0].Rfc822Msg = new Rfc822MsgElement(msg);
How can i divide attachments into multi part?
Exception I am getting while migrating this EML to Google apps is:
{"The request was aborted: The request was canceled."}
Question on Google Forum
One solution may be to use multipart zip (or other compression format that supports a similar concept) files and send each file in a seperate email.
At the least, GZip supports multipart compression as well, though I don't think either zip or gz have really good support in .NET for multipart files.
Your best bet for either is probably #ziplib.
If you're trying to send, e.g. a 2MB WMV file and your mail gateway only allows ~500kb attachments, this just isn't going to work. You can't arbitrarily split a WMV file - the email recipient would need the same software to 'join' the pieces back up.
There are archive utilities such as WinRAR that allow you to created archives split into configurable sized chunks. Then you could either send lots of attachments in one email, or lots of emails with single attachments.
An easier solution would be to upload the file somewhere and put a link in the email to download the file later. This could be your own HTTP or FTP server, or there is an abundance of 3rd party services out there that let you do just this, YouSendIt seems popular.
I know this isn't exactly an answer, but in my testing, I was able to send attachments up to 3MB in size without a problem. Are you sure you're not running into a limitation imposed by your smtp host on outbound attachment sizes?
Before everyone goes on a wild goose chase, can the original poster actually post the exception text they are seeing?
This could be anything from the web server timing out, to the mail server not accepting large attachments, to not having proper permissions.
Without seeing the exception text, and stack trace, these vauge questions can be a exercise in futility.
Not trying to be rude or anything, but the more information provided with a question, the easier it is to answer.
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
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