using System.Net.Mail; using System.Net; using System.Net.Mime; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Net.Mail; using System.Net; using System.Net.Mime;public partial class test_sendMail : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {} protected void Button1_Click(object sender, EventArgs e) { MailMessage mailMsg = new MailMessage(); mailMsg.From = new MailAddress(" 发送邮件用户名 mailMsg.To.Add(" 接收邮件用户 mailMsg.Subject = "邮件主题"; mailMsg.Body = "邮件主体内容ffffffffffffffffffff"; mailMsg.BodyEncoding = System.Text.Encoding.UTF8; mailMsg.IsBodyHtml = false; mailMsg.Priority = MailPriority.High;//附件的地址 string file = Request.PhysicalApplicationPath + "test\\data.xls"; // Create the file attachment for this e-mail message. Attachment data = new Attachment(file, MediaTypeNames.Application.Octet); // Add time stamp information for the file. ContentDisposition disposition = data.ContentDisposition; disposition.CreationDate = System.IO.File.GetCreationTime(file); disposition.ModificationDate = System.IO.File.GetLastWriteTime(file); disposition.ReadDate = System.IO.File.GetLastAccessTime(file); mailMsg.Attachments.Add(data);SmtpClient smtp = new SmtpClient(); // 提供身份验证的用户名和密码 // 网易邮件用户可能为:username password // Gmail 用户可能为:username@gmail.com password smtp.Credentials = new NetworkCredential("userName", "userpwd"); smtp.Port = 25; // Gmail 使用 465 和 587 端口 smtp.Host = "smtp.163.com"; // 如 smtp.163.com, smtp.gmail.com smtp.EnableSsl = false; // 如果使用GMail,则需要设置为true //smtp.SendCompleted += new SendCompletedEventHandler(SendMailCompleted); smtp.SendAsync(mailMsg, mailMsg); } }163测试成功,Gmail未测试。。。。
C#发送带附件的邮件
最新推荐文章于 2022-12-26 11:31:54 发布