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.
Related
I am trying to send mail from Web API using SMTP (GODaddy). It failed to send the mail and returning the exception as follows
Code :
public void SendMail()
{
try
{
MailMessage mail = new MailMessage("support#abc.com","toMail");
mail.Subject = "Subject";
mail.IsBodyHtml = true;
mail.Body = "this is email body";
SmtpClient client = new SmtpClient("relay-hosting.secureserver.net");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("support#abc.com", "****");
client.Port = 25;
client.Send(mail);
}
catch (SmtpFailedRecipientException Ex)
{
Ex.FailedRecipient.ToString();
}
}
Can anyone help me to fix this.
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);
Firewall: Turned OFF. I am getting a connection timed out error. The email and password are verified.
The exception is: System.Net.Mail.SmtpException : The operation has timed out.
My code
string filename = #"C:\emailsample.htm";
string mailbody = System.IO.File.ReadAllText(filename);
mailbody = mailbody.Replace("##NAME##", firstname.Text);
string to = emailid.Text;
string from = "xxx.abc45#gmail.com";
MailMessage message = new MailMessage(from, to);
message.Subject = "Auto Generated Mail";
message.Body = mailbody;
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com", 465);
System.Net.NetworkCredential basic = new System.Net.NetworkCredential(from, "Password");
client.EnableSsl = true;
client.Timeout = 20000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = basic;
try
{
client.Send(message);
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.ToString());
return;
}
Try modifying the Port to 587.
Also verify if the From email is correct.
SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("user#gmail.com","password");
MailMessage mm = new MailMessage("donotreply#domain.com", "sendtomyemail#domain.co.uk", "test", "test");
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
client.Send(mm);
This may be very trivial for you but i just couldn't figure out why am i getting this error message when i run my code. I looked some of the relative questions on this same website for eg Sending email through Gmail SMTP server with C#
but none of them was helpful. Anyone willing to help please?
using different assemblies are also acceptable. so if anyone got a working solution that would be appreciated.
Error Message = The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
here is my code
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.From = new MailAddress("bob#googlemail.com");
message.To.Add("bob#hotmail.com");
message.Subject = "Hello";
message.Body = "Hello Bob ";
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential("MyGoogleMailAccount",
"mygooglemailpassword");
smtpClient.Send(message.From.ToString(), message.To.ToString(),
message.Subject, message.Body);
I don't think there's anything wrong with your code other than the e-mail addresses. I used this code to successfully send an e-mail from gmail to my personal account (ran it in LINQPad, actually). Simply replace the 3 string values with valid values for your accounts and you should be good to go:
MailMessage message = new System.Net.Mail.MailMessage();
string fromEmail = "myaddr#gmail.com";
string fromPW = "mypw";
string toEmail = "recipient#receiver.com";
message.From = new MailAddress(fromEmail);
message.To.Add(toEmail);
message.Subject = "Hello";
message.Body = "Hello Bob ";
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
using(SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential(fromEmail, fromPW);
smtpClient.Send(message.From.ToString(), message.To.ToString(),
message.Subject, message.Body);
}
By this post.
MailMessage mail = new MailMessage("you#yourcompany.com", "user#hotmail.com");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.google.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);
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: