/// <summary> /// 发送邮件 /// </summary> /// <param name="from">发送人邮件地址</param> /// <param name="to">接收人邮件地址</param> /// <param name="subject">邮件主题</param> /// <param name="isBodyHtml">是否是Html</param> /// <param name="body">邮件体</param> /// <param name="smtpHost">SMTP服务器地址,例如:smtp.163.com</param> /// <param name="userName">用户名</param> /// <param name="password">密码</param> /// <returns>是否成功</returns> public static bool Send(string from, string to, string subject, bool isBodyHtml, string body, string smtpHost, string userName, string password) { string[] ts = to.Split(','); bool isSuccess = true; foreach (string t in ts) { try { MailMessage mm = new MailMessage(); mm.From = new MailAddress(from); mm.To.Add(new MailAddress(t.Trim())); mm.Subject = subject; mm.IsBodyHtml = isBodyHtml; mm.Body = body; SmtpClient sc = new SmtpClient(); sc.Host = smtpHost; sc.UseDefaultCredentials = true;//winform中不受影响,asp.net中,false表示不发送身份严正信息 //smtpClient.EnableSsl = true;//如果服务器不支持ssl则报,服务器不支持安全连接 错误 sc.Credentials = new System.Net.NetworkCredential(userName, password); sc.DeliveryMethod = SmtpDeliveryMethod.Network; sc.Send(mm); } catch { isSuccess = false; } } return isSuccess; }
、、--------------------------------------------------------------第二种方法------------------------------------------------------------------
#region 邮件发送 /// <summary> /// 发送邮件支持HTML格式 先引用System.Net.Mail; /// </summary> /// <param name="MailFrom">发送人Email 如[email protected]</param> /// <param name="MailServer">邮件服务器 如mail.126.com</param> /// <param name="MailUserName">登陆用户名 如[email protected]</param> /// <param name="MailPassword">登陆密码 如123456</param> /// <param name="mailto">收件人邮箱</param> /// <param name="title">邮件标题</param> /// <param name="msg">邮件正文</param> /// <returns>成功 true; 失败 false</returns> public static bool SendSmtpEmail(string MailFrom, string MailServer, string MailUserName, string MailPassword, string mailto, string title, string msg) { var client = new System.Net.Mail.SmtpClient(); client.Host = MailServer; client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential(MailUserName, MailPassword); client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; System.Net.Mail.MailMessage message = new MailMessage(MailFrom, mailto); message.Subject = title; message.Body = msg; message.SubjectEncoding = System.Text.Encoding.UTF8; message.BodyEncoding = System.Text.Encoding.UTF8; message.IsBodyHtml = true; message.Priority = MailPriority.High; message.IsBodyHtml = true; try { client.Send(message); return true; } catch { return false; } } #endregion