WinForm Web Crawl Mailbox single mass and have attachments

Source: Internet
Author: User
Tags mailmessage smtpclient

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Threading;usingSystem.Net.Mail;usingSystem.Net;usingSystem.IO;usingSystem.Text.RegularExpressions;namespacewindowsformsapplication1{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        Private voidSendstringSenderstringPwdstringReceiverstringTitlestringcontent) {SmtpClient client=NewSmtpClient ();//Mailbox Transport Protocol classesClient. Host ="smtp.qq.com";//Mail ServerClient. Port = -;//the port number on the SMTP host, which is by default.Client. Deliverymethod = Smtpdeliverymethod.network;//How messages are sent: sent over the network to the SMTP serverClient. Credentials =NewNetworkCredential (sender, PWD);//credentials, user name and password for the sender's logon mailboxmailaddress Sender=NewMailAddress (sender);//Sender Email, the mailbox is displayedMailAddress Receiver =NewMailAddress (receiver);//recipient email, in the mailbox is displayedMailMessage MailMessage =NewMailMessage (sender, receiver);//Create an e-mail classMailmessage.subject =txttitle. Text;//titleMailmessage.body = txtcontent. Text;//content can be HTML-formatted textmailmessage.subjectencoding = System.Text.Encoding.UTF8;//Message Subject Codemailmessage.bodyencoding = System.Text.Encoding.GetEncoding ("GB2312");//Message content Encodingmailmessage.isbodyhtml =true;//whether the message content is in HTML formatmailmessage.priority = Mailpriority.high;//message priority, there are three values: high (there is a red exclamation point in front of the message subject, indicating urgency), low (there is a blue down arrow in front of the message subject, indicating slowness), normal (no display). //Add an attachment            if(file!=NULL) {Attachment ss=NewAttachment (file);//file is the path to the selected files attachment e-mail attachmentsMAILMESSAGE.ATTACHMENTS.ADD (ss); }            //Attachment ss = new Attachment (file);//file is the path to the selected files attachment e-mail attachments//MAILMESSAGE.ATTACHMENTS.ADD (ss);            Try{client. Send (mailmessage);//Send mailMailmessage.dispose ();//Freeing Resources            }            Catch(Exception ex) {MessageBox.Show (ex).            Message); }        }        Private voidButton1_Click (Objectsender, EventArgs e) {            if(string. Isnullorwhitespace (Txtadd. Text) {MessageBox.Show ("Please add a recipient mailbox"); }            Else{ListViewItem LT=NewListViewItem (); Lt. Text=Txtadd.                Text.trim ();                LISTVIEW1.ITEMS.ADD (LT); Txtadd. Text=""; }        }        Private voidButton2_Click (Objectsender, EventArgs e) {             for(inti =0; i < ListView1.Items.Count; i++) {Send (txtsend. Text, Txtpwd. Text, Listview1.items[i]. Text, Txtadd. Text,txtcontent.                Text); Thread.Sleep ( -); } MessageBox.Show ("sent successfully"); }        Private voidButton3_Click (Objectsender, EventArgs e) {             This.        Close (); }        Private voidForm1_Load (Objectsender, EventArgs e) {Txtsend. Text="[email protected]"; Txtpwd. Usesystempasswordchar=true; Textbiaoti. Text="http://1111.baidu.com/p/3860763762"; }        stringfile; Private voidButton4_Click (Objectsender, EventArgs e) {Openfiledialog1.filter="Word file. doc|*.doc| table file. xls|*.xls| All Files |*.*";//Filter File TypesDialogResult dr = Openfiledialog1.showdialog ();//Create an enumeration type of variable DR to receive open this dialog box            if(dr = = 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            }        }        //---------------------------------------------------------------         PublicString getwebcontent (string url, Encoding encode)//encode encoding{URL= Textbiaoti. Text.trim ();//Web pages that need to get source codeWebRequest request = webrequest.create (URL);//WebRequest.Create method, returns the WebRequest subclass HttpWebRequestWebResponse response = Request. GetResponse ();//Webrequest.getresponse method that returns the response to an Internet requestStream Resstream = Response. GetResponseStream ();//The WebResponse.GetResponseStream method returns a data stream from an Internet resource. Encoding enc = encoding.getencoding ("Utf-8");//if it is garbled, change to Utf-8/GB2312StreamReader sr =NewStreamReader (Resstream, ENC);//namespace: System.IO.   The StreamReader class implements a TextReader (TextReader class that represents a reader that can read a series of sequential characters) so that it reads characters from a byte stream in a specific encoding. Txtshow. Text = Sr. ReadToEnd ();//output (HTML code), TextBox control contenthtml for Multiline mode            returnTxtshow.        Text; }        Private voidButton5_click (Objectsender, EventArgs e) {            Try            {                stringx =getwebcontent (Textbiaoti.                Text, Encoding.UTF8); Txtshow. Text=""; Regex R=NewRegex (@"([a-z0-9_\.-]+) @ ([\da-z\.-]+) \. ([A-z\.] {2,6})");//RegularSystem.Text.RegularExpressions.MatchCollection MC =r.matches (x);  for(inti =0; I < MC. Count; i++)                {                    if(MC. Count = =0) {txtshow. Text="no information was queried for the email address. "; }                    Else{txtshow. Text+ = Mc[i]. Value +"\ r \ n"; }                }            }            Catch(Exception ex) {MessageBox.Show ("Error getting site information:"+Ex.            Message); }        }        Private voidButton6_click (Objectsender, EventArgs e) {ListViewItem LT=NewListViewItem (); Lt. Text=Txtshow.            SelectedText;        LISTVIEW1.ITEMS.ADD (LT); }    }}

1 This applet can now crawl in the Web address field to enter the URL and crawl this page some QQ number.

Now to introduce its principle, in the Web address bar to enter the destination URL, click to grab the mailbox to request the Web page source code and through the written regular expression to traverse the matching mailbox address and through a for loop written to the lower textbox.

2 by crawling has already shown the email address in the right text box, click the mouse to select the email address you want to add (can choose multiple), click the Add button to display the recipient text box by the assignment (if single, select one, mass please select more than one).

When you click the "Add Recipient" button: Add the Web page to fetch the email address;

When you click the Add button: Add the email address entered in the Enter recipient address bar.

3 Click the "Add Attachment" button to send the selected file as a message attachment.

Filter out the file type by Openfiledialog1.filter and open the dialog box, define the variable to receive the Open dialog box, OK after defining a variable to receive the file path, and create a stream to convert the file path.

4 First enter the sender's e-mail address and password (in order to prevent the sending of spam, the general use of enterprise mailbox as the sending address, and in the mass when the processing of delayed delivery).

Click on the "Send button" will be based on the number of recipient address automatic single or mass (the recipient address is one time, for multiple when the mass), if the successful send prompt "send success", otherwise you will be prompted to get the error message.

Defines the mailbox transport protocol class, determines the port number of the mailbox server and server SMTP, determines how the send is sent to the SMTP server for the network, establishes the e-mail class, confirms the encoding, determines and sends.

WinForm Web Crawl Mailbox single mass and have attachments

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.