What are the various parts to an email message?
I'm working with these 3rd party components, and so far from what I understand are:
emails have unique messageID's (per server I would presume)
emails have headers
emails have body text that can be either html or plain text.
attachments have to be parsed using mime, and each mime part can have different types like: multipart/ applicatoin/octet-stream and filenames.
side question, where exactly is the documentation that these 3rd party components have to adhere to?
I think its time to read some RFCs. ;)
RFC2822 - Internet Message Format
http://www.faqs.org/rfcs/rfc2822.html
RFC2045 - Multipurpose Internet Mail Extensions (MIME) Part One
http://www.faqs.org/rfcs/rfc2045.html
RFC2046 - Multipurpose Internet Mail Extensions (MIME) Part Two
http://www.faqs.org/rfcs/rfc2046.html
RFC 2049 - Multipurpose Internet Mail Extensions (MIME) Part Five: Conformance Criteria and Examples
http://www.faqs.org/rfcs/rfc2049.html
RFC2821 - Simple Mail Transfer Protocol
http://www.faqs.org/rfcs/rfc2821.html
If sent by an MS Exchange server which is not configured properly, an email can be a single blob in a propriety binary format known as TNEF that is attached to a message which contain little else as a file usually named winmail.dat.
Related
Is there a way to fetch only the new emails from the mail server (e.g. when you store the fetched emails locally) instead of fetching all of them?
I guess I could fetch emails in bunches of 5 and compare, for example, the hash of the last email (or its Uid from Pop3Client.GetMessageUid()) to the hash of the fetched emails, but I am not sure if this is the best and most reliable way. I think this will be a waste of bandwidth or bombarding the server with multiple requests.
Does the protocol allow specifying a point in time (using date, last email hash or whatever), after which it should return the emails?
Currently, I am using OpenPop.NET's OpenPop.Pop3.Pop3Client.
EDIT
I have found an example here:
http://hpop.sourceforge.net/exampleDownloadUnread.php
Although it doesn't suit my needs 100% (because it fetched ALL UIDs which may be too many) it is a good starting point.
Basically, what I want to do is what this IMAP sample does by comparing UIDs:
http://www.limilabs.com/blog/get-new-emails-using-imap
EDIT 2
I have found this similar question, but it also suggest the approach that retrieves ALL UIDs:
How to retrieve only new emails using POP3 protocol
You'll need to download all of the UIDs no matter what you do because you'll need to compare them to what you've already downloaded (in some sort of UID log) as well as having the UIDs of the new messages so that you can log them to compare against the next time you connect.
It should be noted, however, that not all POP3 servers support the UIDL command (it is an extension, after all), so if your code is meant to be used with a variety of POP3 servers, it will have to deal with that. How you deal with it is up to you, but I believe that my approach in the past has been to md5sum a collection of headers (although not all of them, because some POP3 servers might add or modify headers such as X-Status or Status or some custom header). You can probably get away with just hashing the Received headers.
I am writing an SMTP parser that needs to handle standard smtp as well as SMTP messages sent from a system that I do not control, which do not include an IMF header. For example,
Standard SMTP with IMF header:
From: "Blah blah blah" blah#bluhblah.blah
To: Derp#dederp.com
Subject: This is a standard SMTP message
Date: Mon, 5 Oct 2009 11:36:07 +0530
MIME-Version: 1.0
Content-Type: text / plain
Content-Language: en-us
Testing testing 1 2 3
Bastardized SMTP:
Testing testing 1 2 3
Preferably in C#, but any language is fine.
No speculation please, I need someone who understands the SMTP / IMF specs to respond. I take that back, speculate all you want. lol
Unless you really, really want to write your own SMTP parser; you'll want a 3rd party library to help. We chose OpenPop. It works well, handles all the things we've thrown at it, and is still supported.
No, I'm not affiliated with them.
couple of things.
You don't want a SMTP parser, you want a Mime message parser. Parsing mime messages is not a trivial exercise. There are 1000s of broken message formats out there, or just plain weird formats, yet still RFC2822 (Mime Message) compliant.
I recommend just using a Mime message parser (either one from codeproject or like source -- if you want a free one), or else a commercial prouduct.
Then, just loop through the headers to see if they match your rules.
--Dave
PS: I'm partial to aspNetMime, since I wrote it.
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'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");
}
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.