SmtpClient mySC = new SmtpClient();
mySC.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
mySC.Host = "smtp.126.com";//指定SMTP服务器
mySC.Credentials = new System.Net.NetworkCredential("ytywj2005", "123456");//用户名和密码
MailMessage myMM = new MailMessage("ytywj2005@126.com", "ytywj2005@126.com");
myMM.Subject = "这是.net测试邮件0508";//主题
myMM.Body = "fsdfsdfsdfsf";//内容
myMM.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//正文编码
myMM.IsBodyHtml = true;//设置为HTML格式
myMM.Priority = MailPriority.High;//优先级
try
{
mySC.Send(myMM);//发送邮件
}
catch (Exception error)
{
Response.Write(error);
}
错误
System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: 远程主机强迫关闭了一个现有的连接。. ---> System.Net.Sockets.SocketException: 远程主机强迫关闭了一个现有的连接。 at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine) 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 testmail._Default.Button2_Click(Object sender, EventArgs e)
------解决方案--------------------------------------------------------
你那个服务器空间(可能是另外的虚拟主机站点)经常发垃圾邮件。被126禁止了。呵呵