SMTP客户端中的远程主机强制关闭现有连接

时间:2010-04-04 21:35:05

标签: c# .net exception-handling smtp

我即将放弃调试SMTP服务器以发送电子邮件...我的代码如下

 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 个答案:

答案 0 :(得分:1)

您需要传递登录凭据:

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

答案 1 :(得分:1)

这是一个完整的工作示例:

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

请务必更换您的帐户和密码。