應用C#發送帶附件的電子郵件的辦法的代碼示例剖析。本站提示廣大學習愛好者:(應用C#發送帶附件的電子郵件的辦法的代碼示例剖析)文章只能為提供參考,不一定能成為您想要的結果。以下是應用C#發送帶附件的電子郵件的辦法的代碼示例剖析正文
以下代碼除完成發送簡略的郵件之外,還包含了發送附件。From圖沒有貼出,下面就兩按鈕,一個“添加附件”、一個“發送”。點擊添加附件選擇文件, 文件途徑全存儲在listbox1中。在發送按鈕辦法中,把listbox1一切的文件添加到mailmessage對象裡作為郵件發送出去了,請看代碼
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net.Mail; using System.Net; using System.Net.Security; using System.IO; using System.Net.Mime; namespace SmtpTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { //界說一個mail對象 MailMessage mailmessage = new MailMessage(““, ““, “this is a test”, “yes!test!”); //from email,to email,主題,郵件內容 mailmessage.Priority = MailPriority.Normal; //郵件優先級 SmtpClient smtpClient = new SmtpClient(“smtp.163.com”, 25); //smtp地址和端標語 smtpClient.Credentials = new NetworkCredential(“ttlsa.com”, “xxxxxx”);//smtp用戶名暗碼 smtpClient.EnableSsl = true; //啟用ssl //添加附件 Attachment attachment =null; if(listBox1.Items.Count>0) { for (int i = 0; i < listBox1.Items.Count; i++) { string pathFileName = listBox1.Items[i].ToString(); string extName = Path.GetExtension(pathFileName).ToLower(); //獲得擴大名 if(extName==”.rar”||extName==”.zip”) //.rar和.zip的文件屬於緊縮文件類型 { attachment = new Attachment(pathFileName,MediaTypeNames.Application.Zip); }else { attachment = new Attachment(pathFileName,MediaTypeNames.Application.Octet); } //設置附件的MIME信息 ContentDisposition cd = attachment.ContentDisposition; cd.CreationDate = File.GetCreationTime(pathFileName);//設置附件的創立時光 cd.ModificationDate = File.GetLastWriteTime(pathFileName);//設置附件的修正時光 cd.ReadDate = File.GetLastAccessTime(pathFileName);//設置附件的拜訪時光 mailmessage.Attachments.Add(attachment);//將附件添加到mailmessage對象 } } smtpClient.Send(mailmessage); MessageBox.Show(“發送勝利”); } catch (SmtpException se) { MessageBox.Show(se.StatusCode.ToString()); } } //添加附件,把文件添加到listbox中 private void button2_Click(object sender, EventArgs e) { OpenFileDialog opd = new OpenFileDialog();//界說一個選擇文件的對話框 opd.Multiselect = true;//許可選擇多個文件 opd.CheckFileExists = true;//檢討文件能否存在 opd.ValidateNames = true;//檢討文件名的可用性 opd.ShowDialog();//翻開對話框 if(opd.FileNames.Length>0)//將選擇的文件途徑寫入listbox中 { listBox1.Items.AddRange(opd.FileNames); } } } }
PS:應用網易163smtp郵箱發送郵件
protected void Button2_Click(object sender, EventArgs e) { System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(); client.Host = "smtp.163.com";//應用163的SMTP辦事器發送郵件 client.UseDefaultCredentials = true; client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; client.Credentials = new System.Net.NetworkCredential("用戶名", "暗碼");//163的SMTP辦事器須要用163郵箱的用戶名和暗碼作認證,假如沒有須要去163請求個, //這裡假定你曾經具有了一個163郵箱的賬戶,用戶名為abc,暗碼為******* System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage(); Message.From = new System.Net.Mail.MailAddress("上述用戶名暗碼所對應的郵箱地址");//這裡須要留意,163仿佛有劃定發信人的郵箱地址必需是163的,並且發信人的郵箱用戶名必需和下面SMTP辦事器認證時的用戶名雷同 //由於下面用的用戶名abc作SMTP辦事器認證,所以這裡發信人的郵箱地址也應當寫為[email protected] Message.To.Add("目的郵箱地址");//將郵件發送給Gmail //Message.To.Add("[email protected]");//將郵件發送給QQ郵箱 Message.Subject = "customer feedback"; Message.Body = "customer feedback content"; Message.SubjectEncoding = System.Text.Encoding.UTF8; Message.BodyEncoding = System.Text.Encoding.UTF8; Message.Priority = System.Net.Mail.MailPriority.High; Message.IsBodyHtml = true; client.Send(Message); }