System.Net.Mail.SmtpClient一般发送如果启用了ssl则他会先从25端口去建立握手,一般邮件服务器都会禁用25端口,因此会发送报错,远程服务器无响应,连接超时
推荐用web方式发送邮件,这样成功率很高,需要引用C#的程序集组件System.Web
参考代码:
public static void SendEmail()
{
//发件人
const string sendAddress = "xxxxxgg@petrochina.com.cn";
//发件人密码
const string sendPassword = "123456";
//收件人
const string receiveAddress = "kkkmmmm@petrochina.com.cn";
System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
try
{
mail.From = sendAddress;
mail.To = receiveAddress;
mail.Subject = "whl测试发送邮件2-26-1";
mail.BodyFormat = System.Web.Mail.MailFormat.Html;
mail.Body = "whl发送的测试邮件" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", sendAddress); //set your username here 发件人邮箱
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", sendPassword); //set your password here 发件人邮箱密码
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//发送端口号
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");//是否启用ssl
//需要引用组件System.Web
System.Web.Mail.SmtpMail.SmtpServer = "msg.petrochina.com.cn";
System.Web.Mail.SmtpMail.Send(mail);
Console.WriteLine("发送邮件成功");
}
catch (Exception ex)
{
var exg = ex.InnerException ?? ex;
string msg = exg.ToString();
Console.WriteLine(msg);
}
}
转载:
https://blog.csdn.net/molu474305148/article/details/99713389