欢迎光临
我们一直在努力

阿里云服务器邮件发送

using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Mail; using System.Text; using System.Web; using System.Xml; namespace MallServer.Utility { public class emailhelper { public static bool MailSend(emailpara para) { try { EmailParameterSet EPSModel = new EmailParameterSet(); string filepath = System.Web.HttpContext.Current.Server.MapPath("\\Files\\email\\email.xml"); XmlDocument xml = common.xmlHelper.getXML(filepath); string BccAddress = xml.SelectSingleNode("xml").SelectSingleNode("BccAddress").InnerText;//邮件抄送地址 string portvalue = xml.SelectSingleNode("xml").SelectSingleNode("port").InnerText; //发送邮件的端口 int port = 587; int.TryParse(portvalue, out port); string serverAddress= xml.SelectSingleNode("xml").SelectSingleNode("serverAddress").InnerText;//提示跳转的管理地址  EPSModel.ConsigneeAddress = xml.SelectSingleNode("xml").SelectSingleNode("ConsigneeAddress").InnerText; EPSModel.ConsigneeName = xml.SelectSingleNode("xml").SelectSingleNode("ConsigneeName").InnerText;//  EPSModel.ConsigneeHand = xml.SelectSingleNode("xml").SelectSingleNode("ConsigneeHand").InnerText;//发件人标题 EPSModel.ConsigneeTheme = xml.SelectSingleNode("xml").SelectSingleNode("ConsigneeTheme").InnerText;//收件人的主题 EPSModel.SendSetSmtp = xml.SelectSingleNode("xml").SelectSingleNode("SendSetSmtp").InnerText;//发件邮件服务器的Smtp设置 EPSModel.SendEmail = xml.SelectSingleNode("xml").SelectSingleNode("SendEmail").InnerText;//发件人的邮件 EPSModel.SendPwd = xml.SelectSingleNode("xml").SelectSingleNode("SendPwd").InnerText; EPSModel.SendContent = xml.SelectSingleNode("xml").SelectSingleNode("SendContent").InnerText; if (para.ConsigneeTheme != "") { EPSModel.ConsigneeTheme = para.ConsigneeTheme; } if (para.SendContent != "") { EPSModel.SendContent = para.SendContent+"\r\n查看详细请登陆 "+serverAddress; } //确定smtp服务器端的地址,实列化一个客户端smtp  System.Net.Mail.SmtpClient sendSmtpClient = new System.Net.Mail.SmtpClient(EPSModel.SendSetSmtp);//发件人的邮件服务器地址 //构造一个发件的人的地址 System.Net.Mail.MailAddress sendMailAddress = new MailAddress(EPSModel.SendEmail, EPSModel.ConsigneeHand, Encoding.UTF8);//发件人的邮件地址和收件人的标题、编码 //构造一个收件的人的地址 System.Net.Mail.MailAddress consigneeMailAddress = new MailAddress(EPSModel.ConsigneeAddress, EPSModel.ConsigneeName, Encoding.UTF8);//收件人的邮件地址和收件人的名称 和编码 //构造一个Email对象 System.Net.Mail.MailMessage mailMessage = new MailMessage(sendMailAddress, consigneeMailAddress);//发件地址和收件地址 if (BccAddress != "") { string[] addressArr = BccAddress.Split('|'); for (int i = 0; i < addressArr.Length; i++) { mailMessage.Bcc.Add(new MailAddress(addressArr[i]));//添加抄送  } } mailMessage.Subject = EPSModel.ConsigneeTheme;//邮件的主题 mailMessage.BodyEncoding = Encoding.UTF8;//编码 mailMessage.SubjectEncoding = Encoding.UTF8;//编码 mailMessage.Body = EPSModel.SendContent;//发件内容 mailMessage.IsBodyHtml = false;//获取或者设置指定邮件正文是否为html //设置邮件信息 (指定如何处理待发的电子邮件) sendSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定如何发邮件 是以网络来发 sendSmtpClient.EnableSsl = true;//服务器支持安全接连,安全则为true sendSmtpClient.Port = port; sendSmtpClient.UseDefaultCredentials = true;//是否随着请求一起发 //用户登录信息 NetworkCredential myCredential = new NetworkCredential(EPSModel.SendEmail, EPSModel.SendPwd); sendSmtpClient.Credentials = myCredential;//登录  sendSmtpClient.Send(mailMessage);//发邮件 return true; } catch (Exception ex) { //common.CommonMethod.WriteTxt("ex.message:"+ex.Message); //common.CommonMethod.WriteTxt("ex.Source:" + ex.Source); //common.CommonMethod.WriteTxt("ex.StackTrace:" + ex.StackTrace); return false; } } } }

赞(0)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:阿里云服务器邮件发送
文章链接:https://www.jmwz.net/3880.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址