Không thể kiểm tra gửi email từ mã .NET trong Windows Vista Business."Không thể đọc dữ liệu từ kết nối truyền tải: net_io_connectionclosed." - Windows Vista Business và SMTP
Tôi viết mã mà tôi sẽ di chuyển đến một gói SSIS một khi nó đã được chứng minh của nó. Mã này là để gửi một thông báo lỗi qua email đến một danh sách người nhận.
Mã dưới đây, tuy nhiên tôi nhận được ngoại lệ khi tôi thực thi mã.
Tôi tạo ra một lớp đơn giản để thực hiện gửi thư ... thiết kế có thể được tốt hơn, tôi đang thử nghiệm chức năng trước khi thực hiện chức năng mạnh mẽ hơn, phương pháp vv
namespace LabDemos
{
class Program
{
static void Main(string[] args)
{
Mailer m = new Mailer();
m.test();
}
}
}
namespace LabDemos
{
class MyMailer
{
List<string> _to = new List<string>();
List<string> _cc = new List<string>();
List<string> _bcc = new List<string>();
String _msgFrom = "";
String _msgSubject = "";
String _msgBody = "";
public void test(){
//create the mail message
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("[email protected]");
//set the content
mail.Subject = "This is an email";
mail.Body = "this is a sample body";
mail.IsBodyHtml = false;
//send the message
SmtpClient smtp = new SmtpClient();
smtp.Host = "emailservername";
smtp.Port = 25;
smtp.UseDefaultCredentials = true;
smtp.Send(mail);
}
}
Exception nhắn
Inner Exception
{"Unable to read data from the transport connection: net_io_connectionclosed."}
Stack Trace
" at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine)\r\n at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)\r\n at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)\r\n at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)\r\n at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)\r\n at System.Net.Mail.SmtpClient.GetConnection()\r\n at System.Net.Mail.SmtpClient.Send(MailMessage message)"
Outer Exception
System.Net.Mail.SmtpException was unhandled
Message="Failure sending mail."
Source="System"
StackTrace:
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at LabDemos.Mailer.test() in C:\Users\username\Documents\Visual Studio 2008\Projects\LabDemos\LabDemos\Mailer.cs:line 40
at LabDemos.Program.Main(String[] args) in C:\Users\username\Documents\Visual Studio 2008\Projects\LabDemos\LabDemos\Program.cs:line 48
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
at System.Activator.CreateInstance(ActivationContext activationContext)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.IO.IOException
Message="Unable to read data from the transport connection: net_io_connectionclosed."
Source="System"
StackTrace:
at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
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)
InnerException:
Tôi sẽ không cung cấp câu trả lời cho điều này đơn giản chỉ vì tôi không biết đủ về SMTP để cảm thấy tốt về nó. Tuy nhiên, điều này chắc chắn âm thanh như một vấn đề môi trường (có thể là trong các thiết lập IIS). Bạn đang sử dụng 1) "localhost" cho smtp.Host 2) một máy chủ từ xa cho smtp.Host và/hoặc 3) một IP cụ thể chứ không phải là tên máy chủ? –
Tôi lưu trữ thư của tôi đang được gọi theo tên. Ví dụ "trao đổi". Tôi đã thử nghiệm DNS thông qua ping máy chủ. Điều này đã thành công. Tôi cũng đã thử cổng truy cập thông qua telnet đến cổng 25. Điều này cũng thành công. Tôi đã thử nghiệm mã trên một máy khác và đã có thể gửi tin nhắn. Dường như với tôi tôi cần phải cài đặt một số loại dịch vụ SMTP/giao thức trên máy. Máy mà mã không thành công là Windows Vista Business. Các thử nghiệm đã thành công trên Windows Server 2008 Standard. –
vui lòng tham khảo: http://stackoverflow.com/questions/13689265/net-3-5-smtpclient-failure-sending-email-works-only-after-restarting-everyt/14272912#14272912 –