An existing connection was forcibly closed by the remote host in SMTP client

萝らか妹 提交于 2019-12-11 00:17:12

问题


I am about to give up debugging SMTP servers to send email... My code is the following

 SmtpClient mailClient = new SmtpClient("plus.smtp.mail.yahoo.com", 465);
    mailClient.EnableSsl = true;
    MailMessage message = new MailMessage();
    message.To.Add("aditya15417@hotmail.com");
    message.Subject = "permias-tucson-contact-us";
    mailClient.Credentials = new NetworkCredential("myemail@yahoo.com", "mypassword");
    MailAddress fromAddress = new MailAddress(Email.Text, Name.Text);
    message.From = fromAddress;

    mailClient.Send(message);

回答1:


You need to pass login credentials:

mailClient.Credentials = new NetworkCredential(Email.Text, password)



回答2:


Here's a full working example:

public class Program
{
    static void Main(string[] args)
    {
        using (var client = new SmtpClient("smtp.mail.yahoo.com", 587))
        {
            client.Credentials = new NetworkCredential("youraccount@yahoo.com", "secret");
            var mail = new MailMessage();
            mail.From = new MailAddress("youraccount@yahoo.com");
            mail.To.Add("destaccount@gmail.com");
            mail.Subject = "Test mail";
            mail.Body = "test body";
            client.Send(mail);
        }
    }
}

Make sure you replace your account and password.



来源:https://stackoverflow.com/questions/2575978/an-existing-connection-was-forcibly-closed-by-the-remote-host-in-smtp-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!