Send email using Outlook.com SMTP
Asked Answered
H

2

6

I am trying to send an automated email using Outlook.com smtp support. However I am get the following exception:

System.Net.Mail.SmtpException: Failure sending mail.  
---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.  
---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host" Exception while sending email.

My code:

    public bool SendEmail(MailMessage msg)
    {
        try
        {
            SmtpClient smtpClient = new SmtpClient("smtp-mail.outlook.com")
            {
                UseDefaultCredentials = false,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                Credentials = new NetworkCredential("userAddress", "userPassword"),
                Port = 587,
                EnableSsl = true,
            };
            smtpClient.Send(msg);
            msg.Dispose();
            smtpClient.Dispose();
            return true;
        }
        catch (Exception exp)
        {
            Console.WriteLine(exp.ToString());
            return false;
        }
    }
Heeltap answered 18/9, 2013 at 2:28 Comment(5)
Have you tried using: smtp.live.com?Undressed
Incoming mail server: pop3.live.com Outgoing mail server (SMTP): smtp.live.comShena
Yes, I tried with smtp.live.com as well. But the exception is still being thrownHeeltap
Did you try "smtp.office365.com"?Michaels
My account is just regular Outlook.com free account. So I suspect if office365 paid smtp would work!Heeltap
N
34

I know that this is an extremely old question and I might not even be able to help, however I had a similar problem when I tried to send an email using C#.

As a result I used this which allowed me to send the emails:

string _sender = "";
string _password = "";

SmtpClient client = new SmtpClient("smtp-mail.outlook.com");

client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
System.Net.NetworkCredential credentials =
            new System.Net.NetworkCredential(_sender, _password);
client.EnableSsl = true;
client.Credentials = credentials;

MailMessage message = new MailMessage(_sender, "recipient of email");
message.Subject = "";
message.Body = "";
client.Send(message);

This probably will be of no use to you anymore, but in case anyone stumbles onto this question at least there is an answer which has working code acting as a fix!

Naughty answered 31/7, 2017 at 16:17 Comment(6)
I can confirm that this configuration just worked for me today.Unconsidered
@alex is "_sender" an email address? If not, can you give me an example of what I should put for this?Mazzard
@Mazzard Yes it is mateNaughty
Works still as of 2021-03-06Dripping
@Mazzard MailMessage(string from, string to), MailMessage(MailAddress from, MailAddress to), MailMessage(string from, string to, string subject, string body)Millican
doesn't work in 2024Spearhead
A
-1

I had this exact issue today on an old Windows 7 laptop. I also noticed that while the Internet was working fine through my browser, I could not ping ANYTHING from outside the router via the Command Prompt, therefore any attempts at SMTP were bound to fail. The problem was IPv6 was set to default on the network card. If you get this issue, check by pinging www.google.com: ping www.google.com If you get "Unreachable", force it to ping using IPv4 with ping -4 www.google.com. If you get a reply now, you can likely solve your problem by disabling IPv6 via your network card properties:

enter image description here

Just uncheck the box next to IPv6 and reboot your machine, go back to the Command Prompt and retry a straight "default" IPv ping: ping www.google.com. If it replies, your machine is now using IPv4 as default, so now re-try your SMTP e-mail code. I used the exact code in this article and it started working. Note that you can also apply the same logic for pinging with whatever your SMTP mail server address is eg. smtp.live.com which can help you test that you can communicate with the SMTP server regardless of your code.

Anthropomorphize answered 10/3, 2022 at 12:46 Comment(2)
this is not related to the questionScharaga
I disagree. I had this exact problem, and tried Alex Marchant's code, which didn't work at first. It could well be that anyone that reads this thread may have an IPv6 problem, which will brutally block any attempts at SMTP. Alex' code is great once this is tweaked on the PC.Anthropomorphize

© 2022 - 2024 — McMap. All rights reserved.