【发送邮件】通过阿⾥云服务器发送邮件
写在最前⾯:
由于绝⼤部分云服务器提供商把25端⼝屏蔽了,所以想要通过25端⼝在服务器发送邮件基本不成功
⼀、通过阿⾥云邮件api发送
⼆、通过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;
// 邮件正⽂内容param name
//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", 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
View Code
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("schemas.microsoft/cdo/configuration/smtpauthenticate", "1"); //⾝份验证
mail.Fields.Add("schemas.microsoft/cdo/configuration/sendusername", mail.From); //邮箱登录账号,这⾥跟前⾯的发送账号⼀样就⾏
mail.Fields.Add("schemas.microsoft/cdo/configuration/sendpassword", smtp_password); //这个密码要注意:如果是⼀般账号,要⽤授权码;企业账号⽤登录密码                mail.Fields.Add("schemas.microsoft/cdo/configuration/smtpserverport", 465);//端⼝
mail.Fields.Add("schemas.microsoft/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;    //企业账号⽤ail.qq
System.Web.Mail.SmtpMail.Send(mail);
return true;
}
catch (Exception ex)
{
//失败,错误信息:ex.Message;
Console.WriteLine(ex.Message);
return false;
}
}
#endregion
View Code
PS:80端⼝发送是阿⾥云邮件发送帮助⽂档⾥介绍的⽅法,465端⼝是在⽹友总结的⽅法基础上增加了附件

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。