Search code examples
c#smtprfc822

How to send an RFC822 file in c#


I'm trying to convert some Python code for use in a .Net website. The code retrieves a message stored in RFC822 format and sends the message to an SMTP server again using:

mail from: blah blah
rcpt to: blah blah
data
<send the text from the RFC822 file here>
.

So no parsing of the RFC822 file is required (fortunately!). In Python this is simply:

smtp = smtplib.SMTP(_SMTPSERVER)
smtp.sendmail(_FROMADDR, recipient, msg)

where the file has been read into the variable msg. Is there an easy way to do this in C#?

The built in C# SMTP objects don't offer a way to do this, or at least I haven't found a way. They seem to be based on the principle of building up a MailMessage by providing the addresses, subject and body separately. SmtpClient has a Send(string, string, string, string) method, but again this requires a separate subject and body so I guess it constructs the RFC822 formatted message for you.

If necessary I can write my own class to send the mail. It's such a simple requirement that it wouldn't take long. However if there is a way using the standard libraries they're probably less buggy than my code.


Solution

  • You're right, the inbuilt stuff doesn't offer a solution for this.

    My advice would be to simply write a bit of code that uses TcpClient and StreamReader / StreamWriter to interact with the SMTP server. It shouldn't need more than 50 lines of code.