Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The remote name could not be resolved when sending mail using SMTP with Host as IP address

MailMessage message = new MailMessage();
message.Subject = "test";
message.Body = "test";
message.To.Add("[email protected]");
message.From = new MailAddress("[email protected]");
SmtpClient smtp = new SmtpClient();
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Host = "172.22.0.20";
smtp.Port = 25;
smtp.Send(message);

Any idea why I might be getting an error

The remote name could not be resolved.

Clearly no resolution is required as I have specified an IP address. I can ping the IP and even telnet on port 25 and successfully send an e-mail. But, I can't send an e-mail.

I ran a wireshark trace and it doesn't look like any traffic is being send to 172.22.0.20

like image 684
Lee Tickett Avatar asked Sep 28 '16 08:09

Lee Tickett


1 Answers

I use this code and its working fine on my end Make sure that you are using the correct IP Address and port number. I think your Port Number is not correct. (open your email id and check mail configuration)

MailMessage mail = new MailMessage("[email protected]", "[email protected]");
mail.Subject = "Subject";
mail.IsBodyHtml = true;
mail.Body = "this is email body";

SmtpClient client = new SmtpClient("172.22.0.20");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
client.Port = 25;

client.Send(mail);
like image 115
Zulqarnain Jalil Avatar answered Nov 15 '22 17:11

Zulqarnain Jalil