发邮件的出现System.Net.Sockets.SocketException的问题

zlzx_980 2008-02-19 12:53:15
请大家帮帮忙,帮我看一下为什么运行下面的代码就报这个错误
您的主机中的软件放弃了一个已建立的连接。
异常详细信息: System.Net.Sockets.SocketException: 您的主机中的软件放弃了一个已建立的连接。


string s_to = "zlzx_982@163.com", pwd2 = "123456";
string s_from = "zlzx_980@163.com";
string pwd = "251743677";
string s_body = "尊敬的用户" + name + ",您的密码为:\n" + pwd2;
s_body = s_body.Replace("\r\n", "<br/>");
s_body = s_body.Replace("\n", "<br/>");
s_body = s_body.Replace(" ", " ");

int i = s_from.IndexOf("@");
string username = s_from.Substring(0, i);
MailAddress from = new MailAddress(s_from);
MailAddress to = new MailAddress(s_to);
MailMessage mailobj = new MailMessage(from, to);
mailobj.Subject = "会员密码找回";
mailobj.Body = s_body;
mailobj.IsBodyHtml = true;
mailobj.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
mailobj.Priority = MailPriority.High;
SmtpClient smtp = new SmtpClient("smtp.163.com");
// smtp.Host = "smtp.163.com";
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(username, pwd);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

//try
//{
smtp.Send(mailobj);

Response.Redirect("RecoverSuccessaspx");
//}
//catch
//{
// string strScript = "<script language=javascript defer>\n";
// strScript += "window.alert(" + "\"邮件回发密码失败,可能是您的邮箱地址不正确!" + "\");";
// strScript += "</script>";
// Response.Write(strScript);
//}
...全文
569 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zlzx_980 2008-02-20
  • 打赏
  • 举报
回复
如何解决?
awfer1 2008-02-19
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Net;
using System.Net.Mail;
using System.ComponentModel;
namespace Framework
{
public class GmailSmtpEmail
{
/// <summary>
/// 返回为值表示发送成功
/// </summary>
private bool mailSent = false;
/// <summary>
/// Gmail的用户名:如: gamil@gmail.com
/// </summary>
private string username = "";//gmail的用户账号
/// <summary>
/// Gmail的密码
/// </summary>
private string password = "";//gmail的用户账号的密码
/// <summary>
/// Gmail的发送邮件服务器
/// </summary>
private string Host = " smtp.gmail.com";
/// <summary>
/// 端口号
/// </summary>
private int Port = 587;
/// <summary>
/// 发送Email要使用的编码
/// </summary>
private Encoding encoding = Encoding.GetEncoding("utf-8");
/// <summary>
/// 设置正文内容是否是包含Html的格式
/// </summary>
private bool bodyhtml = true;

/// <summary>
/// 设置电子邮件的优先级
/// </summary>
private MailPriority Priority = MailPriority.High;
/// <summary>
/// 是否使用套接字层安全加密连接
/// </summary>
private bool Ssl = true;
/// <summary>
/// 发送Email要使用的编码
/// </summary>
private string errormessage = "";

public string ErrorMessage
{
get { return errormessage; }
}
public Encoding Encoding
{
get { return encoding; }
set { encoding = value; }
}
/// <summary>
/// 设置正文内容是否是包含Html的格式
/// </summary>
public bool BodyHtml
{
get { return bodyhtml; }
set { bodyhtml = value; }
}

/// <summary>
/// 设置电子邮件的优先级
/// </summary>
public MailPriority priority
{
get { return Priority; }
set { Priority = value; }
}


/// <summary>
/// 发送单个Email
/// </summary>
/// <param name="Subject">主题</param>
/// <param name="Body">内容</param>
/// <param name="displayname">显示名</param>
/// <param name="addressee">收件人的Email地址</param>
public bool SendMail(string Subject, string Body, string displayname, string addressee)
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.To.Add(addressee);
msg.From = new MailAddress(username, displayname, encoding);
msg.Subject = Subject;
msg.SubjectEncoding = encoding;
msg.Body = Body;
msg.BodyEncoding = encoding;
msg.IsBodyHtml = bodyhtml;
msg.Priority = Priority;
//Add the Creddentials
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential(username, password);
client.Port = Port;//or use 587
client.Host = Host;
client.EnableSsl = Ssl;
client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
object userState = msg;
try
{
//you can also call client.Send(msg)
client.Send(msg);
mailSent = true;
}
catch (System.Net.Mail.SmtpException ex)
{
mailSent = false;
this.errormessage = ex.Message;
}
return mailSent;
}
/// <summary>
/// 发送一个或者多个的Email
/// </summary>
/// <param name="Subject">主题</param>
/// <param name="Body">内容</param>
/// <param name="displayname">显示名</param>
/// <param name="emaillist">收件人Email的列表</param>
public bool SendMail(string Subject, string Body, string displayname, string[] emaillist)
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
foreach (string mail in emaillist)
{
msg.To.Add(mail);
}
msg.From = new MailAddress(username, displayname, encoding);
msg.Subject = Subject;
msg.SubjectEncoding = encoding;
msg.Body = Body;
msg.BodyEncoding = encoding;
msg.IsBodyHtml = bodyhtml;
msg.Priority = Priority;

//Add the Creddentials
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential(username, password);
client.Port = Port;//or use 587
client.Host = Host;
client.EnableSsl = Ssl;
client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
object userState = msg;
try
{
//you can also call client.Send(msg)
client.Send(msg);
mailSent = true;
}
catch (System.Net.Mail.SmtpException ex)
{
mailSent = false;
this.errormessage = ex.Message;
}
return mailSent;
}

void client_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
MailMessage mail = (MailMessage)e.UserState;
string subject = mail.Subject;
if (e.Cancelled)
{
mailSent = false;
}
if (e.Error != null)
{
mailSent = false;
}
mailSent = true;
}
}
}
zyf2001 2008-02-19
  • 打赏
  • 举报
回复
怀疑是认证的问题!
lextm 2008-02-19
  • 打赏
  • 举报
回复
现在SMTP的发信服务器都要求进行身份认证。你的情况似乎是没有进行身份验证一部,所以服务器拒绝了你的发信请求。
chuxue1342 2008-02-19
  • 打赏
  • 举报
回复
可能是邮箱服务器的问题,你换个邮箱试试!
zlzx_980 2008-02-19
  • 打赏
  • 举报
回复
也不知道那里出了错,报邮件发送失败的错误
zlzx_980 2008-02-19
  • 打赏
  • 举报
回复
还是出错啊返回False
SMTP 服务器要求安全连接或客户端未通过身份验证。 服务器响应为: 5.5.1 Authentication Required n20sm17031210pof.6

62,241

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧