c#中winform根據(jù)郵箱地址和密碼一鍵發(fā)送email的實現(xiàn)
企業(yè)信息化進程中,根據(jù)自己的Email地址一鍵發(fā)送郵件,了解發(fā)送原理可以批量發(fā)送多人郵箱。原來曾經(jīng)用VB做過群發(fā)工資條,效果比較理想,現(xiàn)在使用c#做開發(fā),原理基本一樣。
應用的技術(shù):訪問郵件服務器發(fā)送郵件、文件操作保存默認信息、winform按鈕的邏輯操作
效果圖:
核心要點及代碼(這里以163為例)
1.發(fā)送代碼:這是最核心的,注意引用。文本框:發(fā)送地址,發(fā)送密碼,發(fā)送服務器,接收地址,發(fā)送主題,發(fā)送內(nèi)容。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net.Mail; using System.Text.RegularExpressions; using System.IO; ? private void button1_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? Regex r = new Regex("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"); ? ? ? ? ? ? if (!(r.IsMatch(tbSend.Text))) ?//用正則表達式驗證郵箱 ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("發(fā)送郵箱地址格式不正確!"); ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? //生成SmtpClient實例,用它發(fā)送電子郵件 ? ? ? ? ? ? MailMessage mail = new MailMessage(); ? ? ? ? ? ? mail.BodyEncoding = System.Text.Encoding.UTF8; ? ? ? ? ? ? mail.IsBodyHtml = true; ? ? ? ? ? ? mail.From = new MailAddress(tbSend.Text); ? ? ? ? ? ? mail.To.Add(new MailAddress(tbAccep.Text)); ? ? ? ? ? ? mail.Subject = tbAcceptS.Text; ? ? ? ? ? ? mail.Body = tbB.Text; ? ? ? ? ? ? //生成SmtpClient實例,用它發(fā)送電子郵件 ? ? ? ? ? ? //指定SMTP服務器主機 ? ? ? ? ? ? SmtpClient client = new SmtpClient(tbSendS.Text); ? ? ? ? ? ? client.UseDefaultCredentials = false; ? ? ? ? ? ? client.EnableSsl = true; ? ? ? ? ? ? client.Credentials = new System.Net.NetworkCredential(tbSend.Text.Substring(0, tbSend.Text.IndexOf('@')), tbSendP.Text); ? ? ? ? ? ? client.DeliveryMethod = SmtpDeliveryMethod.Network; ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? client.Send(mail); ? ? ? ? ? ? ? ? MessageBox.Show("發(fā)送成功"); ? ? ? ? ? ? } ? ? ? ? ? ? catch (Exception ex) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("發(fā)送失敗" + ex.Message.ToString()); ? ? ? ? ? ? } ? ? ? ? }
2.配置了一些方便操作的功能,比如可以把默認發(fā)送地址密碼保存在文件中,每次可以提取,還可以隨時修改默認地址和密碼。對winform的美觀性做了強化。這里展示一些代碼。有2個文本框是隱藏的,為了輸入默認地址。一鍵可以現(xiàn)實。
這2個是修改默認地址的代碼
? private void button3_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (n%2 == 0) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? textBox1.Visible = true; ? ? ? ? ? ? ? ? textBox2.Visible = true; ? ? ? ? ? ? ? ? string fileName = Environment.CurrentDirectory + "\\myText" + ".txt"; ? ? ? ? ? ? ? ? if (System.IO.File.Exists(fileName)) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? var lines = File.ReadAllLines(@fileName); ? ? ? ? ? ? ? ? ? ? string str0 = lines[0]; ? ? ? ? ? ? ? ? ? ? string str1 = lines[1]; ? ? ? ? ? ? ? ? ? ? textBox1.Text = str0; ? ? ? ? ? ? ? ? ? ? textBox2.Text = str1; ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? n++; ? ? ? ? ? ? ? ? button3.Text = "確認修改"; ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if (writefile(textBox1.Text, textBox2.Text)) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? textBox1.Visible = false; ? ? ? ? ? ? ? ? ? ? textBox2.Visible = false; ? ? ? ? ? ? ? ? ? ? n++; ? ? ? ? ? ? ? ? ? ? button3.Text = "修改默認"; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ?private static bool writefile(string name, string password) ? ? ? ? { ? ? ? ? ? ? string fileName = Environment.CurrentDirectory + "\\myText" + ".txt"; ? ? ? ? ? ? if (System.IO.File.Exists(fileName)) ? ? ? ? ? ? { ? ? ? ? ? ? ? ?File.Delete(fileName); ? ? ? ? ? ? } ? ? ?? ? ? ? ? ? ? ? ? StreamWriter sw = File.AppendText(fileName); ? ? ? ? ? ? ? ?sw.WriteLine(name); ? ? ? ? ? ? ? ?sw.WriteLine(password); ? ? ? ? ? ? ? ?sw.Flush(); ? ? ? ? ? ? ? ?sw.Close(); ? ? ? ? ?? ? ? ? ? ? ? return true; ? ? ? ? ? ? ? ? ? ? ? ? ? }
到此這篇關(guān)于c#中winform根據(jù)郵箱地址和密碼一鍵發(fā)送email的實現(xiàn)的文章就介紹到這了,更多相關(guān)c# winform郵箱地址和密碼發(fā)送email內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
WinForm開發(fā)中屏蔽WebBrowser腳本錯誤提示的方法
這篇文章主要介紹了WinForm開發(fā)中屏蔽WebBrowser腳本錯誤提示的方法,在C#項目開發(fā)中比較實用,需要的朋友可以參考下2014-08-08C#?CefSharp?根據(jù)輸入日期段自動選擇日期的操作代碼
這篇文章主要介紹了C#?CefSharp?根據(jù)輸入日期段自動選擇日期的操作代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01Unity3D利用DoTween實現(xiàn)卡牌翻轉(zhuǎn)效果
這篇文章主要為大家詳細介紹了Unity3D利用DoTween實現(xiàn)卡牌翻轉(zhuǎn)效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-02-02Unity輸出帶點擊跳轉(zhuǎn)功能的Log實現(xiàn)技巧詳解
這篇文章主要為大家介紹了Unity輸出帶點擊跳轉(zhuǎn)功能的Log實現(xiàn)技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11C#設計模式編程中運用適配器模式結(jié)構(gòu)實戰(zhàn)演練
這篇文章主要介紹了C#設計模式編程中運用適配器模式結(jié)構(gòu)實戰(zhàn)演練,并總結(jié)了適配器模式的優(yōu)缺點和適用場景以及.NET框架中的應用,需要的朋友可以參考下2016-02-02