Error: System.IO.IOException:
connection: 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.Unable to read data from the transport
I am using G- Suite account id for sending mail.i posted my code.
MailMessage mail = new MailMessage();
mail.From = new MailAddress(_configuration["SenderMail"]);
mail.To.Add(ToEmailId);
mail.IsBodyHtml = true;
mail.Subject = subject;
mail.Body = "" + mailBody;
SmtpClient smtpServer = new SmtpClient(_configuration["SMTPServer"], int.Parse(_configuration["SmtpPortNumber"]));
//smtpServer.EnableSsl = true;
smtpServer.UseDefaultCredentials = false;
smtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpServer.Credentials = new System.Net.NetworkCredential(_configuration["SenderMail"], _configuration["SenderPassword"]);
smtpServer.EnableSsl = true;
smtpServer.Send(mail);
Related
I've tried with Gmail (tried activating less secure apps, 2 steps verification and apps password, zone time, etc..).
Also, I tried sending emails with outlook and I'm having the same error.
In the console, I get the followed error: POST 500
Notes:
I don't have access to the Server.
This is working on my Local.
The website uses Plesk.
The Gmail code was working before, but after changing the password is not working (we activated less security apps).
In Gmail code, I've tried different ports.
Code:
MailMessage message = new MailMessage();
message.From = new MailAddress(model.EmailAddress);
message.To.Add("email#gmail.com");
message.Subject = string.Format("Mensaje de {0} ({1}) - {2}", model.FirstName, model.EmailAddress, model.Subject);
message.Body = model.Message;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("email#gmail.com", "password");
client.Send(message);
/*
MailMessage mc = new MailMessage(System.Configuration.ConfigurationManager.AppSettings["Email"].ToString(), "email#hotmail.com");
mc.Subject = string.Format("Mensaje de {0} ({1}) - {2}", model.FirstName, model.EmailAddress, model.Subject);
mc.Body = model.Message;
mc.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient("smtp.office365.com", 587);
smtp.Timeout = 1000000;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
NetworkCredential nc = new NetworkCredential(System.Configuration.ConfigurationManager.AppSettings["Email"].ToString(), System.Configuration.ConfigurationManager.AppSettings["Password"].ToString());
smtp.UseDefaultCredentials = false;
smtp.Credentials = nc;
smtp.Send(mc);
*/
When I run this code, I get an error :
System.Net.Mail.SmtpException: Failure sending mail. --->
System.Net.WebException: Unable to connect to the remote server
---> System.Net.Sockets.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 65.55.163.152:587
Code :
static void Main(string[] args)
{
string smtpAddress = "smtp.live.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "xxxxx#hotmail.co.uk";
string password = "xxxxxxxxxxx";
string emailTo = "myname#businessemail.co.uk";
string subject = "Daily Email Check";
string body = "Email reached business exchange server from an external hotmail email account";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = false;
try
{
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
catch (Exception e)
{
Console.WriteLine("Error\n\n {0}", e);
Console.ReadKey();
}
}
}
Try this;
using System.Net.Mail;
...
MailMessage mail = new MailMessage("xxxxx#hotmail.co.uk", "myname#businessemail.co.uk");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.live.com";
mail.Subject = "Daily Email Check";
mail.Body = "Email reached business exchange server from an external hotmail email account";
client.Send(mail);
MailMessage message = new MailMessage();
message.From = new MailAddress("MyMailAddress");
message.To.Add("DestinationMailAddress");
message.CC.Add("CCMailAddress");
message.Subject = "This is Subject";
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Body = "This is a test e-mail message sent by an application. ";
SmtpClient client = new SmtpClient();
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = true;
client.EnableSsl = true;
client.Host = "smtp.gmail.com";
client.Send(message);
This my code. The error was
Failure sending email.
Inner Exception:
{"Unable to connect to the remote server"} {"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 74.125.68.109:587"} Errorcode:10060
Your code uses gmail as your SMTP:
client.Host = "smtp.gmail.com";
Yet you specify your SMTP login credentials to be your windows account:
client.UseDefaultCredentials = true;
If gmail is in fact your SMTP, you need to set this to false and then provide your gmail login credentials.
So your code should look like:
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("yourusername", "yourpassword");
It also looks like you might have a firewall issue as well
{"Unable to connect to the remote server"} {"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 74.125.68.109:587"} Errorcode:10060
Make sure that the application has access to the internet and the necessary ports are open to reach gmail. Also make sure that on your gmail account, less ssecure applications have been allowed, and that, if you are using 2-factor authentication, that you have created an application-specific password for the account and are using that to connect.
How to enable less secure apps: https://support.google.com/accounts/answer/6010255?hl=en
You might also need to change how your MailAddress.To is populated:
message.To.Add(new MailAddress("DestinationMailAddress"));
Since the functionality is wrapped you should use the inner exception message to get the detailed error message. The failure reason will be right there!
public static void Main()
{
try {
SendMail();
}
catch(Exception e) {
if (e.InnerException != null)
Console.WriteLine("Inner exception: {0}", e.InnerException);
}
}
Make these changes
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("yourusername", "yourpassword");
And this too
http://docs.helpscout.net/article/120-smtp-settings
Full working code
protected void SendMail()
{
MailMessage msg = new MailMessage();
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
try
{
msg.Subject = "Add Subject";
msg.Body = "Add Email Body Part";
msg.From = new MailAddress("Valid Email Address");
msg.To.Add("Valid Email Address");
msg.IsBodyHtml = true;
client.Host = "smtp.gmail.com";
System.Net.NetworkCredential basicauthenticationinfo = new System.Net.NetworkCredential("Valid Email Address", "Password");
client.Port = int.Parse("587");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = basicauthenticationinfo;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(msg);
}
catch (Exception ex)
{
log.Error(ex.Message);
}
}
Error:
System.Mail.SmtpException: System.IO.IOEXCEPTION
{"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."}
Code:
string mailServer = "outlook.domain.com";
SmtpClient client = new SmtpClient(mailServer, 587);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.UseDefaultCredentials = false;
var AuthenticationDetails = new NetworkCredential("user#domain.com", "password");
client.Credentials = AuthenticationDetails;
using (MailMessage message = new MailMessage("user#domain.com", recipient))
{
message.IsBodyHtml = true;
message.Body = htmlString;
message.Subject = "Test Email";
client.Send(message);
}
I believe you have the wrong server-adress. Take a look here:
http://windows.microsoft.com/en-us/windows/outlook/send-receive-from-app
Use the correct adress for your use.
string mailServer = "smtp-mail.outlook.com";
SmtpClient client = new SmtpClient(mailServer, 25); // or 587
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.UseDefaultCredentials = false;
var AuthenticationDetails = new NetworkCredential("user#domain.com", "password");
client.Credentials = AuthenticationDetails;
using (MailMessage message = new MailMessage("user#domain.com", recipient))
{
message.IsBodyHtml = true;
message.Body = htmlString;
message.Subject = "Test Email";
client.Send(message);
}
Tips from this question:
Cant emails through exchange: An existing connection was forcibly closed by the remote host
Solved the problem by providing the correct Host name and Port. Thank you all for helping me.
I am getting exception while sending mail through C# for
SmtpClient client = new SmtpClient()
as System.Security.Cryptography.CryptographicException: The handle is invalid.
MailMessage mail = new MailMessage(from,to);
mail.To.Add(to);
mail.From = new MailAddress(from, "", System.Text.Encoding.UTF8);
mail.Subject = "This is a test mail";
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = fr.message;
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
//Add the Creddentials- use your own email id and password
client.Credentials = new System.Net.NetworkCredential(from, Password);
client.Port = 587; // Gmail works on this port
client.Host = "smtp.gmail.com";
client.EnableSsl = true; //Gmail works on Server Secured Layer
client.Send(mail);
I am not getting why this happens?
Check your project settings and make sure you have checked NTLM Authentication: