简体   繁体   中英

Cannot able to send mail from SMTP server using ASP.net with c#

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("serveradmin.dmd@ril.com");
            mail.Subject = "Pallet Shortage Emergency";
            mail.To.Add("jay.desai@ril.com");
            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("serveradmin.dmd@ril.com", "1234");             
            ServicePointManager.ServerCertificateValidationCallback =
            delegate(object s, X509Certificate certificate,
            X509Chain chain, SslPolicyErrors sslPolicyErrors)
            { return true; };
            smtp.Send(mail);

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

}

}

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM