/// C#发送邮件函数 该方法调用的是微软的.NET内部组件 /// </summary> /// <param name="from">发送者邮箱</param> /// <param name="fromer">发送人</param> /// <param name="to">接受者邮箱</param> /// <param name="toer">收件人</param> /// <param name="Subject">主题</param> /// <param name="Body">内容</param> /// <param name="file">附件</param> /// <param name="SMTPHost">smtp服务器</param> /// <param name="SMTPuser">邮箱</param> /// <param name="SMTPpass">密码</param> /// <returns></returns> public bool Sendmail(string sfrom, string sfromer, string sto, string stoer, string sSubject, string sBody, string sfile, string sSMTPHost, string sSMTPuser, string sSMTPpass) { ////设置from和to地址 MailMessage oMail = new MailMessage(); try { MailAddress from = new MailAddress(sfrom, sfromer); MailAddress to = new MailAddress(sto, stoer); ////创建一个MailMessage对象 oMail = new MailMessage(from, to); //// 添加附件 if (sfile != "") { oMail.Attachments.Add(new System.Net.Mail.Attachment(sfile)); } ////邮件标题 oMail.Subject = sSubject.ToString(); ////邮件内容 oMail.Body = sBody; ////邮件格式 oMail.IsBodyHtml = true; ////邮件采用的编码 oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312"); ////设置邮件的优先级为高 oMail.Priority = MailPriority.High; ////发送邮件 SmtpClient client = new SmtpClient(); ////client.UseDefaultCredentials = false; //smtp服务器 client.Host = sSMTPHost; //身份验证 if (!sfrom.ToLower().Contains("hotmail") && !sfrom.ToLower().Contains("hexun")) { client.Credentials = new NetworkCredential(sSMTPuser, sSMTPpass); } client.DeliveryMethod = SmtpDeliveryMethod.Network; if (sfrom.ToLower().Contains("gmail")) { client.EnableSsl = true; client.Port = 587; } if (sfrom.ToLower().Contains("hexun")) { client.Credentials = new NetworkCredential(sSMTPuser, sSMTPpass); // client.Credentials = new NetworkCredential(sfrom, sSMTPpass); } if (sfrom.ToLower().Contains("yahoo")) { client.Port = 587; } if (sfrom.ToLower().Contains("hotmail")) { client.EnableSsl = true; client.Credentials = new NetworkCredential(sfrom, sSMTPpass); client.Port = 25; } client.Send(oMail); return true; } catch (Exception err) { return false; } finally { ////释放资源 oMail.Dispose(); } }