string MailFrom = "******"; string MailPWD = "*****"; string MailFName = "******"; string MailTo = "****"; string MailCc = "****"; string MailSubject = "邮件激活"; string MailContent = "在返回给拟稿人的时候,只要你把拟稿人的部门id去掉就会问题再现了。G_pnodes"; string Mailattachment = "C:\Documents and Settings\Administrator\桌面\NET资料\TabPublc\TabPublc\OnLoad\demo要求.docx"; if (EmailClass.EmailClass.SendEmail(MailFrom, MailPWD, MailFName, MailTo, MailSubject, MailContent, Mailattachment, MailCc)) { }
/// <summary> /// 发送邮件 /// </summary> /// <param name="mailFrom">登陆用户名</param> /// <param name="mailPWD">登陆密码</param> /// <param name="mailFName">发送者用户名</param> /// <param name="mailTo">接收邮件的地址</param> /// <param name="mailSubject">邮箱标题</param> /// <param name="mailContent">邮箱内容</param> /// <param name="mailAttachment">附件路径,多附件路径中间必须用","隔开</param> /// <param name="mailCc">抄送的邮箱地址,多接收地址中间必须用","隔开</param> /// <returns>返回发送邮箱的结果</returns> public static bool SendEmail(string mailFrom, string mailPWD, string mailFName, string mailTo, string mailSubject, string mailContent, string mailAttachment = "", string mailCc = "") { // 设置发送方的邮件信息,例如使用网易的smtp //string mailFrom = "fjptwwf@163.com"; //登陆用户名 //string mailPWD = "**18";//授权密码 163邮箱必须用授权密码 #region SMTP服务器 string _smtp=mailFrom.Split('@')[1].Split('.')[0].ToUpper(); switch (_smtp) { case "QQ"://QQ邮箱 _smtp = "smtp.qq.com"; break; case "163"://网易163邮箱 _smtp = "smtp.163.com"; break; case "SINA"://新浪邮箱 _smtp = "smtp.sina.com.cn"; break; case "SOHU"://搜狐邮箱 _smtp = "smtp.sohu.com"; break; case "126"://126邮箱 _smtp = "smtp.126.com"; break; case "188"://188邮箱 _smtp = "smtp.188.com"; break; } #endregion // 邮件服务设置 SmtpClient smtpClient=new SmtpClient(); smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式 smtpClient.Host = _smtp; //指定SMTP服务器 smtpClient.Credentials = new System.Net.NetworkCredential(mailFrom, mailPWD);//用户名和密码 smtpClient.EnableSsl = true; // 发送邮件设置 MailMessage mailMessage = new MailMessage(); mailMessage.From = new MailAddress(mailFrom, mailFName); mailMessage.To.Add(mailTo);//收件人地址,多接收地址中间必须用","隔开 mailMessage.Subject = mailSubject;//主题 mailMessage.Body = mailContent;//内容 mailMessage.CC.Add(new MailAddress(mailCc)); //抄送邮箱地址,多接收地址中间必须用","隔开 mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//正文编码 //mailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312"); mailMessage.IsBodyHtml = true;//设置为HTML格式 mailMessage.Priority = MailPriority.Normal;//优先级 if (mailAttachment != "") mailMessage.Attachments.Add(new Attachment(mailAttachment)); try { smtpClient.Send(mailMessage); // 发送邮件 return true; } catch (SmtpException ex) { return false; } }