First, build a form that is sent by mail
Second, edit the code sent by the mail (including attachments)
① Attachment Add dialog boxstringfile; Private voidButton6_click (Objectsender, EventArgs e)//Add an attachment{Openfiledialog1.filter="Word file. doc|*.doc| table file. xls|*.xls| All Files |*.*";//Filter File TypesDialogResult DK = Openfiledialog1.showdialog ();//Create a variable of enum type DK to receive open this dialog box if(DK = = DialogResult.OK)//If the point is OK, the following code will be executed{file= Openfiledialog1.filename;//define a variable to receive the path//Create a file stream to convert the pathStreamReader Path =NewStreamReader (file, Encoding.default);//Encoding.default Encoding MethodMessageBox.Show ("Add attachment succeeded"); Path. Close ();//must be closed after the stream is exhausted}}② send mail function//start writing the SendEmail function below Public voidSendEmail (stringEmailshoujian,stringEmailbiaoti,stringEmailzhengwen, MailAddress emailfrom) { Try { //Create a Send mailbox and get the sender address, the recipient address, and the message header and bodyMailMessage msg =NewMailMessage ();//Create a MailMessage class that is used to send messagesMsg. To.add (Emailshoujian);//Add the recipient's email addressMsg. Subject = Emailbiaoti;//get the title of the sent messageMsg. subjectencoding = System.Text.Encoding.UTF8;//e-mail title changed to international encoding methodMsg. from = Emailfrom;//get a sender's email addressMsg. Body = Emailzhengwen;//the body content of the messageMsg. bodyencoding = System.Text.Encoding.UTF8;//change the body content of the message to the encoding methodMsg. isbodyhtml =false;//confirm that the body content is not sent in a Web page formatMsg. priority = Mailpriority.high;//Mail is sent with the highest priority level//Add an attachmentAttachment att =NewAttachment (file);//file is the path to the selected filesMsg. Attachments.Add (ATT); // //set Credentials to verify sender identitySmtpClient client =NewSmtpClient ();//allows applications to use Simple Mail Transfer Protocol (SMTP) to send e-mail messages. Client. Host ="smtp.qq.com";//set the server name of the applicationClient. Credentials =NewSystem.Net.NetworkCredential ("[email protected]","lk19920619");//Enter the user name password of the sender's mailbox to send the message//Attention!! Must be sent at the time of the sender's mailbox account Pop3/imap agreement Open, and then enter the password is QQ mailbox independent password, not QQ password!!!! Client. Send (msg);//Send//mail from address must is same as authorization user if this error, prove that the sender's mailbox is not the POP3/IMAP protocol opened, and password is QQ mailbox independent password, not QQ password //open Way in the most there is aMessageBox.Show ("sent successfully"); } Catch(Exception) {MessageBox.Show ("Please enter information correctly"); }}③ Click the Send button//Call the SendEmail function when you click the Send button, and enter all the parameters in Private voidButton1_Click (Objectsender, EventArgs e)//send button{//MailAddress represents the address of the e-mail sender or recipient. MailAddress Emailfrom =NewMailAddress ("[email protected]");//Sender e-mail address//Create a MailAddress class to write the sender's address stringEmailshoujian = Txtshoujian. Text;//Recipient Email Address stringEmailbiaoti = Txtbiaoti. Text;//message Header stringEmailzhengwen =textbox1.text;//Message ContentSendEmail (Emailshoujian, Emailbiaoti, Emailzhengwen, Emailfrom);//calling the Send mail function }
QQ Mailbox Attachment Send