Save MailMessage message information as a local EML file in. Net

Source: Internet
Author: User
Tags mailmessage smtpclient

Recently do not know whether the IP of the server is 163 blocked, the site sent out the mail, so think of a way to save the message, and then download the reply, find a lot of methods are not good use, I tried some, according to the garden some code rewrite, measured available

Define the extension method first:

The framework version used for. NET 3.5

namespacesystem.net.mail{ Public Static classMailmessageextend {//1         Public Static byte[] ToArray ( Thismailmessage msg) {            ConstBindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic |BindingFlags.FlattenHierarchy; using(varms =NewMemoryStream ()) {                varAssembly =typeof(System.Net.Mail.SmtpClient).                Assembly; varWritertype = assembly. GetType ("System.Net.Mail.MailWriter"); varwriter = Activator.CreateInstance (Writertype, Flags,NULL,New Object[] {ms}, CultureInfo.InvariantCulture); Msg. GetType (). GetMethod ("Send", flags). Invoke (MSG,New[] {writer,true }); returnMs.            ToArray (); }        }        //2         Public Static voidToArray ( ThisMailMessage msg,stringPath) {            ConstBindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic |BindingFlags.FlattenHierarchy; using(varms =NewMemoryStream ()) {                varAssembly =typeof(System.Net.Mail.SmtpClient).                Assembly; varWritertype = assembly. GetType ("System.Net.Mail.MailWriter"); varwriter = Activator.CreateInstance (Writertype, Flags,NULL,New Object[] {ms}, CultureInfo.InvariantCulture); Msg. GetType (). GetMethod ("Send", flags). Invoke (MSG,New[] {writer,true }); byte[] content =Ms.                ToArray (); using(varFile =NewFileStream (Path, FileMode.Create)) {file. Write (Content,0, content.                Length); }            }        }    }}

I rookie, define the extension method, how to define, make a long time only understand, actually outside also to set a layer corresponding namespace

The use of the example is very simple, it is very convenient to change, Method 2 is my own convenience to use the extended

// Generate MailMessage " 127.0.0.1 " );  Project inside the method, return MailMessage
string string. Format ("/eml/{0}.eml"
// calling the method without the path parameter is called Method 1, and you need to receive the byte array yourself

Response.Redirect (path);

Reference address

Http://www.cnblogs.com/westsource/archive/2010/11/01/1865992.html

In this blog post

The next thing to do is to use the Mailwriter internal (internal) class, which is very simple, of course:

The following code in this sentence

It's written in this way,

Probably in Msg. GetType (). GetMethod (...) This place is always an error.

Also did not find out what is the reason, have to know in the following reply, I began to think is the framework version of the reason, several versions of the background have tried not

2015-07-30

Save MailMessage message information as a local EML file in. Net

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.