string file = "邮件测试.txt";//放在Debug下的一个txt文件。
MailAddress from = new MailAddress("自己的邮箱@163.com");
MailAddress to = new MailAddress("别人的邮箱@qq.com");
MailMessage message = new MailMessage(from ,to);//实例化一个MailMessage类对象
message.Subject = "邮件测试";
message.Body = "邮件正文";
//为要发送的邮件创建附件信息
Attachment myAttachment = new Attachment(file, System.Net.Mime.MediaTypeNames.Application.Octet);
//为附件添加时间信息
System.Net.Mime.ContentDisposition disposition = myAttachment.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
//将创建的附件添加到邮件中
message.Attachments.Add(myAttachment);
SmtpClient client = new SmtpClient();//实例化SmtpClient邮件发送类对象
client.Host = "smtp.163.com"; //这例题我的邮箱是.163.com ,设置SMTP的地址,注意:是什么邮箱就应该用相对应的地址
client.Port = 25;// 对应事务端口号
client.Credentials = new System.Net.NetworkCredential("自己邮箱名字", "自己邮箱密码");//设置用于验证自己邮件信息
client.Send(message);
MessageBox.Show("发送OK");