/// <summary> /// 发送邮件 /// </summary> /// <param name="MailServerName">服务器</param> /// <param name="PubMail">公共邮箱地址</param> /// <param name="MailUserName">用户名</param> /// <param name="MailPassword">密码</param> /// <param name="To">收件人</param> /// <param name="CC">抄送人</param> /// <param name="MailHistoryGUID">邮件记录guid,获取附件</param> /// <param name="MailBody">邮件正文</param> /// <param name="Subject">主题</param> /// <returns></returns> private int SendMessage_internal(string MailServerName, string PubMail, string MailUserName, string MailPassword, string To, string CC, string MailHistoryGUID, string MailBody, string Subject) { //简单邮件传输协议类 SmtpClient client = new SmtpClient(); //电子邮件信息类 MailMessage mailMessage = new MailMessage(); int isSucess = 0; try { client.Host = MailServerName;//设置邮件服务器 client.Port = 25;//smtp主机端口号,默认25 client.DeliveryMethod = SmtpDeliveryMethod.Network; client.Credentials = new System.Net.NetworkCredential(PubMail, MailPassword); mailMessage.Sender = new MailAddress(PubMail, MailUserName);//发件人信息 //设置发件人 mailMessage.From = new MailAddress(PubMail, MailUserName); //设置收件 DataTable dtTemp = GetMailToList(); int count = dtTemp.Rows.Count; if (count > 0) { for (int i = 0; i < count; i++) { mailMessage.To.Add(new MailAddress(dtTemp.Rows[i]["Email"].ToString(), dtTemp.Rows[i]["UserName"].ToString())); } } //设置抄送人 if (!string.IsNullOrEmpty(CC)) { dtTemp = GetCCUserList(); count = dtTemp.Rows.Count; if (count > 0) { for (int i = 0; i < count; i++) { mailMessage.CC.Add(new MailAddress(dtTemp.Rows[i]["Email"].ToString(), dtTemp.Rows[i]["UserName"].ToString())); } } } //添加附件 dtTemp = GetAttachDocuments(); count = dtTemp.Rows.Count; if (count > 0) { for (int i = 0; i < count; i++) { mailMessage.Attachments.Add(new Attachment(HttpContext.Current.Server.MapPath("/") + dtTemp.Rows[i]["location"] + dtTemp.Rows[i]["FileName"].ToString())); } } //主题 mailMessage.Subject = Subject; mailMessage.Body = MailBody; mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;//邮件主题编码 mailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件内容编码 mailMessage.IsBodyHtml = true;//邮件内容是否为html格式 client.Send(mailMessage); isSucess = 1; } catch (Exception e) { MyDB.LogException(e); isSucess = 0; } finally { mailMessage.Dispose(); } return isSucess; } }
时间: 2024-11-10 03:05:18