11
MailMessage message = new MailMessage();
message.Subject = "test";
message.Body = "test";
message.To.Add("test@gmail.com");
message.From = new MailAddress("bob@internalhost.com");
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

Lee Tickett
  • 5,847
  • 8
  • 31
  • 55
  • add and check if it works with either true or false `smtp.EnableSsl = false` – Rahul Hendawe Sep 28 '16 at 09:48
  • also you need to add `NetworkCredential nc = new NetworkCredential(EmailFrom, tEmailFromPassword);` – Rahul Hendawe Sep 28 '16 at 09:55
  • @RahulHendawe our internal smtp server doesn't require authentication – Lee Tickett Sep 28 '16 at 10:40
  • @RahulHendawe adding `smtp.EnableSsl = false` didn't help either – Lee Tickett Sep 28 '16 at 10:41
  • Try set empty credentials: smtp.Credentials = new NetworkCredential(); – klashar Sep 30 '16 at 09:17
  • Could u indicate what OS and what language? – Danny Sep 30 '16 at 14:08
  • @dgoo2308 windows server 2008 r2 standard, c# – Lee Tickett Oct 02 '16 at 08:02
  • @klashar frustratingly this seems to have magically started working, but would you be able to explain why your suggest may help? and why I would receive this misleading error? – Lee Tickett Oct 02 '16 at 08:10
  • Assume there's no proxy involved as well locally to connect to the web? – Squiggs. Oct 02 '16 at 08:14
  • @Paul i don't believe so (i can telnet to that ip on port 25 without problem) – Lee Tickett Oct 02 '16 at 19:03
  • Do you have an exact error/stack trace? – Simon Mourier Oct 03 '16 at 04:42
  • 1
    @LeeTickett, You have quite unique setup with anonymous access to SMTP server. Even though you have specified `smtp.UseDefaultCredentials = false`. It would attempt setup the credentials to access the SMTP. With `smtp.Credentials = new NetworkCredential()` you explicitly say that request will go anonymously. I guess in your case [smtpconnection](https://referencesource.microsoft.com/#System/net/System/Net/mail/smtpconnection.cs,cbed1f9671320cdf,references) goes in via wrong path and you are getting misleading error message. – klashar Oct 03 '16 at 09:19
  • @SimonMourier unfortunately it magically started working so I can't provide a stack trace now. I think I will have to just re-raise if it happens again. – Lee Tickett Oct 03 '16 at 14:39
  • 4
    Maybe a red-herring but smpt host is a string and you're assigning it a number: `smtp.Host = 172.22.0.20;` - not sure how you got that to compile, if the problem happens again please include the complete code and Error Message/StackTrace. – Jeremy Thompson Oct 04 '16 at 03:09
  • 3
    If you are doing this at work, are you behind a network firewall? Could the firewall or antivirus be causing the issue? You might want to try talking to your system admin about this. You could also try running the code at home with your home computer. If it still does not work, then disable your antivirus software (temporarily) and try again. – Bob Bryan Oct 04 '16 at 05:54
  • @BobBryan sounds likely, but it's the fact that telnet works fine and also the misleading error that confuses me – Lee Tickett Oct 04 '16 at 07:36
  • @JeremyThompson good spot, but you're right, it's a red herring as my actual code pulls the IP from the app.config. I just replaced that with the IP for SO benefit. – Lee Tickett Oct 05 '16 at 07:36
  • Is there a system level proxy configured for .net possibly? What happens if you try a webrequest? – ameer Oct 06 '16 at 04:41
  • The other issue could be that your ISP is blocking traffic on port 25. Some ISP's require you to open that port on your account – timkly Oct 06 '16 at 23:19

8 Answers8

8

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("bob@internalhost.com", "Toemai@email.com");
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("bob@internalhost.com", "password");
client.Port = 25;

client.Send(mail);
Community
  • 1
  • 1
Zulqarnain Jalil
  • 1,679
  • 16
  • 26
3

I tested your code with :

static void Main(string[] args)
        {
            try
            {
                MailMessage message = new MailMessage();
                message.Subject = "test";
                message.Body = "test";
                message.To.Add("atmane.elbouachri@gmail.com");
                message.From = new MailAddress("atmane.elbouachri@softeam.fr");
                SmtpClient smtp = new SmtpClient();
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.UseDefaultCredentials = false;
                smtp.Host = "127.0.0.1";
                smtp.Port = 25;
                smtp.Send(message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                Console.ReadLine();
            }
        }

And it seems good. I used smtp4dev Tools and it truks this :

220 localhost smtp4dev ready
EHLO Aepro
250-Nice to meet you.
250-8BITMIME
250-STARTTLS
250-AUTH=CRAM-MD5 PLAIN LOGIN ANONYMOUS
250-AUTH CRAM-MD5 PLAIN LOGIN ANONYMOUS
250 SIZE
MAIL FROM:<atmane.elbouachri@softeam.fr>
250 Okey dokey
RCPT TO:<atmane.elbouachri@gmail.com>
250 Recipient accepted
RSET
250 Rset completed
RSET
250 Rset completed
MAIL FROM:<atmane.elbouachri@softeam.fr>
250 Okey dokey
RCPT TO:<atmane.elbouachri@gmail.com>
250 Recipient accepted
DATA
354 End message with period
MIME-Version: 1.0

But when i put a fake IP I have an exception (witch is normal).

I think your IP is bad or you must have a problem with a firewall

3

Below code is working fine at my end.....

MailMessage mailMessage = new MailMessage("abc@xyz.in", "receiver@xyz.in");
mailMessage.Priority = System.Net.Mail.MailPriority.High;
mailMessage.Body = "message";
mailMessage.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient("host/IP", 25);
NetworkCredential credentials = new NetworkCredential("abc@xyz.in", "password");
smtpClient.Credentials = credentials;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Credentials = credentials;
smtpClient.Send(mailMessage);
ImAtWar
  • 1,073
  • 3
  • 11
  • 23
Sukhvindra Singh
  • 200
  • 2
  • 14
2

If your code is running in a service application pool - you need to set the Identity under Advanced Settings in the Process Model as "Network Service" rather than the default "ApplicationPoolIdentity". If you do that, it'll work every time. If you don't, some emails work, some do not. It is a weird permission thing.

Robin Johnson
  • 357
  • 2
  • 11
  • I experienced this problem a few days ago using log4net smtp appender. Some emails worked. Some didn't. I was totally freaked out. But lo and behold, I found a similar problem on StackOverflow that provided the solution (as outlined above) and it worked perfectly. I share with you now. – Robin Johnson Oct 07 '16 at 02:19
1

A similar issue popped up over here: System.Net.WebException: The remote name could not be resolved:

As you can see, a simple polling mechanism wrapped around a try-catch statement can gives you an opportunity try your block of method multiple times (which fails sporadically) before you report it to the user.

Community
  • 1
  • 1
yantraman
  • 19
  • 5
0

The problem I see is this line:

smtp.Host = 172.22.0.20; 

smtp.Host expects string. So, it should be smtp.Host = "172.22.0.20"; Besides that, please add the code below so we can see error

try 
{
    smtp.Send(mail); 
}
catch (Exception e) 
{
    Debug.WriteLine("Exception Message: " + e.Message);
}
Balagurunathan Marimuthu
  • 2,927
  • 4
  • 31
  • 44
  • 1
    that's my mistake... the code is actually pulling the IP from the app.config. I replaced it with the IP when I copied/pasted to SO. unfortunately this is not the issue. the e.Message is `the remote name could not be resolved` – Lee Tickett Oct 05 '16 at 07:36
  • @LeeTickett, no problem! if you pulling it from web.config please paste the web.config for the host and port. It should be something like below and your delivery method should be smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis: – Ilgorbek Kuchkarov Oct 05 '16 at 14:10
0

Your IP address is not correct or you might be working under a firewall. Also check you DNS settings.

Ankit Zalani
  • 3,068
  • 5
  • 27
  • 47
0

Maybe the reverse name is mandatory, have you tried to register a name for this IP in the host file ? (Or in your DNS of course)

Try to add:

172.22.0.20 my-smtp.example
Leahkim
  • 343
  • 1
  • 2
  • 11