C # Implementation Send mail, measured success (Welcome to trial feedback)

Source: Internet
Author: User

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 usingSystem.Net.Mail;7 usingSystem.Net.Mime;8 usingSystem.Net;9 usingSystem.IO; Ten  One namespaceTestform A { -     classEmail -     { the         stringSMTPHost =string. Empty; -         stringSendmailaddress =string. Empty; -         stringSendpassword =string. Empty; -         stringSenddisplayname =string. Empty; +         stringRecevivemailaddress =string. Empty; -         stringRecevivedisplayname =string. Empty; +  A         /// <summary> at         ///  -         /// </summary> -         /// <param name= "host" >Host number</param> -         /// <param name= "sendmailaddress" >Sender e-mail address</param> -         /// <param name= "Sendpassword" >Send email password</param> -         /// <param name= "Senddisplayname" >Sender Display Name</param> in         /// <param name= "recevivemailaddress" >Recipient Email Address</param> -         /// <param name= "Recevivedisplayname" >Recipient Display Name</param> to          PublicEmail (stringHoststringSendmailaddress,stringSendpassword,stringSenddisplayname,stringRecevivemailaddress,stringrecevivedisplayname) +         { -SMTPHost =host; theSendmailaddress =sendmailaddress; *Sendpassword =Sendpassword; $Senddisplayname =Senddisplayname;Panax NotoginsengRecevivemailaddress =recevivemailaddress; -Recevivedisplayname =Recevivedisplayname; the         } +  A         /// <summary> the         ///Send mail feature +         /// </summary> -         /// <param name= "Mailsubject" >message Header</param> $         /// <param name= "Mailbody" >Message Main content</param> $         /// <param name= "Isadddocument" >whether to add attachments</param> -         /// <param name= "Documentpath" >List of file paths for adding attachments</param> -         /// <returns></returns> the          Public BOOLSendmail (stringMailsubject,stringMailbody,BOOLIsadddocument, list<string>documentpath) -         {Wuyi             BOOLSendstatus =false; the             Try  -             { WuSystem.Net.Mail.SmtpClient client =NewSystem.Net.Mail.SmtpClient (SMTPHost);//determine the SMTP server address. Instantiate an SMTP client -MailAddress from=NewMailAddress (sendmailaddress, Senddisplayname, Encoding.UTF8);//constructs a sender address object AboutMailAddress to =NewMailAddress (recevivemailaddress, Recevivedisplayname, Encoding.UTF8);//constructs a recipient address object $MailMessage message =NewMailMessage ( from, to);//construct a Message object for an email -  -Message. Subject =Mailsubject; -Message. Subjectencoding =Encoding.UTF8; AMessage. Body =Mailbody; +Message. Bodyencoding =Encoding.UTF8; the  -                 //set information for a message $Client. Deliverymethod =smtpdeliverymethod.network; theMessage. Bodyencoding =System.Text.Encoding.UTF8; theMessage. isbodyhtml =false; the  the                 //if the server supports a secure connection, set the secure connection to true.  -                 //if it's Gmail, make sure it's set to True in                 if(SMTPHost = ="smpt.gmail.com") theClient. Enablessl =true; the                 Else AboutClient. Enablessl =false; the  the                 if(Isadddocument = =true) the                 { + adddocument (message, documentpath); -                 } the                 //set the user name and password. Bayi                 //string userState = message. Subject; theClient. useDefaultCredentials =false; the                 //User Login Information -NetworkCredential mycredentials =NewNetworkCredential (sendmailaddress, Sendpassword); -Client. Credentials =mycredentials; the                 //Send mail the client. Send (message); theSendstatus =true;  the             } -             Catch { } the             returnSendstatus; the         } the 94         /// <summary> the         ///Add attachments feature the         /// </summary> the         /// <param name= "message" >MailMessage Object</param>98         /// <param name= "Documentpath" >Attachment Path List</param> About         Private voidAdddocument (MailMessage message, list<string>documentpath) -         {101             foreach(stringFilePathinchdocumentpath)102             {103                 Try104                 { the                     if(File.exists (filepath))//determine if a file exists106                     {107Attachment attach =NewAttachment (filepath);//constructs an Attachment object108contentdisposition disposition = attach. Contentdisposition;//get information about a file109Disposition. CreationDate =System.IO.File.GetCreationTime (filepath); theDisposition. Modificationdate =System.IO.File.GetLastWriteTime (filepath);111Disposition. Readdate =System.IO.File.GetLastAccessTime (filepath); theMessage. Attachments.Add (attach);//add an attachment to a message113                     } the                 } the                 Catch { }       the             }             117         }118     }119}

When you do the website, you see a blog post: http://www.cnblogs.com/youring2/archive/2008/11/29/1343911.html

Very useful, special repair and write class, hope to help people to help themselves ~

C # Implementation Send mail, measured success (Welcome to trial feedback)

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.