最近在测试用yahoo.cn(支持smtp/pop)发送邮件时总是出现超时,但是如果使用outlook发送,同样的smtp配置和用户信息,可以正常发送,不知道怎么回事。
被这个问题困扰了2个礼拜了,敢请各位高手出招相助.:)。
先谢了。下面是邮箱的smtp配置和用户信息
=======================================
smtp服务器:smtp.mail.yahoo.com
smtp端口:465
stmp是否要求加密连接(SSL):是pop服务器:pop.mail.yahoo.cn (和smtp服务器确实不同)
pop端口:995
pop是否要求加密连接(SSL):是用户信息
email地址:[email protected]
用户名:[email protected] (需要加上 yahoo.cn)
密码:9b1f3cb0af0=======================================下面是主要的发送代码:SmtpClient client;
MailMessage message = null;//initialize mail message
message = new MailMessage();
string emailAddFrom = "[email protected]";
message.From = new MailAddress(emailAddFrom);
message.To.Add(new MailAddress("[email protected]", "tester"));message.Subject = "test subject";
message.Body = "test";
message.IsBodyHtml = true;message.Headers.Add("MIME-Version", "1.0");
message.Headers.Add("X-Mailer", "Microsoft Office Outlook 11");
message.Headers.Add("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.3790.4133");client = new SmtpClient("smtp.mail.yahoo.com",465);
client.EnableSsl = true;NetworkCredential nc = new NetworkCredential("[email protected]", "9b1f3cb0af0");
//将认证实例赋予mailClient
client.Credentials = nc;client.Send(message);
被这个问题困扰了2个礼拜了,敢请各位高手出招相助.:)。
先谢了。下面是邮箱的smtp配置和用户信息
=======================================
smtp服务器:smtp.mail.yahoo.com
smtp端口:465
stmp是否要求加密连接(SSL):是pop服务器:pop.mail.yahoo.cn (和smtp服务器确实不同)
pop端口:995
pop是否要求加密连接(SSL):是用户信息
email地址:[email protected]
用户名:[email protected] (需要加上 yahoo.cn)
密码:9b1f3cb0af0=======================================下面是主要的发送代码:SmtpClient client;
MailMessage message = null;//initialize mail message
message = new MailMessage();
string emailAddFrom = "[email protected]";
message.From = new MailAddress(emailAddFrom);
message.To.Add(new MailAddress("[email protected]", "tester"));message.Subject = "test subject";
message.Body = "test";
message.IsBodyHtml = true;message.Headers.Add("MIME-Version", "1.0");
message.Headers.Add("X-Mailer", "Microsoft Office Outlook 11");
message.Headers.Add("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.3790.4133");client = new SmtpClient("smtp.mail.yahoo.com",465);
client.EnableSsl = true;NetworkCredential nc = new NetworkCredential("[email protected]", "9b1f3cb0af0");
//将认证实例赋予mailClient
client.Credentials = nc;client.Send(message);
解决方案 »
- 关于devexpress 的 gridControl的几个小问题
- Drawstring画出来的文本的边距问题
- 在窗体上如何给 动态生成的控件定位啊?
- 请问C Sharp如何将整数(int)转化成字符串(string)
- 最近突然想到一件事,.NET也用了一段時間了,但總是想不明白,大家都來看看,說說.
- 我用dotnet自带的打包工具打包,在其他机器上安装了dotnet框架,怎么也不能运行,怎么回事哦
- c#解析JSON的问题
- 关于dataset的使用
- 晕头转向
- C#中怎样调用其他软件?如NetMeeting?
- 求2.0 winform datagridview 第三方控件,请给个地址和说明。
- C#能够调用VC写的动态库函数吗??
改成false看看还有通过程序发送时,要求能够打开IE,即能够访问http
谢谢client.EnableSsl = true;
这句是有的。:)另外,能够代开ie能够访问http。
类似与上面的代码,使用别的邮箱都可以发送成功的。就是yahoo的邮箱不行。大家可以用outlook试试看,在用程序跑跑看就知道
谢谢
outlook的配置我倒是比较清楚配置方法,:)我现在的情况是outlook可以发送,但是程序里不行。。
---------
协议都一样...去学学SSL你就明白怎么回事了...
???呵呵,没看明白。
这个只是说明一下我的outlook里面的配置。
---------
一样的...SSL-SMTP...不支持...
你是说帐号被屏蔽?问题是,如果我使用outlook来发送,同样的smtp服务器和用户名密码是可以的。.........
yahoo的邮箱要求用户提交smtp、pop的申请才能使用yahoo的帮助页面里可以设置
{
SslStream sslStream = null;
try
{
if (null == Host || Host.Trim().Length == 0)
{
ErrorEventArgs e = new ErrorEventArgs("邮件服务器地址未设置!");
OnSmtpError(e);
}
else
{
OnBeginConnect(EventArgs.Empty);
tcpClient = new TcpClient(Host, Port);
RemoteCertificateValidationCallback callback = new RemoteCertificateValidationCallback(OnCertificateValidation); tcpClient.SendTimeout = this.SendTimeout;
tcpClient.ReceiveTimeout = this.RecieveTimeout;
tcpClient.ReceiveBufferSize = 1024;
tcpClient.SendBufferSize = 1024; LingerOption linger = new LingerOption(true, 10);
tcpClient.LingerState = linger;
sslStream = new SslStream(tcpClient.GetStream(), false, callback);
ReadFromStream(ref sslStream, ref returnMsg);
if (CheckError(returnMsg, ReplyConstants.SmtpReplyConstants.HELO_REPLY))
{
throw new Exception();
}
sslStream.AuthenticateAsClient(Host);
if (sslStream.IsAuthenticated)
{
OnConnect(EventArgs.Empty);
return sslStream;
}
}
}
catch
{
ErrorEventArgs e = new ErrorEventArgs("无法连接到邮件服务器!");
OnSmtpError(e);
}
return sslStream;
}
private static bool OnCertificateValidation(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
{
return true;
}
return false;
}
private bool AuthLogin(ref SslStream sslStream)
{
if (null != this.Username && this.Username.Length > 0 && null != this.Password && this.Password.Length > 0)
{
WriteToStream(ref sslStream, "AUTH LOGIN\r\n");
ReadFromStream(ref sslStream, ref returnMsg);
if (AuthImplemented(returnMsg))
{
OnBeginAuthenticate(EventArgs.Empty);
string encodedUsername = Convert.ToBase64String(Encoding.ASCII.GetBytes(this.Username.ToCharArray()));
WriteToStream(ref sslStream, encodedUsername + "\r\n"); ReadFromStream(ref sslStream, ref returnMsg);
if (!CheckError(returnMsg, ReplyConstants.SmtpReplyConstants.SERVER_CHALLENGE))
{
return false;
} string encodedPassword = Convert.ToBase64String(Encoding.ASCII.GetBytes(this.Password.ToCharArray()));
WriteToStream(ref sslStream, encodedPassword + "\r\n"); ReadFromStream(ref sslStream, ref returnMsg);
if (!CheckError(returnMsg, ReplyConstants.SmtpReplyConstants.AUTH_SUCCESSFUL))
{
return false;
}
OnAuthenticated(EventArgs.Empty);
}
}
return true;
}
{
try
{
byte[] by = Encoding.Default.GetBytes(str.ToCharArray());
str = Convert.ToBase64String(by);
}
catch (Exception)
{
// 忽略错误
}
return str;
}
private void smtpMail(string smtpServer, string port, string uid, string pwd, string receiver, string subject, string content)
{
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
//client.EnableSsl = true; client.Host = smtpServer;//"smtp.163.com"
client.Port = int.Parse(port);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(uid, pwd);//"用户名", 邮箱的密码
client.DeliveryMethod = SmtpDeliveryMethod.Network; System.Net.Mail.MailMessage message = new MailMessage(uid, receiver);//"邮箱地址", "收件人邮箱地址"+ smtpServer.Replace("smtp." , "@")
message.Subject = subject;
message.Body = content;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true; //添加附件
//Attachment data = new Attachment(@"附件地址如:e:\a.jpg", System.Net.Mime.MediaTypeNames.Application.Octet);
//message.Attachments.Add(data); try
{
client.Send(message);
ClientScript.RegisterStartupScript(this.GetType(), "a1", "alert('Email successfully send.');", true); }
catch (Exception ex)
{
ClientScript.RegisterStartupScript(this.GetType(), "a2", "alert(@'Send Email Failed. <br /><br /> " + ex.ToString() + "');", false);
}
} protected void Button1_Click(object sender, EventArgs e)
{
this.smtpMail(txt_smtpServer.Text, txt_port.Text, txt_uid.Text, txt_pwd.Text, txt_receiver.Text, txt_subject.Text, txt_content.Text);
} protected void Button2_Click(object sender, EventArgs e)
{
this.test2();
} private void test2()
{
MailSender ms = new MailSender();
ms.From = "***@yahoo.cn";
ms.To = "***@yahoo.cn";
ms.Subject = "Subject";
ms.Body = "body text";
ms.UserName = "***@yahoo.cn"; // 怎么能告诉你呢
ms.Password = "******"; // 怎么能告诉你呢
ms.Server = "smtp.mail.yahoo.com";
//ms.Port = 465; //ms.Attachments.Add(new MailSender.AttachmentInfo(@"D:\\test.txt")); Response.Write("mail sending... <br />");
try
{
ms.SendMail();
Response.Write("<br />mail sended.<br />");
}
catch (Exception e)
{
Response.Write("<br />"+e);
}
}
特别感谢Fibona的帮助,他找出了问题所在。其实代码都没问题,问出处在smtp服务器的端口和EnableSsl设置。
即在代码中,smtp的设置如下:
smtp服务器:smtp.mail.yahoo.com
smtp端口:25
stmp是否要求加密连接(SSL):否
与使用outlook的配置不一样,太奇怪了。
不过问题总算解决了,暂时不深究。
公司的邮件服务器是SSL的,端口号465 用Outlook能发,用SMTP就不行,提示“发送超时”
126,gmail的邮箱都可以发
郁闷了,不知道怎么解决了