Unable to send mail

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.
LVL 12
HuaMin ChenProblem resolverAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dale BurrellDirectorCommented:
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.
HuaMin ChenProblem resolverAuthor Commented:
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)
Dale BurrellDirectorCommented:
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).
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

HuaMin ChenProblem resolverAuthor Commented:
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

Dale BurrellDirectorCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HuaMin ChenProblem resolverAuthor Commented:
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

Dale BurrellDirectorCommented:
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.
HuaMin ChenProblem resolverAuthor Commented:
I don't know how to resolve the exception (with the stacktrace), due to the codes posted above.
Dale BurrellDirectorCommented:
Are you sure you have the correct port number?
Dale BurrellDirectorCommented:
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
HuaMin ChenProblem resolverAuthor Commented:
This is not a port number but a problem to send out mail. which certificate should be collected for the problem I've got?
Dale BurrellDirectorCommented:
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.
HuaMin ChenProblem resolverAuthor Commented:
Do you or anyone have any ideas to resolve this problem encountered? Thanks
AshokSr. Software EngineerCommented:
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
HuaMin ChenProblem resolverAuthor Commented:
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)
Dale BurrellDirectorCommented:
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
HuaMin ChenProblem resolverAuthor Commented:
Sorry, where to open SMTP virtual server properties within IIS 6 manager?
t622.png
Dale BurrellDirectorCommented:
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?
HuaMin ChenProblem resolverAuthor Commented:
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.
Dale BurrellDirectorCommented:
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.
HuaMin ChenProblem resolverAuthor Commented:
My Exchange server is working fine there. What to set up for SMTP? What to check within my Exchange server?
Dale BurrellDirectorCommented:
HuaMin ChenProblem resolverAuthor Commented:
But it is not for Exchange server 2010.
Dale BurrellDirectorCommented:
I'm sure you can google it.
HuaMin ChenProblem resolverAuthor Commented:
But I really am not sure, if setting up SMTP, is the solution to the problem I got.
Dale BurrellDirectorCommented:
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.
HuaMin ChenProblem resolverAuthor Commented:
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?
Dale BurrellDirectorCommented:
But are you actually using the SMTP server in IIS6? You said you were using exchange server?
HuaMin ChenProblem resolverAuthor Commented:
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.
Dale BurrellDirectorCommented:
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.
HuaMin ChenProblem resolverAuthor Commented:
I am still stuck with the codes and cannot move further. what to award?
Dale BurrellDirectorCommented:
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.
HuaMin ChenProblem resolverAuthor Commented:
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.
Dale BurrellDirectorCommented:
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.
HuaMin ChenProblem resolverAuthor Commented:
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.
HuaMin ChenProblem resolverAuthor Commented:
Any help?
Dale BurrellDirectorCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.