This article is mainly to. NET how to implement send e-mail as a sample analysis, so there are many involved in the sample program. None of the content outside the Net.mail was done. Recently more busy not too much time to improve its function, interested friends can download the source code to improve the next. This is not a big software, it is not a special thing, I write this article is to let everyone learn together. NET to send the realization of e-mail, I hope this article can help to do not know how to achieve the message to send friends, for has been or this aspect of the ancestors of the cattle to see this article please leave valuable suggestions.
This procedure is used. NET Framework of mail classes to complete the delivery of mail, inside the recipient mail address I used the XML to store, this involves the XML read and write operations, in addition to the attachment of the application of the dialog box. To analyze this, first we need to refer to the appropriate namespace, namely:
1using System.Net.Mail;
2using System.Xml;
3using System.IO;
First, take a look at the final operating effect (you can understand the structure of the program from the interface layer)
Two. Here's a look at the code that implements the email function, as shown in the following code:
private void Btnsend_click (object sender, EventArgs e) {System.Net.Mail.SmtpClient client = new SmtpClient (); Client. Host = This.tbSmtpServer.Text.ToString (); The host name or IP address client of the SMTP transaction used to send the message.
useDefaultCredentials = false; NetworkCredential (sender email address, sender's email password) client. Credentials = new System.Net.NetworkCredential (this.tbSender.Text.ToString (), This.tbsend
ErPassWord.Text.ToString ()); Client. Deliverymethod = Smtpdeliverymethod.network; e-mail messages are sent over the network to the SMTP server//mailmessage (sender address, recipient address) MailMessage message = new MailMessage (this.tbSender.Text.To
String (), this.lbQuester.SelectedItem.ToString ()); Message. Subject = This.tbTitle.Text.ToString (); Mail subject message. BODY = this.tbContext.Text.ToString (); Message content. bodyencoding = System.Text.Encoding.UTF8; The encoding of the message body.
Isbodyhtml = true; The following is an attachment process string attahmentname=this.tbatrrac.tExt. Trim ().
ToString (); if (!string. Equals (attahmentname, null) &&!string. Equals (Attahmentname, "")) {Attachment data = new Attachment (Attahmentname, System.Net.Mime.MediaTypeNames.
Application.octet); Message.
Attachments.Add (data); ///Send mail try {client.
Send (message);
MessageBox.Show ("e-mail sucessfully send to:" + this.lbQuester.SelectedItem.ToString ()); catch (Exception ex) {MessageBox.Show ("e-mail send fail!" + ex.)
ToString ()); } }