Handling e-mail as files
-
I have a need to write a service that deals with custom incoming/outgoing emails. I'm using the MS SMTP service on my server, without POP3 support. The plan is to pick up incoming emails from the SMTP server's drop folder, process the contents, and if necessary create a new outbound message and drop it in the SMTP server's pickup folder. The problem I'm having is that System.Web.Mail.MailMessage doesn't appear to have any methods that I can use to either create a MailMessage object from an existing file, nor does it have a method that will allow me to dynamically create an outgoing message and drop it into the pickup folder to be delivered. So far I've had no luck searching the web for 3rd party open-source libraries that perform these functions, either. (Actually, I haven't found any at all, not even commercial.) For my specific application, I don't need MIME support, or even HTML support. It's all plain text. What I do need is the ability to load a file into a mail object and have the headers automatically parsed out into a collection, with property access to From, To, Subject, etc. Any suggestions?
Grim
(aka Toby)
MCDBA, MCSD, MCP+SB
SELECT * FROM user WHERE clue IS NOT NULL GO
(0 row(s) affected)
-
I have a need to write a service that deals with custom incoming/outgoing emails. I'm using the MS SMTP service on my server, without POP3 support. The plan is to pick up incoming emails from the SMTP server's drop folder, process the contents, and if necessary create a new outbound message and drop it in the SMTP server's pickup folder. The problem I'm having is that System.Web.Mail.MailMessage doesn't appear to have any methods that I can use to either create a MailMessage object from an existing file, nor does it have a method that will allow me to dynamically create an outgoing message and drop it into the pickup folder to be delivered. So far I've had no luck searching the web for 3rd party open-source libraries that perform these functions, either. (Actually, I haven't found any at all, not even commercial.) For my specific application, I don't need MIME support, or even HTML support. It's all plain text. What I do need is the ability to load a file into a mail object and have the headers automatically parsed out into a collection, with property access to From, To, Subject, etc. Any suggestions?
Grim
(aka Toby)
MCDBA, MCSD, MCP+SB
SELECT * FROM user WHERE clue IS NOT NULL GO
(0 row(s) affected)
Write one yourself; it's quite simple. You can read about SMTP in RFC 228[^], but it's really quite simple. SMTP headers use a Key: Value pair on separate lines. Headers that span multiple lines place whitespace before the Value (thus you should deduce that headers begin at the beginning of a line). So, you could easily create a
Hashtable
and aTextReader
derivative on the SMTP mail message. CallReadLine
and parse the text. If the line begins with white space, concatenate the line with the previous value. Put the key/value pairs into theHashtable
. CallReadLine
until you encounter a \r\n pair on a line by itself.Microsoft MVP, Visual C# My Articles
-
Write one yourself; it's quite simple. You can read about SMTP in RFC 228[^], but it's really quite simple. SMTP headers use a Key: Value pair on separate lines. Headers that span multiple lines place whitespace before the Value (thus you should deduce that headers begin at the beginning of a line). So, you could easily create a
Hashtable
and aTextReader
derivative on the SMTP mail message. CallReadLine
and parse the text. If the line begins with white space, concatenate the line with the previous value. Put the key/value pairs into theHashtable
. CallReadLine
until you encounter a \r\n pair on a line by itself.Microsoft MVP, Visual C# My Articles
Umm, are you sure you don't mean RFC 821[[^](http://RFC 821 "New Window")]? 228 seems to be a one-line update to RFC 70. Anywho. I had considered writing it myself, but I was hoping to save a little time by find an existing one. I also need to generate outgoing e-mails as a text stream that I can save to file. I'd like to have a MailMessage class that automatically creates the other necessary headers that I don't want to have to manually provide. See, that's the problem. I'm too much of a perfectionist when I do my own stuff. I suffer from "Speculative Generality" syndrome. ;)
Grim
(aka Toby)
MCDBA, MCSD, MCP+SB
SELECT * FROM user WHERE clue IS NOT NULL GO
(0 row(s) affected)
-
Umm, are you sure you don't mean RFC 821[[^](http://RFC 821 "New Window")]? 228 seems to be a one-line update to RFC 70. Anywho. I had considered writing it myself, but I was hoping to save a little time by find an existing one. I also need to generate outgoing e-mails as a text stream that I can save to file. I'd like to have a MailMessage class that automatically creates the other necessary headers that I don't want to have to manually provide. See, that's the problem. I'm too much of a perfectionist when I do my own stuff. I suffer from "Speculative Generality" syndrome. ;)
Grim
(aka Toby)
MCDBA, MCSD, MCP+SB
SELECT * FROM user WHERE clue IS NOT NULL GO
(0 row(s) affected)
Yes, RFC 821. Sorry. I was trying to recall it from memory where I've packed several dozen RFCs. Numbers are just too hard to remember. :-O I'm also a perfectionist when it comes to extensibility and modularity, but honestly SMTP headers are easy to parse. It's much easier than parsing MIME headers (which really isn't that hard). That simple algorithm I gave you would work nicely. You could check out IP!Works[^], though. They've been doing library development for IP-related technology for quite some time and are pretty good products. They do have SMTP and MIME components, as well as many, many other components for all your IP-related needs (SMTP, HTTP, POP, IMAP, SSH, etc.).
Microsoft MVP, Visual C# My Articles
-
Yes, RFC 821. Sorry. I was trying to recall it from memory where I've packed several dozen RFCs. Numbers are just too hard to remember. :-O I'm also a perfectionist when it comes to extensibility and modularity, but honestly SMTP headers are easy to parse. It's much easier than parsing MIME headers (which really isn't that hard). That simple algorithm I gave you would work nicely. You could check out IP!Works[^], though. They've been doing library development for IP-related technology for quite some time and are pretty good products. They do have SMTP and MIME components, as well as many, many other components for all your IP-related needs (SMTP, HTTP, POP, IMAP, SSH, etc.).
Microsoft MVP, Visual C# My Articles
Thanks, Heath. I'll check out IP!Works. I also stumbled across Chilkat Software[^], which has a .Net mail library. (Now I'm wondering why I didn't run across these with all the searching I was doing yesterday. O.o) And finally, I'll consider writing my own if none of these commercial packages seem worth the money. (Or, god forbid, simply don't allow me to create new mails and save to file.) If you're wondering why save to file is such a big deal, I'm having trouble with my setup getting the MSSMTP service to send mail out, although it receives fine. The only way I've been able to get it to send so far is to drop files in the pickup folder. :-/ Some kind of problem with running a 3rd party mail server on the same machine (different IP address.)
Grim
(aka Toby)
MCDBA, MCSD, MCP+SB
SELECT * FROM user WHERE clue IS NOT NULL GO
(0 row(s) affected)
-
Thanks, Heath. I'll check out IP!Works. I also stumbled across Chilkat Software[^], which has a .Net mail library. (Now I'm wondering why I didn't run across these with all the searching I was doing yesterday. O.o) And finally, I'll consider writing my own if none of these commercial packages seem worth the money. (Or, god forbid, simply don't allow me to create new mails and save to file.) If you're wondering why save to file is such a big deal, I'm having trouble with my setup getting the MSSMTP service to send mail out, although it receives fine. The only way I've been able to get it to send so far is to drop files in the pickup folder. :-/ Some kind of problem with running a 3rd party mail server on the same machine (different IP address.)
Grim
(aka Toby)
MCDBA, MCSD, MCP+SB
SELECT * FROM user WHERE clue IS NOT NULL GO
(0 row(s) affected)
Most often than not, the problem is relaying. If your SMTP service is trying to send mail out address from a specific domain and the SMTP server to which you're communicating is not set up to rely for that domain, then you won't be able to send mail. The best way is to make sure that DNS resolves correctly on the machine with Microsoft SMTP Service installed and do not forward to another SMTP server, but send mail directly to the recipient's SMTP server. If, for corporate reasons, you have to relay mail, make sure the corporate SMTP server(s) has/have relaying set up for your IP address and/or domain from which you're sending. You should check out http://support.microsoft.com[^] or send mail through another SMTP service if you can't get yours fixed. Your current hack is just that - a hack. I wouldn't recommend using it in production. Fixing the SMTP service is your best option. Also keep in mind that if you're using the
System.Web.Mail.SmtpMail
class, this actually uses CDO. Any CDO server - Microsoft Virtual SMTP Services, Microsoft Exchange, even Microsoft Outlook when configured correctly - will be used and only one can be the primary CDO service (it's a registered COM server). This is similar in concept to MAPI, except that MAPI is a client API for client programs, where CDO is more of a service (they're still both APIs for COM servers). Not sure exactly what you're problem is, but I hope this helps.Microsoft MVP, Visual C# My Articles
-
Most often than not, the problem is relaying. If your SMTP service is trying to send mail out address from a specific domain and the SMTP server to which you're communicating is not set up to rely for that domain, then you won't be able to send mail. The best way is to make sure that DNS resolves correctly on the machine with Microsoft SMTP Service installed and do not forward to another SMTP server, but send mail directly to the recipient's SMTP server. If, for corporate reasons, you have to relay mail, make sure the corporate SMTP server(s) has/have relaying set up for your IP address and/or domain from which you're sending. You should check out http://support.microsoft.com[^] or send mail through another SMTP service if you can't get yours fixed. Your current hack is just that - a hack. I wouldn't recommend using it in production. Fixing the SMTP service is your best option. Also keep in mind that if you're using the
System.Web.Mail.SmtpMail
class, this actually uses CDO. Any CDO server - Microsoft Virtual SMTP Services, Microsoft Exchange, even Microsoft Outlook when configured correctly - will be used and only one can be the primary CDO service (it's a registered COM server). This is similar in concept to MAPI, except that MAPI is a client API for client programs, where CDO is more of a service (they're still both APIs for COM servers). Not sure exactly what you're problem is, but I hope this helps.Microsoft MVP, Visual C# My Articles
I had the SMTP service set up to relay for the loopback address. I wasn't using any objects or scripting languages to test with, I was just telnetting into the service (telnet localhost 25) and typing in the commands manually. There error I kept getting was "Unable to deliver the message because the destination address was misconfigured as a mail loop." So far I've had no luck deciphering what this error message actually means. :-/ I've since changed the configuration so that the server monitors only 1 IP address, and does not respond on the loopback. I'm technically running 3 SMTP services on this system. I have an SMTP proxy running on my main e-mail IP address, on port 25. I have my main mail server running on the same IP address, on a different port. All mail to this server comes through the proxy (AV checks, DNSBL, Bayesian filtering, etc.) The MSSMTP service watches a different IP address, and is not used in conjunction with any POP3 or other service. I have a custom program that picks up the incoming messages from the Drop folder and processes them. I'm using this SMTP service sort of as a message queuing system for distributed communications. The problem is, I have to be able to send messages back to the other application via SMTP. I can't set the proxy to relay, because it's not a real SMTP service, it just relays to the other mail server. The other mail server won't allow me to "watch" port 25 on the loopback and the other port on it's primary IP address. It watches the same port on all watched addresses. I'm just trying to avoid having to install a 4th SMTP-related service on the machine. I have access to a relay server, but I'm trying to avoid using it, as I want to have control over the retry intervals if initial delivery fails.
Grim
(aka Toby)
MCDBA, MCSD, MCP+SB
SELECT * FROM user WHERE clue IS NOT NULL GO
(0 row(s) affected)