【发送邮件】通过阿里云服务器发送邮件
写在最前面:
由于绝大部分云服务器提供商把25端口屏蔽了,所以想要通过25端口在服务器发送邮件基本不成功
一、通过阿里云邮件api发送
api发送之前写过一篇介绍:https://www.cnblogs.com/yhnet/p/14439984.html
二、通过SMTP方式发送
1、80端口发送(带附件发送)
#region 通过SMTP发送带附件的邮件(阿里提供的80端口方法可发送) /// <summary> /// 通过SMTP发送带附件的邮件(阿里提供的80端口方法可发送) /// </summary> /// <param name="to_mail">收件地址</param> /// <param name="from_mail">发件地址</param> /// <param name="subject">标题</param> /// <param name="name">昵称(可以传入姓名)</param> /// <param name="body">内容</param> /// <param name="smtp_server">smtp服务器地址</param> /// <param name="smtp_password">smtp的密码</param> /// <param name="file_address">文件的绝对路径</param> /// <returns></returns> public static bool SendEmailWithFile(string to_mail, string name, string from_mail, string subject, string body, string smtp_server, string smtp_password, string file_address) { try { MailMessage mailMsg = new MailMessage(); mailMsg.To.Add(new MailAddress(to_mail)); mailMsg.From = new MailAddress(from_mail, name); //可选,设置回信地址 //mailMsg.ReplyTo.Add("***"); // 邮件主题 mailMsg.Subject = subject; // 邮件正文内容 //string text = body; // "欢迎使用阿里云邮件推送"; //mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain)); string html = body; mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html)); // 添加附件 string file = file_address; // "D:\\1.txt"; Attachment data = new Attachment(file, MediaTypeNames.Application.Octet); mailMsg.Attachments.Add(data); //邮件推送的SMTP地址和端口 //SmtpClient smtpClient = new SmtpClient("smtpdm.aliyun.com", 25); //C#官方文档介绍说明不支持隐式TLS方式,即465端口,需要使用25或者80端口(ECS不支持25端口),另外需增加一行 smtpClient.EnableSsl = true; 故使用SMTP加密方式需要修改如下: SmtpClient smtpClient = new SmtpClient(smtp_server, 80); smtpClient.EnableSsl = true; // 使用SMTP用户名和密码进行验证 System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(from_mail, smtp_password); smtpClient.Credentials = credentials; smtpClient.Send(mailMsg); return true; } catch (Exception ex) { Console.WriteLine(ex.ToString()); return false; } } #endregion
2、465端口发送(带附件发送)
#region 通过Web.Mail.MailMessage SMTP发送带附件的邮件V2 465端口 /// <summary> /// 通过SMTP发送带附件的邮件2 465端口 /// </summary> /// <param name="toMail">收件人地址</param> /// <param name="fromMail">发件人地址</param> /// <param name="subject">标题</param> /// <param name="body">内容</param> /// <param name="smtp_server">smtp服务器地址</param> /// <param name="smtp_password">smtp授权密码</param> /// <param name="file_address">文件绝对路径</param> /// <returns></returns> public static bool SendEmailWithFile_V2(string toMail, string fromMail, string subject, string body, string smtp_server, string smtp_password, string file_address) { System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage(); try { mail.To = toMail; mail.From = fromMail; mail.Subject = subject; mail.BodyFormat = System.Web.Mail.MailFormat.Html; mail.Body = body; mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //身份验证 mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", mail.From); //邮箱登录账号,这里跟前面的发送账号一样就行 mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", smtp_password); //这个密码要注意:如果是一般账号,要用授权码;企业账号用登录密码 mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//端口 mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");//SSL加密 string file = file_address; // "D:\\1.txt"; System.Web.Mail.MailAttachment oAttch = new System.Web.Mail.MailAttachment(file, System.Web.Mail.MailEncoding.Base64); mail.Attachments.Add(oAttch); System.Web.Mail.SmtpMail.SmtpServer = smtp_server; //企业账号用smtp.exmail.qq.com System.Web.Mail.SmtpMail.Send(mail); return true; } catch (Exception ex) { //失败,错误信息:ex.Message; Console.WriteLine(ex.Message); return false; } } #endregion
PS:80端口发送是阿里云邮件发送帮助文档里介绍的方法,465端口是在网友总结的方法基础上增加了附件
参考自:https://www.bbsmax.com/A/kjdw0m92zN/