在我们使用 .net System.Net.Mail进行邮件发送的时候。如果邮件服务器没有启用身份验证。那么发送时通过如下代码可以发送。
/// <summary> /// 发送邮件 /// </summary> /// <param name="strSmtpServer">邮件服务器地址</param> /// <param name="UserName">发送人邮件用户名</param> /// <param name="Password">邮件密码</param> /// <param name="strFrom">发送方邮件地址</param> /// <param name="strto">接收方邮件地址</param> /// <param name="strSubject">主题</param> /// <param name="strBody">内容</param> /// <param name="fujian">附件</param> /// <returns>bool</returns> public static bool SendMail(string strSmtpServer, string UserName, string Password, string strFrom, string strto, string strSubject, string strBody,string fujian) { try { System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(strSmtpServer); client.UseDefaultCredentials = true; client.Credentials = new System.Net.NetworkCredential(UserName, Password); client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(strFrom, strto, strSubject, strBody); message.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312"); message.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312"); message.IsBodyHtml = true; message.Priority = System.Net.Mail.MailPriority.High; if (!string.IsNullOrEmpty(fujian)) { message.Attachments.Add(new System.Net.Mail.Attachment(fujian)); } client.Send(message); return true;
} catch (Exception) { return false; }
}
如果启用了身份验证的话 这个代码好像就发布出去了。请问。如果邮件系统启用了身份验证的话。应该怎么写才能发送邮件成功
1 回答
侃侃尔雅
TA贡献1801条经验 获得超16个赞
你前面不是已经指定过一次身份信息了吗?后面怎么又加一次,还把用户名写错了.
client.Credentials = new System.Net.NetworkCredential(strFrom, Password);
- 1 回答
- 0 关注
- 481 浏览
添加回答
举报
0/150
提交
取消