日期:2014-05-17 浏览次数:21001 次
/// <summary> /// 发送电子邮件 /// </summary> /// <param name="MessageTo">收件人邮箱地址</param> /// <param name="MessageSubject">邮件主题</param> /// <param name="MessageBody">邮件内容</param> /// <returns></returns> public static bool Send(List<string> MessageTo, string MessageSubject, string MessageBody) { MailMessage message = new MailMessage(); message.From = new MailAddress(MailAccount); MessageTo.ForEach(m => message.To.Add(m)); message.Subject = MessageSubject; message.Body = MessageBody; message.IsBodyHtml = true; message.Priority = MailPriority.Normal; SmtpClient sc = new SmtpClient(); sc.Host = MailHost; sc.Port = MailPort; sc.Credentials = new System.Net.NetworkCredential(MailAccount, MailPassword); try { sc.Send(message); } catch (OtherException ex) { throw (ex); } return true; }
------解决方案--------------------
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("abc", "*******");//163的SMTP服务器需要用163邮箱的用户名和密码作认证,如果没有需要去163申请个, //这里假定你已经拥有了一个163邮箱的账户,用户名为abc,密码为******* System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage(); Message.From = new System.Net.Mail.MailAddress("abc@163.com");//这里需要注意,163似乎有规定发信人的邮箱地址必须是163的,而且发信人的邮箱用户名必须和上面SMTP服务器认证时的用户名相同 //因为上面用的用户名abc作SMTP服务器认证,所以这里发信人的邮箱地址也应该写为abc@163.com Message.To.Add("123456@gmail.com");//将邮件发送给Gmail Message.To.Add("123456@qq.com");//将邮件发送给QQ邮箱 Message.Subject = "测试标题"; Message.Body = "测试邮件内容"; 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);