線程——QQ郵件發送

來源:互聯網
上載者:User

標籤:

一、造一個QQ郵件發送的表單

二、開始編寫關於郵件發送的代碼,以及當點發送按鈕的時候,給發送按鈕單獨造了一個新的線程。這樣如果發送的附件太多的話,如果不給發送按鈕造新的線程,便會卡住,但是如果給發送按鈕造了新的線程,就算附件很多,也不會影響到操作別的按鈕

namespace QQ郵件發送{    public partial class Form1 : Form    {        public Thread thread;//定義一個線程        public Form1()        {            InitializeComponent();                        this.im = Image.FromFile("1.jpg");            this.BackgroundImage = im;        }        string file;        //string aa;        ArrayList Lujing=new ArrayList();             private void button6_Click(object sender, EventArgs e) //添加附件        {            openFileDialog1.Filter = "Word檔案.doc|*.doc|表格檔案.xls|*.xls|所有檔案|*.*"; //篩選檔案類型            DialogResult dk = openFileDialog1.ShowDialog();  //建立一個枚舉類型的變數dk來接收開啟這個對話方塊            if (dk == DialogResult.OK) //如果點的是確定,才會執行下面的代碼            {                             string[] files = openFileDialog1.FileNames;//將擷取到的所有路徑都存到一個string類型的數組裡面                foreach (string file in files)//遍曆所有路徑,一條條的加到Lujing這個ArrayList裡面去,然後用的時候再從集合裡面一條條遍曆出來                {                    Lujing.Add(file);                    txtfujianname.AppendText(file + "\r\n");//AppendText追加文本                }                MessageBox.Show("添加附件成功");                //擷取附件的名字添加到文字框當中            }        }             //下面開始寫SendEmail函數        MailMessage msg;              public void SendEmail(string Emailshoujian, string Emailbiaoti, string Emailzhengwen, MailAddress EmailFrom)        {            try            {                //建立發送郵箱,並擷取寄件者地址,收件者地址,以及郵件標題與本文                msg = new MailMessage();  //建立一個MailMessage的類,用來發送郵件                msg.To.Add(Emailshoujian); //將收件者的郵箱地址添加進來                  msg.Subject = Emailbiaoti; //擷取一下發送郵件的標題                msg.SubjectEncoding = System.Text.Encoding.UTF8;//郵件標題改成國際編碼方式                msg.From = EmailFrom;//擷取一下寄件者的郵箱地址                msg.Body = Emailzhengwen;//郵件的本文內容                msg.BodyEncoding = System.Text.Encoding.UTF8; //將郵件的本文內容改一下編碼方式                msg.IsBodyHtml = false; //確認本文內容是不是以網頁格式發送的                msg.Priority = MailPriority.High;//郵件發送的優先等級為最高                              //添加附件                if(txtfujianname.Text!="")                {                        foreach (string path in Lujing) //路徑都存到Lujing裡面去了,全部遍曆出來                        {                            Attachment data = new Attachment(path); //Attachment:附件                            msg.Attachments.Add(data);                        }                       }                // //設定用於驗證寄件者身份的憑據                SmtpClient client = new SmtpClient(); //允許應用程式使用簡易郵件傳輸通訊協定 (SMTP) 來寄送電子郵件。                 client.Host = "smtp.qq.com"; //設定一下應用程式的伺服器名稱                client.Credentials = new System.Net.NetworkCredential("[email protected]", "XXXXXX"); //輸入寄件者郵箱的使用者名稱密碼來發送郵件                //注意!!必須在發送的時候將寄件者的郵箱賬戶POP3/IMAP協議開啟,然後輸入的密碼是QQ郵箱獨立密碼,而不是QQ密碼!!!!                client.Send(msg);//發送                //mail from address must be same as authorization user 若出現這個錯誤,證明沒有將寄件者郵箱的POP3/IMAP協議開啟,並且密碼是QQ郵箱獨立密碼,而不是QQ密碼                //開啟檔案在最有一張                MessageBox.Show("發送成功");            }            catch (Exception ex  )            {                MessageBox.Show("請正確輸入資訊");            }                   }        //當點擊發送按鈕的時候啟動新的線程,從而不會因為卡住而影響到別的按鈕的功能        private void button1_Click(object sender, EventArgs e) //發送按鈕        {            thread = new Thread(new ThreadStart(fasong)); //為button1按鈕造一個新的線程,這樣點button1的話,就會運行新造的這個線程,而不會影響到表單的主線程,button1執行的發送,點了button1如果卡住,但不會影響到別的按鈕的作用            thread.IsBackground = true;//確認新造的線程後台運行            thread.Start();        }        public void fasong()        {            // MailAddress 表示電子郵件寄件者或收件者的地址。            MailAddress EmailFrom = new MailAddress("[email protected]");  //寄件者郵箱地址 //建立一個MailAddress的類來寫寄件者的地址            string Emailshoujian = txtshoujian.Text;  //收件者郵箱地址            string Emailbiaoti = txtbiaoti.Text; //郵件標題            string Emailzhengwen = textBox1.Text; //郵件內容            SendEmail(Emailshoujian, Emailbiaoti, Emailzhengwen, EmailFrom);  //調用發送郵件函數        }        private bool isok;        private int dianxiaqux;        private int dianxiaquy;        private int chushix;        private int chushiy;        private int movex;        private int movey;        private void Form1_MouseDown(object sender, MouseEventArgs e)        {            isok = true;            dianxiaqux = Cursor.Position.X;            dianxiaquy = Cursor.Position.Y;            chushix = this.Location.X;            chushiy = this.Location.Y;        }        private void Form1_MouseMove(object sender, MouseEventArgs e)        {            if (isok)            {                movex = Cursor.Position.X;                movey = Cursor.Position.Y;                this.Location = new Point(chushix + movex - dianxiaqux, chushiy + movey - dianxiaquy);            }        }        private void Form1_MouseUp(object sender, MouseEventArgs e)        {            isok = false;        }        private void button5_Click(object sender, EventArgs e)        {            this.Close();        }        private void button7_Click(object sender, EventArgs e)        {                   }        private void button7_Click_1(object sender, EventArgs e)        {            this.WindowState = FormWindowState.Minimized;        }        Image im;        private int i = 1;        private void button8_Click(object sender, EventArgs e)        {            if (i == 0)            {                this.im = Image.FromFile("5.jpg");                this.BackgroundImage = im;            }            else if(i == 1)            {                this.im = Image.FromFile("2.jpg");                this.BackgroundImage = im;            }             else if(i == 2)            {                this.im = Image.FromFile("3.jpg");                this.BackgroundImage = im;                           }            else if (i == 3)            {                this.im = Image.FromFile("4.jpg");                this.BackgroundImage = im;            }            else if (i == 4)            {                this.im = Image.FromFile("5.jpg");                this.BackgroundImage = im;            }            else             {                this.im = Image.FromFile("1.jpg");                this.BackgroundImage = im;                i = 0;             }                         i++;        }        private void button9_Click(object sender, EventArgs e)        {                   }        string pic;        private void button9_Click_1(object sender, EventArgs e)        {                               }        //這是為了開啟添加附件的視窗,從而獲得選擇的附件的路徑         }}

三、實際操作

在這裡已經點了發送了,但是因為所添加的附件太多,如果不加線程的話這個表單便會卡住,什麼按鈕也不能點,但是給發送按鈕加了一個新的線程,這樣點擊發送就算附件太多也不會卡住,也不會影響到換膚功能的實現。

下面是點擊發送後,然後又點擊的換膚按鈕

下面是發送成功:

線程——QQ郵件發送

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.