开发者

Saving an auto-generated email? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

How to save MailMessage object to disk as *.eml or *.msg file

I am working on a C# program that emails people when certain conditions are met. I want to be able to save a copy of the email for record keeping and can't figure it out. I find it surprising there is just not a built it function like message.Save()开发者_开发技巧.

I have included a very basic email sample below:

MailMessage message = new MailMessage("from_email", "to_email");
message.Subject = "Email Alert"; 
message.Body = "This is a test email.";
SmtpClient Smtp = new SmtpClient("smtp server");
Smtp.Send(message);

I would like to save a copy of the email for a record. I didn't really consider all the choices of ways to store the message, sorry for that. I would like to have copy in case the recipient didn't receive the email I could forward them a copy from archive. I think a .msg would work well.

Another addition, I would like to be able to save the email and then send a batch at the end of the day. In case I receive updates that needed to be added I could have to program add new entries to the email so that recipient wouldn't be overloaded with multiple emails. However, there would be some cases where their would be escalation level when met an email would automatically be sent regardless of the time of day.


Why not BCC the email to an admin account?


Well, you weren't too specific about what you were looking for, so here are a few options:

  1. BCC yourself. This will (privately) send yourself a copy of the email.
  2. Implement a save yourself if you want to save to file. It's not that hard. Really all you want to do is save a few bits of text. We could implement it like this:

    private void SaveEmailToDisk(MailMessage message, string saveTo)
    {
        var builder = new StringBuilder();
        builder.AppendFormat("To: {0}\n", String.Join("; ", message.To.Select(m => m.Address).ToArray()));
        builder.AppendFormat("From: {0}\n",message.From.Address);
        builder.AppendFormat("Subject: {0}", message.Subject);
        builder.AppendFormat("Body: {0}", message.Body);
        File.WriteAllText(saveTo, builder.ToString());
    }
    

Of course you can tweak it to whatever needs you have.


A couple of different ways to "back up" your email messages so they could be resent if necessary:

  • ProcMail. Depending on the MTA you're using, it would be easy enough to write a ProcMail recipe to archive messages as your MTA sends them. If you're using Exchange, the same can be done on the server side of things.

  • XML Serialization. After you create each instance of the MailMessage class, serialize it and store it, either in the file system, or in a database. Should be easy enough to rehydrate instance when needed.

  • Pickup Directory. The SmtpClient class can be configured to "send" messages to a "Pickup Directory." This is normally used in a configuration where the MTA (message transport agent) is configured to watch a particular directory. Sending mail then consists of dropping a file containing an RFC 2822-compliant message into the directory, where it will shortly get collected by the MTA and sent on its way. If no MTA is configured to watch the pickup directory, the mail message will just get dropped there and sit.

    This is a useful way of testing the app that does the mailing without involving a real MTA. People tend to get grumpy when they get slammed with junk messages.

    It's also a useful technique for archiving: Configure 2 SmtpClient instances in your program: one configured to talk to your MTA and the other configured to drop the message in a pickup directory. Post each MailMessage you create to both instances and you'll have your archive.

Any one of these techniques should work for you. If you actually need to re-send the email, XML serialization might be the best option for you, as rehydrating an object instance is pretty trivial to do via XML serialization.


The important question to ask here is: Save it to where?

That's why there is not a built-in Save() method. Emails are not typically something that is easily just saved to a file-system (that's not to say they cannot be). But there is a lot of information that is not simply stored, like the To/From address, the Subject line, different parts (ie. MIME alternate parts, attachments).


Use

         MailAddress bcc = new MailAddress("youremail@domain.com");
         message.Bcc.Add(bcc);

You'll get a copy of the message.


Why not write the data to a database table before sending the email? Then, you have a log of what email was sent.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜