以下代码除了实现发送简单的邮件以外,还包括了发送附件。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服务器认证,所以这里发信人的邮箱地址也应该写为abc@163.com Message.To.Add("目标邮箱地址");//将邮件发送给Gmail //Message.To.Add("123456@qq.com");//将邮件发送给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); }
新闻热点
疑难解答