3

I have created one webpage in ASP.net with C#. In which I have put one button when this button clicks need to send mail. But, when I click on this button getting this exception :- System.Net.Mail.SmtpException: Transaction failed. The server response was: 5.7.1 : Relay access denied at System.Net.Mail.RecipientCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at _Default.Button1_Click(Object sender, EventArgs e) in c:\Users\jay.desai\Documents\Visual Studio 2013\WebSites\WebSite2\Default.aspx.cs:line 47

Please refer below code:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
     try
        {
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("[email protected]");
            mail.Subject = "Pallet Shortage Emergency";
            mail.To.Add("[email protected]");
            mail.Body ="Only 100 pallets are availabe in ASRS. This is system generated mail do not reply";          
            mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
            SmtpClient smtp = new SmtpClient("rmta010.zmail.ril.com",25);
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false; 
            System.Net.NetworkCredential("[email protected]", "1234");             
            ServicePointManager.ServerCertificateValidationCallback =
            delegate(object s, X509Certificate certificate,
            X509Chain chain, SslPolicyErrors sslPolicyErrors)
            { return true; };
            smtp.Send(mail);

        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }

}

}

3
  • Try run your program with administrator permissions. Commented Dec 5, 2015 at 10:09
  • @MaximGoncharuk same problem Commented Dec 5, 2015 at 10:25
  • Look at this link Commented Dec 5, 2015 at 10:28

1 Answer 1

0

The server error is indicating that it will not relay messages. Usually this means that you need to authenticate with the server before it will allow you to send email, or that it will only accept emails for delivery from specific source domains.

If you are always going to be sending to a single email domain then you can generally use the registered MX server for the domain. In this case (ril.com) the MX server list includes several primary mail servers, but the first one for me is: gwhydsmtp010.ril.com. I'd try that as the target mail server if your website is hosted outside the network that the mail server is on.

Alternatively you can provide SMTP login credentials to the SmtpClient object like this:

SmtpClient smtp = new SmtpClient("rmta010.zmail.ril.com",25);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("username", "password");

Logging in to the server will generally solve most 5.7.1 errors.

One final method that might be useful if you have admin rights on the Exchange server is to setup an SMTP connector to allow relay from a specific source address (your web server). I wouldn't recommend this however as any open relay is a Bad Idea(tm).

Sign up to request clarification or add additional context in comments.

3 Comments

I didn't get your first technique. I am going to send mail to one domain's emails only i.e ril.com. In this case what I have to do? Can you please elaborate. Second technique is not worked.
Are you sure you used an account that is valid for that service? What is zmail? Is it related to O365? Why are you using that mail server instead of the ones listed in the mail exchanger (MX) DNS records?
[email protected] account is valid I can able to mail from outlook from this account. Whenever I configure outlook I manually enter Outgoing mail server (SMTP) ==> rmta010.zmail.ril.com .Its not related to O365.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.