using System; using System.Net; using System.Net.Mail; using System.Net.Mime; namespace Fish { /**//// <summary> /// 发送邮件的类 /// </summary> public class SendMail { private MailMessage mailMessage; private SmtpClient smtpClient; private string password;//发件人密码 /**//// <summary> /// 处审核后类的实例 /// </summary> /// <param name="To">收件人地址</param> /// <param name="From">发件人地址</param> /// <param name="Body">邮件正文</param> /// <param name="Title">邮件的主题</param> /// <param name="Password">发件人密码</param> public SendMail(string To, string From, string Body, string Title, string Password) { mailMessage = new MailMessage(); mailMessage.To.Add(To); mailMessage.From = new System.Net.Mail.MailAddress(From); mailMessage.Subject = Title; mailMessage.Body = Body; mailMessage.IsBodyHtml = true; mailMessage.BodyEncoding = System.Text.Encoding.UTF8; mailMessage.Priority = System.Net.Mail.MailPriority.Normal; this.password = Password; } /**//// <summary> /// 添加附件 /// </summary> public void Attachments(string Path) { string[] path = Path.Split("",""); Attachment data; ContentDisposition disposition; for (int i = 0; i < path.Length; i++) { data = new Attachment(path[i], MediaTypeNames.Application.Octet);//实例化 附件 disposition = data.ContentDisposition; disposition.CreationDate = System.IO.File.GetCreationTime(path[i]);//获取 附件的创建日期 disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]);// 获取附件的修改日期 disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]);//获取附 件的读取日期 mailMessage.Attachments.Add(data);//添加到附件中 } } /**//// <summary> /// 异步发送邮件 /// </summary> /// <param name="CompletedMethod"></param> public void SendAsync(SendCompletedEventHandler CompletedMethod) { if (mailMessage != null) { smtpClient = new SmtpClient(); smtpClient.Credentials = new System.Net.NetworkCredential (mailMessage.From.Address, password);//设置发件人身份的票据 smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; smtpClient.Host = "smtp." + mailMessage.From.Host; smtpClient.SendCompleted += new SendCompletedEventHandler (CompletedMethod);//注册异步发送邮件完成时的事件 smtpClient.SendAsync(mailMessage, mailMessage.Body); } } /**//// <summary> /// 发送邮件 /// </summary> public void Send() { if (mailMessage != null) { smtpClient = new SmtpClient(); smtpClient.Credentials = new System.Net.NetworkCredential (mailMessage.From.Address, password);//设置发件人身份的票据 smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; smtpClient.Host = "smtp." + mailMessage.From.Host; smtpClient.Send(mailMessage); } } } } 这段代码,觉得用起来还行,我在服务器上死活不行,发送失败,提示不允许使用邮箱名,然后百度了一大圈,看了各种资料,尝试了各种办法就是不行,哪位大侠可以告诉我这是为什么! |
|
本地测试是OK的,在WIN7上面运行OK,在另一台XP上运行不行,框架是4.0的,在服务器2008上面运行也出错!
|
|
没人遇到过这样子的情况吗
|
|
40分 |
别折腾了,这个代码以前我也用过,换成QQ的就OK了,126的貌似不通用
|
QQ的是可以不过还是蛮纠结的,为啥win7可以,不过还是解决了,分就给你吧
|