An existing connection was forcibly closed by the

2019-07-16 03:09发布

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);

2条回答
放荡不羁爱自由
2楼-- · 2019-07-16 03:12

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.

查看更多
不美不萌又怎样
3楼-- · 2019-07-16 03:34

You need to pass login credentials:

mailClient.Credentials = new NetworkCredential(Email.Text, password)
查看更多
登录 后发表回答