Click here to Skip to main content
14,923,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have windows service for mail sending its working fine if i send the mail to the client domain but when i try to send mail to gmail or any other domain im getting error
Mailbox unavailable. The server response was: 5.7.1 Unable to relay

im using SMTP for mail sending i know that it is related to SMTP relay but i dont know how to resolve it please give me solution

What I have tried:

try
            {
                System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();

                SmtpClient smtp = new SmtpClient();

                smtp.Host = "172.16.5.2";
                smtp.EnableSsl = false;
                smtp.Port = 25;

                msg.From = new MailAddress("Client domain mail id");

                msg.To.Add("xyz@gmail.com");

                msg.Subject = subject;

                msg.Body = body;

                msg.IsBodyHtml = true;

                if (AttachmentPath != "")
                {
                    msg.Attachments.Add(new Attachment(AttachmentPath));
                }

                smtp.Send(msg);
                
                return (1);
                
            }
            catch (Exception ex)
            {
                TraceService(ex.Message);
                return (0);
            }
                       
        }
Posted
Updated 22-Mar-17 22:22pm
Comments
Garth J Lancaster 23-Mar-17 0:27am
   
you don't seem to be authenticating to your SMTP server - maybe they won't send to another domain unless you're authenticated

I'd also have a look at some of the answers here https://www.codeproject.com/Questions/94257/Mailbox-unavailable-The-server-response-was
Member 12966735 23-Mar-17 0:29am
   
thank you for the answer can please explain me how to authenticate it i mean how to resolve this

1 solution

In the old days relaying mail was no problem and most servers accepted mail for any destination. But with increasing spam activity the setup has been changed and requires authentication. That means that you have to use an existing mail account at the SMTP server (your client domain mail id), connect to the server using an authentication method, and send the password for your account.

You are probably already using authentication with your mail clients (Outlook, Thunderbird, etc). Just use the same parameters as there. Note that a different port may be used.

In general use something like
C#
// These depend on the requirements of the server
//smtp.EnableSsl = true;
//smtp.Port = 465;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("yourusername", "yourpassword");
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900