标签:from 问题 路径 names 收件人 odi 大会 解决 message
using System; using System.IO; using System.Net; using System.Net.Mail; using System.Net.Mime; using System.Text; public class EmailUitls { /// <summary> /// 发送邮件 /// </summary> /// <param name="fileName">附件路径</param> /// <param name="name">发件人名字</param> /// <param name="eamil">接收邮箱</param> /// <param name="title">标题</param> /// <param name="content">内容</param> /// <returns>成功/失败</returns> public static string send(string fileName, string name, string eamil, string title, string content) { SmtpClient client = new SmtpClient("smtp.qq.com"); client.EnableSsl = true; client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential("3295929259@qq.com", "qmftxbybbqtschac"); MailAddress from = new MailAddress("3295929259@qq.com", name, Encoding.UTF8);//初始化发件人 MailAddress to = new MailAddress(eamil, "", Encoding.UTF8);//初始化收件人 //设置邮件内容 MailMessage message = new MailMessage(from, to); message.Body = content.Trim(); message.BodyEncoding = Encoding.UTF8; message.Subject = title.Trim(); message.SubjectEncoding = Encoding.UTF8; message.IsBodyHtml = false; if (!string.IsNullOrEmpty(fileName)) { //附件 Attachment myAttachment = new System.Net.Mail.Attachment(fileName, MediaTypeNames.Application.Octet); //MIME协议下的一个对象,用以设置附件的创建时间,修改时间以及读取时间 ContentDisposition disposition = myAttachment.ContentDisposition; disposition.CreationDate = File.GetCreationTime(fileName); disposition.ModificationDate = File.GetLastWriteTime(fileName); disposition.ReadDate = File.GetLastAccessTime(fileName); //用smtpclient对象里attachments属性,添加上面设置好的myattachment message.Attachments.Add(myAttachment); } try { client.Send(message); return "邮件发送成功!"; } catch (Exception ex) { return "邮件发送失败!" + ex.Message; } } }
有一个问题,附件太大会发送失败
解决方法:https://support.microsoft.com/zh-cn/kb/2468871 (未测试)
标签:from 问题 路径 names 收件人 odi 大会 解决 message
原文地址:http://www.cnblogs.com/margin-gu/p/7068993.html