0

Here is my code:

MailAddress to = new MailAddress("MAIL@hotmail.com");
MailAddress from = new MailAddress("MAIL@hotmail.com");
MailMessage message = new MailMessage(from, to);
message.Subject = "Message du siute as-quebec.net de: " + EmailToSend.Email;
message.Body = EmailToSend.Message;
SmtpClient client = new SmtpClient("smtp.live.com");
client.Credentials = new System.Net.NetworkCredential("MAIL@hotmail.com", "PASSWORD");
client.Send(message);

I have this error:

System.Net.Mail.SmtpException : 'Failure sending mail.'

ExtendedSocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or an established connection failed because the connected host has failed to respond.

Some ideas what I'm doing wrong?

ThomasArdal
  • 4,999
  • 4
  • 33
  • 73
mathdx
  • 45
  • 6

2 Answers2

2

When using SmtpClient, if one specifies the incorrect value for SMTP server name (ie: SmtpClient.Host), one receives the following error message:

System.Net.Mail.SmtpException: 'Failure sending mail.'

WebException: Unable to connect to the remote server

SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond


According to POP, IMAP, and SMTP settings:

SMTP Settings

enter image description here


Also, ensure that you call Dispose for anything that has a Dispose method. The easiest way to do this, is with a using statement (or using declaration).

Try the following:

private void SendEmailOutlook(string toAddress, string fromAddress, string subject, string messageText, bool isHtmlMessage, string username, string password)
{
    MailAddress from = new MailAddress(fromAddress);

    using (SmtpClient client = new SmtpClient("smtp.office365.com", 587) { DeliveryMethod = SmtpDeliveryMethod.Network, EnableSsl = true, UseDefaultCredentials = false })
    {
        client.Credentials = new System.Net.NetworkCredential(username, password);

        MailAddress to = new MailAddress(toAddress);

        using (MailMessage message = new MailMessage(from, to) { IsBodyHtml = isHtmlMessage })
        {
            message.Subject = subject;
            message.Body = messageText;

            client.Send(message);
        }
    }
}
Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24
0

Try something like this:

Message message = new Message();
message.From = "mail@hotmail.com";
message.To = "mail@hotmail.com";
message.Subject = $"New mail from {message.From}";
message.Body = "something";
using (var smtp = new SmtpClient())
{
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    var msg = new MailMessage
    {
        Body = message.Body,
        Subject = message.Subject,
        From = new MailAddress(message.From)
    };
    msg.To.Add(message.To);
    smtp.Host = "email.active24.com";
    smtp.Port = 587;
    smtp.EnableSsl = true;
    smtp.Credentials = new NetworkCredential("mail@hotmail.com", password);
    smtp.SendMailAsync(msg);
}

You just need Host and Port from your provider

ThomasArdal
  • 4,999
  • 4
  • 33
  • 73
Otysko
  • 3
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 04 '23 at 06:05