首页 新闻 会员 周边 捐助

.net 邮件发布到网上发送失败

0
悬赏园豆:100 [已解决问题] 解决于 2011-04-16 18:37

.net邮件发送在本地测试都可以正常发邮件,,一发布的服务器上后就出现发送失败(测试的邮箱和我到本地的测试通过的邮箱一样);分别用过126,163,sina,qq邮箱,在本地测试通过,发布到网上后无法发送成功,捕到异常:

System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 123.125.50.133:25

这里的红色ip是邮箱服务器的地址,后面的25为端口号;

看我代码

/// <summary>
/// 发送邮件
/// </summary>
/// <param name="fromEmail">发件人</param>
/// <param name="fromPsaaWord">发件人密码</param>
/// <param name="toEmail">收件人</param>
/// <param name="subject">主题</param>
/// <param name="body">内容</param>
/// <param name="attachmentsPath">附件(如果没有写为null)</param>
/// <returns></returns>
public bool SendReady(string fromEmail, string fromPsaaWord, string toEmail, string subject, string body, string[] attachmentsPath)
{
bool Success = true;
//验证合法邮箱的表达式,正确时返回true
if (!Regex.IsMatch(fromEmail, @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"))
{
//邮箱不合法
//MessageBox("邮箱不合法");
Success = false;
}
if (!Regex.IsMatch(toEmail, @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"))
{
Success
= false;
}
if (subject.Trim() == "")
{
// MessageBox("请填写主题");
Success = false;
}
if (body.Trim() == "")
{
// MessageBox("请填写内容");
Success = false;
}
else
{
MailAddress from
= new MailAddress(fromEmail);
//收件人地址
MailAddress to = new MailAddress(toEmail);
//邮件主题、内容
MailMessage message = new MailMessage(from, to);
//主题
message.Subject = subject;
message.SubjectEncoding
= System.Text.Encoding.UTF8;
//内容
message.Body = body;
message.BodyEncoding
= System.Text.Encoding.UTF8;
//在有附件的情况下添加附件
try
{
if (attachmentsPath != null && attachmentsPath.Length > 0)
{
Attachment attachFile
= null;
foreach (string path in attachmentsPath)
{
attachFile
= new Attachment(path);
message.Attachments.Add(attachFile);
}
}
}
catch (Exception err)
{
// MessageBox("在添加附件时有错误");
Success = false;
//throw new Exception("在添加附件时有错误:" + err);

}
try
{
//大部分邮件服务器均加smtp.前缀
SmtpClient client = new SmtpClient("smtp." + from.Host);

//不使用默认凭证,注意此句必须放在client.Credentials的上面
client.UseDefaultCredentials = false;
//指定用户名、密码
client.Credentials = new NetworkCredential(from.Address, fromPsaaWord);
//邮件通过网络发送到服务器
client.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
client.Send(message);


}
catch (Exception e)
{
Success
= false;
throw new Exception("系统繁忙,发送失败!");

}
finally
{
//及时释放占用的资源
message.Dispose();
}
}
catch (SmtpException err)
{
Success
= false;
////如果错误原因是没有找到服务器,则尝试不加smtp.前缀的服务器
//if (err.StatusCode == SmtpStatusCode.GeneralFailure)
//{
// try
// {
// //有些邮件服务器不加smtp.前缀
// SmtpClient client = new SmtpClient(from.Host);
// SendMail(client, from, fromPsaaWord, to, message)//发送;
// }
// catch (SmtpException)
// { }
//}
//else
//{ }
}
}
return Success;
}

到网上用过不同的代码,包括.net 1.1的代码!都是出现上面我说的情况,在本地测试成功,发到服务器上失败!我防火墙关了,没有安杀毒软件。。

问题补充: Server Error in '/' Application. -------------------------------------------------------------------------------- System.Exception: System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 123.125.50.133:25 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) --- End of inner exception stack trace --- at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback) at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message) --- End of inner exception stack trace --- at System.Net.Mail.SmtpClient.Send(MailMessage message) at Test03.sendMail(String receiveUser, String sendUser, String sendusername, String userpassword, String sendTitle, String sendContent) at Test03.sendMail(String receiveUser, String sendUse
冰封悲伤的主页 冰封悲伤 | 初学一级 | 园豆:44
提问于:2011-04-14 13:35
< >
分享
最佳答案
0

我记得好像要把系统的smtp服务安装并启动才行,web.config配置正确的smtp项

参考 http://www.4guysfromrolla.com/articles/072606-1.aspx 

收获园豆:50
2012 | 高人七级 |园豆:21645 | 2011-04-14 15:13
其他回答(2)
0

你发送附件了?

收获园豆:50
DYStudio.Net | 园豆:1747 (小虾三级) | 2011-04-14 13:48
没有啊!我调用的时都写的为空(null)
支持(0) 反对(0) 冰封悲伤 | 园豆:44 (初学一级) | 2011-04-14 13:54
qq 1023080982 代码发来 我给你瞅瞅
支持(0) 反对(0) DYStudio.Net | 园豆:1747 (小虾三级) | 2011-04-15 00:12
表面来看,是无法连接远程服务器,要么你的服务器有问题,要么你的配置有问题,要么就是端口问题,不知道你用的谁家的邮箱发送的.
支持(0) 反对(0) DYStudio.Net | 园豆:1747 (小虾三级) | 2011-04-15 00:17
0

现在解决了吗?我也遇到这样的问题了@!

NavyHaijun | 园豆:200 (初学一级) | 2017-09-18 10:56
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册