. NET Magic Hall: send a message to be attached

Source: Internet
Author: User
Tags mailmessage smtpclient

First, preface

Since the job needs to be recently packaged into WebService, the code is now documented here for later review.

Second, apart write code

 Private void_sendmail (stringFormstringpwstringSubjectstring[] To,string[] cc,stringBodystringAttachmentName,byte[] attachment) {SmtpClient client=NewSmtpClient (SMTP service address, SMTP service port); Client. useDefaultCredentials=true; Client. Credentials=Newnetworkcredential (form, pw);//The client cannot be sent if the authentication information is consistent with the source address of the message sent. Deliverymethod=smtpdeliverymethod.network; MailMessage message=NewMailMessage (); Message. from=Newmailaddress (form); foreach(stringToiteminchTo ) {message.}            To.add (Toitem); }            foreach(stringCciteminchcc) {message. Cc.            ADD (Ccitem); } message. Subject=subject; Message. Subjectencoding=Encoding.UTF8; Message. Body=body; Message. Bodyencoding=Encoding.UTF8; Message. Isbodyhtml=true; Message. Priority=Mailpriority.high; Stream Stream=NULL; Attachment Data=NULL; Try            {                if(Attachment! =NULL&& attachment. Length >0) {Stream=NewMemoryStream (attachment); Data=NewAttachment (Stream, AttachmentName, System.Net.Mime.MediaTypeNames.Application.Octet); Message.                Attachments.Add (data); } client.            Send (message); }            finally            {                if(NULL!=stream) {stream.                Close (); }            }        }

Three or three ways to send

There are three ways to send mail:

①. Client. Deliverymethod = System.Net.Mail.SmtpDeliveryMethod.Network;

Send mail directly from a remote SMTP server

②. Client. Deliverymethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis;

Send mail through the SMTP service of the native IIS server (what is the next configuration SMTP server in research?). )

③. Client. Deliverymethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory;

Send the message to the local specified directory, and then send it to the destination by other means (such as via FTP)

Iv. Summary

Only after the practice of paw printing, there is nothing good to summarize.

Respect the original, reprint please indicate from: http://www.cnblogs.com/fsjohnhuang/p/3848355.html ^_^ Fat Boy John

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.