-2

I am trying to send an email using visual studio 2015 on a windows pc. i amusing a outlook email adress to send the emails please can somebody help me get the code rigth. i have tried many methods but they either timeout or say that they caanot send failure to send email. Please help

SmtpClient cv = new SmtpClient("smtp.live.com", 25);
cv.EnableSsl = true;
cv.Credentials = new NetworkCredential("xxxemail@mail.com", "password");
try
{
    cv.Send("xxxemail@mail.com", "xxxanotheremail@mail.com", "", "Hello");
    MessageBox.Show("Done");
}
catch(Exception w)
{
    MessageBox.Show("Not send" + w.InnerException);
}  
Liam
  • 27,717
  • 28
  • 128
  • 190
Richard Teunen
  • 79
  • 1
  • 1
  • 5

2 Answers2

0

you must figured out that setting the SmtpClient Credentials property before setting the UseDefaultCredentials = false causes the credentials to be ignored.

fails:

SmtpClient smtp = new SmtpClient;
smtp.Credentials = new NetworkCredential("richardteunen2@hotmail.com","pass");
smtp.UseDefaultCredentials = false;

Works:

SmtpClient smtp = new SmtpClient;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("richardteunen2@hotmail.com","pass");
Liam
  • 27,717
  • 28
  • 128
  • 190
ABlue
  • 664
  • 6
  • 20
-1
    public async Task<bool> SendAsync(EmailMessage message)
    {
        bool result = true;

        try
        {
            using (var email = new MailMessage("from@gmail.com", "to@gmail.com", message.Subject, message.Body))
            {
                var mailClient = new SmtpClient("smtp.gmail.com", 587) { Credentials = new NetworkCredential("from@gmail.com", "password"), EnableSsl = true };

                await mailClient.SendMailAsync(email);
            }
        }
        catch (Exception ex)
        {
            result = false;
        }

        return result;
    }
}
Hotmail settings
Server          Port
smtp.live.com   25, 587
Igor
  • 9
  • 3