简体   繁体   English

无法使用带有c#的ASP.net从SMTP服务器发送邮件

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

I have created one webpage in ASP.net with C#. 我用ASP#在ASP.net中创建了一个网页。 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. 但是,当我点击此按钮获取此异常时: - System.Net.Mail.SmtpException:事务失败。 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 服务器响应是:5.7.1:在System.Net.Mail.SmtpTransport.SendMail的System.Net.Mail.RecipientCommand.CheckResponse(SmtpStatusCode statusCode,String response)中拒绝中继访问(MailAddress发送者,MailAddressCollection收件人,String deliverNotify,布尔值在System.Net.Mail.SmtpClient.Send(MailMessage消息)的System.Net.Mail.SmtpClient.Send(MailMessage消息)中的allowUnicode,SmtpFailedRecipientException&exception)在c:\\ Users \\ jay.desai \\ Documents \\ Visual Studio 2013 \\ WebSites \\ WebSite2中的_Default.Button1_Click(Object sender,EventArgs e) Default.aspx.cs:第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. 如果您总是要发送到单个电子邮件域,那么您通常可以使用已注册的MX服务器作为域。 In this case (ril.com) the MX server list includes several primary mail servers, but the first one for me is: gwhydsmtp010.ril.com. 在这种情况下(ril.com),MX服务器列表包括几个主要邮件服务器,但第一个对我来说是: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登录凭据,如下所示:

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. 登录服务器通常可以解决大多数5.7.1错误。

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). 如果您在Exchange服务器上拥有管理员权限,则可能有用的最后一种方法是设置SMTP连接器以允许来自特定源地址(您的Web服务器)的中继。 I wouldn't recommend this however as any open relay is a Bad Idea(tm). 我不建议这样做,因为任何开放中继都是坏主意(tm)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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