Solved

Unable to send mail

Posted on 2013-12-26
37
799 Views
Last Modified: 2014-01-09
Hi,
With these codes
                MailMessage message = new MailMessage();
                message.From = new MailAddress("administrator@my-friend.co");
                message.To.Add("peterchen@gmail.com");
                message.Subject = "test message";
                message.Body = "This is a test e-mail message sent by an application. ";


                SmtpClient client = new SmtpClient("mail.my-friend.co", 587);
                client.UseDefaultCredentials = false;
                client.Credentials = new System.Net.NetworkCredential("administrator@my-friend.co", "?????");
                client.EnableSsl = true;
                client.Send(message);

Open in new window

I'm getting this problem

Server Error in '/App9' Application.

The remote name could not be resolved: 'mail.my-friend.co'
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

 Exception Details: System.Net.WebException: The remote name could not be resolved: 'mail.my-friend.co'

Source Error:


 An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace:



[WebException: The remote name could not be resolved: 'mail.my-friend.co']
   System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6) +8804886
   System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback) +8805322
   System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) +3215103
   System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) +286
   System.Net.Mail.SmtpClient.Send(MailMessage message) +1500

[SmtpException: Failure sending mail.]
   System.Net.Mail.SmtpClient.Send(MailMessage message) +2579791
   App9._Default.Page_Load(Object sender, EventArgs e) +437
   System.Web.UI.Control.LoadRecursive() +71
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3178

 


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18408  

while "mail.my-friend.co" is surely the one I've entered in my MX record, and my Exchange server is working fine.
0
Comment
Question by:HuaMinChen
  • 18
  • 18
37 Comments
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39741134
There server/computer where you are running your code is unable to find or access 'mail.my-friend.co'. So either it can't find it due to DNS issues, or it isn't allowed to contact it due to permissions.
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 39741164
Thanks. I now use this instead
                ...
                SmtpClient client = new SmtpClient("my-friend.co", 587);
                ...
               
but I am getting this exception and stacktrace. why?

The remote certificate is invalid according to the validation procedure.   at System.Net.Security.SslState.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, Exception exception)
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39741183
Because your server doesn't recognise the certificate authority I would imagine - you're probably using a self generated one?

I think everything you want to know is here http://stackoverflow.com/questions/777607/the-remote-certificate-is-invalid-according-to-the-validation-procedure-using

Note the difference between hiding the issue (easy) and fixing the issue (a little harder).
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 39741228
But I get these errors

Error      2      The type or namespace name 'X509Certificate' could not be found (are you missing a using directive or an assembly reference?)      C:\App9\App9\Default.aspx.cs      217      94      App9
Error      3      The type or namespace name 'X509Chain' could not be found (are you missing a using directive or an assembly reference?)      C:\App9\App9\Default.aspx.cs      217      123      App9
Error      4      The type or namespace name 'SslPolicyErrors' could not be found (are you missing a using directive or an assembly reference?)      C:\App9\App9\Default.aspx.cs      217      140      App9

due to this line

                ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
                ...

Open in new window

0
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 145 total points
ID: 39741308
Have you tried using google? You'll find the namespace/reference you need to add faster than asking me :)

http://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509certificate(v=vs.110).aspx
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 39741331
Sorry, I am still getting this exception and stacktrace
The remote certificate is invalid according to the validation procedure.   at System.Net.Security.SslState.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, Exception exception)

when running the newly re-deployed project, having these inside.

                ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
                try
                {
                    MailMessage message = new MailMessage();
                    message.From = new MailAddress("administrator@my-friend.co");
                    message.To.Add("peterchen@gmail.com");
                    message.Subject = "test message";
                    message.Body = "This is a test e-mail message sent by an application. ";


                    SmtpClient client = new SmtpClient("my-friend.co", 587);
                    client.UseDefaultCredentials = false;
                    client.Credentials = new System.Net.NetworkCredential("administrator@my-friend.co", "?????");
                    client.EnableSsl = true;
                    client.Send(message);
                    ...

Open in new window

0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39741363
Have you read through all the possible answers in the URL I posted? There appear to be lots of things you can try, but I don't know which will solve your problem.
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 39741370
I don't know how to resolve the exception (with the stacktrace), due to the codes posted above.
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39741403
Are you sure you have the correct port number?
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39741411
You probably should make sure you have installed the certificate correctly http://stackoverflow.com/questions/9983265/the-remote-certificate-is-invalid-according-to-the-validation-procedure
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 39741416
This is not a port number but a problem to send out mail. which certificate should be collected for the problem I've got?
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39741427
If you are using the wrong port number for the mail server you are trying to contact you will get that error. You are trying to use a secure (SSL) connection (client.EnableSsl = true;) so you need to verify the certificate of the mail server. You may need to do some research on this subject.
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 39743006
Do you or anyone have any ideas to resolve this problem encountered? Thanks
0
 
LVL 13

Expert Comment

by:Ashok
ID: 39743015
Could you try this?

client.Credentials = new System.Net.NetworkCredential("administrator", "?????");

I think you do not need to provide full email address.

SmtpClient client = new SmtpClient("smtp.mail.my-friend.co", 587);

By the way, port # 587 is CORRECT for SECURE e-mail.

HTH
Ashok
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 39743022
I have to put the full email address.

with these
                    SmtpClient client = new SmtpClient("smtp.mail.my-friend.co", 587);
                    client.UseDefaultCredentials = false;
                    client.Credentials = new System.Net.NetworkCredential("administrator", "?????");
                    client.EnableSsl = true;
                    client.Send(message);
                    ...

Open in new window

                   
I then have got this exception and stacktrace.

Mailbox unavailable. The server response was: 5.7.1 Unable to relay  at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39743177
Secure Ports are configurable by the mail server - but it does sound like that isn't the issue.

With every new error you get I wonder why you don't just google it? They are all well understood and the solutions are only a google away. Waiting for someone here to answer means you have a smaller resource pool, and a slower to respond one.

Anyway failure to relay means that your mail server isn't configured to allow you to send email via it. It therefore thinks you are a spammer and is denying you access.

You need to access the mail server, and configure it to allow relaying from your web server.

This link explains further http://stackoverflow.com/questions/3165721/mailbox-unavailable-the-server-response-was-5-7-1-unable-to-relay-for-abcxyz
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 39743183
Sorry, where to open SMTP virtual server properties within IIS 6 manager?
t622.png
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39743221
That server doesn't appear to have an SMTP server - are you sure you are looking at the correct one?

If there is one it should be displayed for editing http://www.smtp2go.com/docs/iis/

If it is the correct server you may have to install the SMTP server. If thats not the case then you may need to read some forums such as http://forums.iis.net/t/1164041.aspx?SMTP+Virtual+Server+Does+Not+Appear+In+IIS+6+Windows+Server+2008

Also are you definitely using IIS6 for your SMTP server?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 10

Author Comment

by:HuaMinChen
ID: 39743229
I'm having IIS 6 manager in the server on which I am to deploy the projects. Actually, my Exchange server is working fine, to receive/send out mails. I wonder if I really have to set up SMTP server in the machine.
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39743237
You have an SMTP server here smtp.mail.my-friend.co - which is the one you are trying to send mail via. You need to configure that SMTP server to allow your web server to relay through it. I don't know anything about the setup of smtp.mail.my-friend.co to be able to advise on how to configure it.
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 39743241
My Exchange server is working fine there. What to set up for SMTP? What to check within my Exchange server?
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39743243
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 39743244
But it is not for Exchange server 2010.
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39743247
I'm sure you can google it.
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 39747477
But I really am not sure, if setting up SMTP, is the solution to the problem I got.
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39747502
The error you showed us was "Mailbox unavailable. The server response was: 5.7.1 Unable to relay".

To solve this error requires you to configure your SMTP server correctly - there is no doubt about that - its a fact.

And if you type that error into google and look at the results you will be able to verify that for yourself. You can also google how to configure your mail server correctly.
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 39747518
I've just set up SMTP virtual server and have also done these
Open IIS6 Manager using Control Panel --> Administrative Tools.
Open SMTP Virtual Server properties.
On General tab, Set IP address of the Web server instead of "All Unassigned".
In Access tab, click on Relay button, this will open Relay Restrictions dialog.
In relay computers list, add the relevant IP of the server to the list.

but I still get the same exception and stacktrace. why?
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39749092
But are you actually using the SMTP server in IIS6? You said you were using exchange server?
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 39749354
I have Exchange server in the server. I tried to use the codes to send out mail from my VS 2013 project. If you are inexperienced in such kind of project, please leave this. I need only advice from experienced guys.
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39749377
I have answered all your questions to date, and lets not forget we are onto your third question now. Your original question was answered long ago. Your most recent question was what is causing the relay error, not how to configure exchange server. I suggest you award points on this question and ask a new question if you need help configuring your exchange server.
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 39749407
I am still stuck with the codes and cannot move further. what to award?
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39749420
Your first question was "The remote name could not be resolved: 'mail.my-friend.co'" which I answered and you resolved.

Your second question was "The remote certificate is invalid according to the validation procedure." which Ashok answered.

Your third question was "Mailbox unavailable. The server response was: 5.7.1 Unable to relay" which I have given you the answer for but you have been unable to configure your SMTP server correctly.

We have answered your all your questions, so should get the points. You should really ask a new question each time.

One further thing you might like to try is using a different SMTP server as a test. For example try using a gmail account or your local ISP SMTP server. If you are able to get it working using a different SMTP server you will at least believe me that that is the issue.

In fact many web-servers use localhost as the SMTP server - but it depends whose web server it is and how it is configured.

Otherwise I suggest posting a new question asking for assistance on how to configure Exchange to allow SMTP relay - but you can probably google it to be honest.

HTH as I don't have anymore suggestions.
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 39750674
The point is, the original question is not really resolved yet, after I've changed

SmtpClient client = new SmtpClient("mail.my-friend.co", 587);

to

SmtpClient client = new SmtpClient("my-friend.co", 587);

I cannot blindly mark the posts as answer if there're some other issues happening there. Since my Exchange server is working fine, I just wonder why I can't implement the current codes by calling the Exchange server set up in there.
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39750688
There is nothing blind about it, the original problem has been solved, this is a new problem.

So at the high risk of repeating myself, the error "unable to relay" means that Exchange server is deliberately blocking your attempt to send email because it is not configured to allow your sever to send email via it.

This is a classic spam scenario, spammers are always out there looking for SMTP servers which are not locked down, because then they hijack them and use them to send out thousands of spam emails. So Exchange is working as you would expect for a default install.

You say that your Exchange server is configured correctly, which it probably is for the Outlook clients that connect to it. But by default a correctly configured Exchange server will prevent any SMTP access to avoid being turned into a spam relay server.

So as I said before, configure your Exchange server to allow relay from your web server OR try using a different SMTP server - it will take you no time at all to change the SMTP server settings to use your gmail account and when that works you might then believe that I know what I am talking about and get your Exchange server configured to allow relaying from your web server.

This is all very common knowledge, with a quick google you could verify everything I am saying.
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 39758304
I've created New Receive Connector on port 25 in my Exchange server like
https://dl.dropboxusercontent.com/u/40211031/t627.png

Now I change the code to be

                    ...
                    SmtpClient client = new SmtpClient("my-friend.co", 25);
                    ...

Open in new window


But I still get the same exception and stacktrace.
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 39758351
Any help?
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39758404
I have no idea how to configure exchange server - thats a completely different question to the original one and requires different experts. I would post a new question in the correct topic for exchange server.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

If you don't know how to downgrade, my instructions below should be helpful.
Are you unable to connect or configure Hotmail email account in Microsoft Outlook 2010, 2007? Or Outlook.com emails are not downloading to Outlook? Lets’ see the problem and resolve Outlook Connector error syncing folder hierarchy (0x8004102A).
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now