應用C#編寫簡略的圖形化的可發送附件的郵件客戶端法式。本站提示廣大學習愛好者:(應用C#編寫簡略的圖形化的可發送附件的郵件客戶端法式)文章只能為提供參考,不一定能成為您想要的結果。以下是應用C#編寫簡略的圖形化的可發送附件的郵件客戶端法式正文
明天談一下C#(WinForm)若何發送帶附件的電子郵件!空話少說,先截圖服侍:
起首C#發送郵件須要smtp辦事的支撐,我也不曉得是否是C#只支撐smtp協定,不外似乎在MSDN裡,Mail這個定名空間下只要引見smtp的辦法的,似乎沒看到POP的,算了,先不要說這個
我們臨時用smtp協定來做就行了!是以起首你要確保你的發件郵箱支撐smtp辦事,據我說知,雅虎郵箱,HotMail郵箱和GMail郵箱都不支撐smtp的,不外沒事,還好我們經常使用的QQ郵箱,163郵箱,新浪郵箱等郵箱都支撐smtp的,如許我們便可以用這些郵箱來發郵件了,哈哈,不外QQ郵箱的smtp辦事默許是封閉的,須要我們手動去守舊,守舊很簡略,進入你的QQ郵箱後,選擇【設置】,在賬戶選項卡裡就有個smtp的復選框,打個勾保留一下就OK了。163郵箱和新浪郵箱守舊smtp辦事也差不多如許的,很簡略。好了 守舊好了接上去就開端來說代碼了 OK!
為了便利菜鳥懂得,我把全部法式分紅一下幾部門:
OK 以下代碼服侍:
一些全局變量,都有正文的
SmtpClient SmtpClient = null; //設置SMTP協定 MailAddress MailAddress_from = null; //設置發信人地址 固然還須要暗碼 MailAddress MailAddress_to = null; //設置收信人地址 不須要暗碼 MailMessage MailMessage_Mai = null; FileStream FileStream_my = null; //附件文件流
1.smtp辦事信息設置
#region 設置Smtp辦事器信息 /// <summary> /// 設置Smtp辦事器信息 /// </summary> /// <param name="ServerName">SMTP辦事名</param> /// <param name="Port">端標語</param> private void setSmtpClient(string ServerHost, int Port) { SmtpClient = new SmtpClient(); SmtpClient.Host = ServerHost;//指定SMTP辦事名 例如QQ郵箱為 smtp.qq.com 新浪cn郵箱為 smtp.sina.cn等 SmtpClient.Port = Port; //指定端標語 SmtpClient.Timeout = 0; //超不時間 } #endregion
2.驗證發件人信息
#region 驗證發件人信息 /// <summary> /// 驗證發件人信息 /// </summary> /// <param name="MailAddress">發件郵箱地址</param> /// <param name="MailPwd">郵箱暗碼</param> private void setAddressform(string MailAddress, string MailPwd) { //創立辦事器認證 NetworkCredential NetworkCredential_my = new NetworkCredential(MailAddress, MailPwd); //實例化發件人地址 MailAddress_from = new System.Net.Mail.MailAddress(MailAddress, textBoxX4.Text); //指定發件人信息 包含郵箱地址和郵箱暗碼 SmtpClient.Credentials = new System.Net.NetworkCredential(MailAddress_from.Address, MailPwd); ; } #endregion
3.添加附件
#region 檢測附件年夜小 private bool Attachment_MaiInit(string path) { try { FileStream_my = new FileStream(path, FileMode.Open); string name = FileStream_my.Name; int size = (int)(FileStream_my.Length / 1024/1024); FileStream_my.Close(); //掌握文件年夜小不年夜於10M if (size > 10) { MessageBox.Show("文件長度不克不及年夜於10M!你選擇的文件年夜小為"+ size.ToString()+"M","正告",MessageBoxButtons.OK,MessageBoxIcon.Warning); return false; } return true; } catch (IOException E) { MessageBox.Show(E.Message); return false; } } #endregion
4.正式發送郵件
private void btnSend_Click(object sender, EventArgs e) { //檢測附件年夜小 發件必須小於10M 不然前往 不會履行以下代碼 if (txt_Path.Text != "") { if (!Attachment_MaiInit(txt_Path.Text.Trim())) { return; } } if (txt_SmtpServer.Text == "") { MessageBox.Show("請輸出SMTP辦事器名!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (textBoxX2.Text == "") { MessageBox.Show("請輸出發件人郵箱地址!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (txtformPwd.Text == "") { MessageBox.Show("請輸出發件人郵箱暗碼!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (dataGridViewX1.Rows.Count == 0) { MessageBox.Show("請添加收件人!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (MessageBox.Show("您肯定要發送以後郵件嗎?", "訊問", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { try { //初始化Smtp辦事器信息 setSmtpClient("smtp." + txt_SmtpServer.Text.Trim() + comboBoxEx3.Text, Convert.ToInt32(numericUpDown1.Value)); } catch (Exception Ex) { MessageBox.Show("郵件發送掉敗,請肯定SMTP辦事名能否准確!" + "\n" + "技巧信息:\n" + Ex.Message, "毛病", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { //驗證發件郵箱地址和暗碼 setAddressform(textBoxX2.Text.Trim() + comboBoxEx2.Text, txtformPwd.Text.Trim()); } catch (Exception Ex) { MessageBox.Show("郵件發送掉敗,請肯定發件郵箱地址和暗碼的准確性!" + "\n" + "技巧信息:\n" + Ex.Message, "毛病", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //清空汗青發送信息 以防發送時收件人收到的毛病信息(收件人列表會赓續反復) MailMessage_Mai.To.Clear(); //添加收件人郵箱地址 foreach (DataGridViewRow row in dataGridViewX1.Rows) { MailAddress_to = new MailAddress(row.Cells["Column1"].Value.ToString()); MailMessage_Mai.To.Add(MailAddress_to); } MessageBox.Show("收件人:" + dataGridViewX1.Rows.Count.ToString() + " 個"); //發件人郵箱 MailMessage_Mai.From = MailAddress_from; //郵件主題 MailMessage_Mai.Subject = txttitle.Text; MailMessage_Mai.SubjectEncoding = System.Text.Encoding.UTF8; //郵件注釋 MailMessage_Mai.Body = Rtb_Message.Text; MailMessage_Mai.BodyEncoding = System.Text.Encoding.UTF8; //清空汗青附件 以防附件反復發送 MailMessage_Mai.Attachments.Clear(); //添加附件 MailMessage_Mai.Attachments.Add(new Attachment(txt_Path.Text.Trim(), MediaTypeNames.Application.Octet)); //注冊郵件發送終了後的處置事宜 SmtpClient.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback); //開端發送郵件 SmtpClient.SendAsync(MailMessage_Mai, "000000000"); } }
5.發送郵件後處置
#region 發送郵件後所處置的函數 private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e) { try { if (e.Cancelled) { MessageBox.Show("發送已撤消!"); } if (e.Error != null) { MessageBox.Show("郵件發送掉敗!" + "\n" + "技巧信息:\n" + e.ToString(), "毛病", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show("郵件勝利收回!", "祝賀!", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception Ex) { MessageBox.Show("郵件發送掉敗!" + "\n" + "技巧信息:\n" + Ex.Message, "毛病", MessageBoxButtons.OK, MessageBoxIcon.Error); } } #endregion