无法从传输连接读取数据?

Zac*_*man 4 c# email smtp

这是我在 Stack 上的第一篇文章,我真的刚刚开始编程,所以请耐心等待。

我正在尝试在按下按钮 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());
            }
Run Code Online (Sandbox Code Playgroud)

任何帮助是极大的赞赏!

小智 5

您遇到的错误可能来自连接失败、服务器拒绝您的尝试、端口被阻止等多种原因。

我绝不是 SMTP 及其工作方面的专家,但是,您似乎缺少设置 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();
                }

        }

    }
}
Run Code Online (Sandbox Code Playgroud)

经测试也能正常工作。另请注意:如果您的 Gmail 帐户不允许“安全性较低”的应用程序访问它,您将收到一条错误消息,并向您的收件箱发送消息,说明已捕获未经授权的访问尝试。

要更改这些设置,请转到此处

希望这会有所帮助,让我知道它是如何工作的。

  • 上帝保佑加里,你是救星! (2认同)
  • SmtpClient 不支持在 SSL 模式下连接到 SMTP 服务器(这是端口 465 所要求的)。但是,它*确实*支持以明文模式连接,然后在与服务器的初始握手(这是端口 587 所做的)后切换到 SSL。 (2认同)