1.发送邮件本地可以,放到阿里云服务器上就不行了。
一般是服务器禁用了25端口。所以使用465端口发送邮箱。
25端口代码:
public static void SendEmail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody, bool isHtmlFormat, string[] files)
{
try
{
SmtpClient smtpClient = new SmtpClient(strSmtpServer);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential(strFrom, strFromPass);
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
MailMessage mailMessage = new MailMessage(strFrom, strto, strSubject, strBody);
mailMessage.BodyEncoding = Encoding.Default;
mailMessage.IsBodyHtml = isHtmlFormat;
if (files != null)
{
for (int i = 0; i < files.Length; i++)
{
if (File.Exists(files[i]))
{
mailMessage.Attachments.Add(new Attachment(files[i]));
}
}
}
smtpClient.Send(mailMessage);
}
catch (Exception ex)
{
throw new Exception("发送邮件失败。错误信息:" + ex.Message);
}
}
465端口代码:
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="mailHost">邮件服务器</param>
/// <param name="mailPort">端口</param>
/// <param name="mailName">发送邮件的用户名</param>
/// <param name="mailPassword">发送邮件的密码</param>
/// <param name="tomail">收件人邮箱地址</param>
/// <param name="file">附件地址</param>
/// <param name="subject">邮件主题</param>
/// <param name="body">内容</param>
public static void Sendmail(string mailHost, int mailPort, string mailName, string mailPassword, string tomail, List<string> file = null, string subject = "", string body = "")
{
try
{
System.Web.Mail.MailMessage mmsg = new System.Web.Mail.MailMessage();
//验证
mmsg.Subject = subject;// "zhuti1";//邮件主题
mmsg.BodyFormat = System.Web.Mail.MailFormat.Html;
mmsg.Body = body;// "wqerwerwerwer";//邮件正文
mmsg.BodyEncoding = Encoding.UTF8;//正文编码
mmsg.Priority = System.Web.Mail.MailPriority.High;//优先级
//添加邮件附件,可发送多个文件
if (file != null && file.Count() > 0)
{
foreach (var filename in file)
{
System.Web.Mail.MailAttachment mailAttach_1 = new System.Web.Mail.MailAttachment(filename);
mmsg.Attachments.Add(mailAttach_1);
}
}
mmsg.From = mailName;//发件者邮箱地址
mmsg.To = tomail;//收件人收箱地址
mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//登陆名
mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", mailName);
//登陆密码
mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", mailPassword);
mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//端口
mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
System.Web.Mail.SmtpMail.SmtpServer = mailHost;
System.Web.Mail.SmtpMail.Send(mmsg);
}
catch (Exception ex)
{
LogHelper.Info("Erro" + ex.ToString());
throw new Exception("邮箱异常!" + ex.Message);
}
}
2154



被折叠的 条评论
为什么被折叠?



