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.
Related
I have to prepare client application (c#) to send EDI messages over AS2. To be honest our client have no control over comunication and can see only correctly sended EDI messages.
I tried different solution and libraries to test it like http://www.edidev.com/example_edi_transmission.html, https://mattfrear.com/2010/07/13/send-as2-with-dotnet/ and other, but none of them does not works with customer AS2 provider. I thought that support of AS 2 provider will help me but for now it doesn't work.
I found a test AS2 server which cover my needs to do the tests (setting are the same as AS2 provider) but sended requests didn't shown on request list. My production requirements::
Encrypt - 3DES with XXXX.cer
Sign - SHA1 with YYYY.cer
Mdm mode - synchronous
Signed MDN - YES
So far my message which doesn't throw error (but don't work too) looks like:
MIME-Version: 1.0
Date: Thu, 12 May 2016 09:40:14 -0060
Content-Type: application/pkcs7-mime; smime-type=enveloped-data; name=smime.p7m
Content-Disposition: attachment; filename=smime.p7m
Content-Transfer-Encoding: binary
Message-ID: person#company.com
AS2-Version: 1.0
AS2-To: xxxx
AS2-From: yyyyy
Disposition-Notification-To: <person#company.com>
xxxxxxxxxxxxxxxxxxxx signed and encrypted data xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Has anyone done this kind of comunication. Any advice, library (open source), mime params need to specify, documetation ? I lost about a week on this without any progress. I will be grateful for any help :)
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 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.
Im writing a tool c# (and System.Net.Mail) which will be sending around 4000 emails fornigtly.
Can i simply loop through a list of 4000 email addresses and send emails one at a time?
or will this cause any potential problems?
Is there a better way to do this?
Thanks
First of all you must have rDNS record that will map IP address back to domain name. Without it mail services like GMail will hold your e-mails for hours or even days. Furthermore you may become blacklisted after few dozens of letters. rDNS usually can be set using hoster's control panel or by support request.
Second, don't be tempted to use your ISP's or GMail's SMTP servers. You will quickly run out of daily quota. It is usually not allowed to send more than few dozens letters (50 for GMail AFAIK). So either send mail from your own server, or use a 3rd party service like auth smtp.
Such services are useful also if you're unable to set rDNS for some reason.
P.S. If the content of your letters is OK (i.e. they are not spam actually) and rDNS/SMTP are set correctly, you can send at any rate, without pauses.
Provided you don;t break any server limitations, such as memory or disk space, technically there is no issue. However you will run into problems with with ISP's and mail forwarders who may block your domains for spam - rightly or wrongly..
You could send the 4000 emails in one go, but you do run the risk of getting marked very quickly as a spammer.
Your best option is to 'burst send' so send maybe 100 straight away, in 10mins send another 100 etc.
If you do it this way your are less likely to be seen as a spammer.
Check that the machine sending the mails has a valid PTR record - i.e., if you're sending from www.example.com with IP 1.2.3.4, that a PTR query on 1.2.3.4 will return www.example.com; I'd look into SPF and DKIM, too.
Some e-mail hosts have an incoming rate limit (e.g. Hotmail used that in the past) - "if you are sending us more than x emails/minute, you get a higher spam score". I had a script to "stir the queue", so that the per-minute rate to a single server was lowered.
Also, check this - it has plenty of good advice on e-mail content, but that's slightly off-topic.
In addition to what has been mentioned above, be sure to set up adequate logging.
I.e. you should log sent emails so if you need re-send emails you can be sure not to send duplicates.
Also, you can set up an app to log bounced emails and use this to remove addresses etc.
You can of course write your own software or purchase some.
http://www.quiksoft.com/bouncebuster/
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.