2

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);
abatishchev
  • 98,240
  • 88
  • 296
  • 433
aherlambang
  • 14,290
  • 50
  • 150
  • 253
  • You cannot send emails without logging in to an SMTP server with a username and password. You should probably create a separate email account to send from. – SLaks Apr 04 '10 at 21:45
  • I did that...updated the code above and it still gives the same error – aherlambang Apr 04 '10 at 23:35
  • 1
    I am having the same issue, but intermittently. For example when looping through 3000 recipients and sending out 3000 emails, it will throw this error randomly on say 100 of the emails that go out, but the rest will go fine. It can happen just when sending one single email out by itself too. Emails are behaving very unreliably for me and I don't know what else I can do to debut it either. Any luck at your end?? – Aaron Feb 18 '11 at 09:42

2 Answers2

1

You need to pass login credentials:

mailClient.Credentials = new NetworkCredential(Email.Text, password)
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928