无法从传输连接读取数据?
Unable to read data from the transport connection?
这是我在 Stack 上的第一个 post,我 字面上 刚刚开始编程,所以请耐心等待。
我正在尝试在按下按钮 1 时向 "x" 电子邮件地址发送电子邮件。我环顾四周,每个线程都是行话,而不是在我的上下文中。如果这是一个“新手”问题,或者如果它已经在其他地方得到彻底回答,请原谅我。
我得到的错误是“
邮件发送失败。 ---> System.IO.IOException: 无法从传输连接读取数据: net_io_connectionclosed."
这是我的代码
using (SmtpClient smtp = new SmtpClient())
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 465;
smtp.Credentials = new NetworkCredential("myemail", "mypassword");
string to = "toemail";
string from = "myemail";
MailMessage mail = new MailMessage(from, to);
mail.Subject = "test test 123";
mail.Body = "test test 123";
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateTestMessage2(): {0}",
ex.ToString());
}
非常感谢任何帮助!
您遇到的错误可能来自连接失败、服务器拒绝您的尝试、端口被阻止等多种原因。
我绝不是 SMTP 及其工作方面的专家,但是,您似乎缺少设置 SmtpClient.
的某些属性
我还发现使用端口 465 有点过时,当我 运行 使用端口 587 的代码时,它执行时没有问题。尝试将您的代码更改为与此类似的内容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Net;
namespace EmailTest
{
class Program
{
static void Main(string[] args)
{
SendMail();
}
public static void SendMail()
{
MailAddress ma_from = new MailAddress("senderEmail@email", "Name");
MailAddress ma_to = new MailAddress("targetEmail@email", "Name");
string s_password = "accountPassword";
string s_subject = "Test";
string s_body = "This is a Test";
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
//change the port to prt 587. This seems to be the standard for Google smtp transmissions.
Port = 587,
//enable SSL to be true, otherwise it will get kicked back by the Google server.
EnableSsl = true,
//The following properties need set as well
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(ma_from.Address, s_password)
};
using (MailMessage mail = new MailMessage(ma_from, ma_to)
{
Subject = s_subject,
Body = s_body
})
try
{
Console.WriteLine("Sending Mail");
smtp.Send(mail);
Console.WriteLine("Mail Sent");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateTestMessage2(): {0}",
ex.ToString());
Console.ReadLine();
}
}
}
}
经测试也能正常工作。另请注意:如果您的 Gmail 帐户不允许 "Less Secure" 应用程序访问它,您将收到一条错误消息,并会向您的收件箱发送一条消息,指出已发现未经授权的访问尝试。
要更改这些设置,请转到 here。
希望这对您有所帮助,让我知道结果如何。
这是我在 Stack 上的第一个 post,我 字面上 刚刚开始编程,所以请耐心等待。
我正在尝试在按下按钮 1 时向 "x" 电子邮件地址发送电子邮件。我环顾四周,每个线程都是行话,而不是在我的上下文中。如果这是一个“新手”问题,或者如果它已经在其他地方得到彻底回答,请原谅我。
我得到的错误是“ 邮件发送失败。 ---> System.IO.IOException: 无法从传输连接读取数据: net_io_connectionclosed."
这是我的代码
using (SmtpClient smtp = new SmtpClient())
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 465;
smtp.Credentials = new NetworkCredential("myemail", "mypassword");
string to = "toemail";
string from = "myemail";
MailMessage mail = new MailMessage(from, to);
mail.Subject = "test test 123";
mail.Body = "test test 123";
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateTestMessage2(): {0}",
ex.ToString());
}
非常感谢任何帮助!
您遇到的错误可能来自连接失败、服务器拒绝您的尝试、端口被阻止等多种原因。
我绝不是 SMTP 及其工作方面的专家,但是,您似乎缺少设置 SmtpClient.
我还发现使用端口 465 有点过时,当我 运行 使用端口 587 的代码时,它执行时没有问题。尝试将您的代码更改为与此类似的内容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Net;
namespace EmailTest
{
class Program
{
static void Main(string[] args)
{
SendMail();
}
public static void SendMail()
{
MailAddress ma_from = new MailAddress("senderEmail@email", "Name");
MailAddress ma_to = new MailAddress("targetEmail@email", "Name");
string s_password = "accountPassword";
string s_subject = "Test";
string s_body = "This is a Test";
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
//change the port to prt 587. This seems to be the standard for Google smtp transmissions.
Port = 587,
//enable SSL to be true, otherwise it will get kicked back by the Google server.
EnableSsl = true,
//The following properties need set as well
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(ma_from.Address, s_password)
};
using (MailMessage mail = new MailMessage(ma_from, ma_to)
{
Subject = s_subject,
Body = s_body
})
try
{
Console.WriteLine("Sending Mail");
smtp.Send(mail);
Console.WriteLine("Mail Sent");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateTestMessage2(): {0}",
ex.ToString());
Console.ReadLine();
}
}
}
}
经测试也能正常工作。另请注意:如果您的 Gmail 帐户不允许 "Less Secure" 应用程序访问它,您将收到一条错误消息,并会向您的收件箱发送一条消息,指出已发现未经授权的访问尝试。
要更改这些设置,请转到 here。
希望这对您有所帮助,让我知道结果如何。