【问题标题】:Email code doesn't work电子邮件代码不起作用
【发布时间】:2018-01-11 22:29:47
【问题描述】:

以下是我发送邮件的代码,它显示错误

服务器拒绝发件人地址。服务器响应为:530 5.7.1 需要身份验证

    System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
    mail.To = "hsbanga@yahoo.com";
    mail.From = "hsbanga@yahoo.com";
    mail.Subject = "Query from agnihotrindt";

    mail.Body = "Name : " + TextBox1.Text + "<br/>" + "Email: " + TextBox2.Text + "<br/>Contact :" + TextBox3.Text + "<br/><br/><b>Address:</b><br/>" + TextBox4.Text + "<br/><b>Comment:</b><br/>" + TextBox5.Text ;
    mail.BodyFormat = System.Web.Mail.MailFormat.Html;

    mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = 25;
    mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;
    mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
    mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "hsbanga@yahoo.com";
    mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "secret";
    mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = "false";

    System.Web.Mail.SmtpMail.SmtpServer = "smtp.mail.yahoo.com";
    System.Web.Mail.SmtpMail.Send(mail);

【问题讨论】:

标签: c# asp.net email smtpclient


【解决方案1】:

Use

SmtpClient emailClient = new SmtpClient("smtp.mail.yahoo.com");
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential("xyz@yahoo.com","*******"); 
emailClient.EnableSsl = true;
emailClient.Credentials = SMTPUserInfo;
emailClient.Port = 465;

MailMessage message = new System.Net.Mail.MailMessage("xyz@gmail.com", "someone@something.something", "fire!!", "Call up 911 and inform my house is on fire and my phone too");
emailClient.Send(message);

【讨论】:

    【解决方案2】:

    首先,您使用的是哪个版本的 .NET?如果我没记错的话,System.Web.Mail 在 .NET 3.5 中已被弃用。建议使用 System.Net.Mail 命名空间。

    至于发送带有凭据的邮件,我没有 C# 代码作为示例,因为我将所有邮件配置添加到 system.net 元素中的 web.config 中。下面是一个如何查找 Yahoo 的示例。

    <system.net>
      <mailSettings>
        <smtp deliveryMethod="Network">
          <network host="smtp.mail.yahoo.com" port="465" userName="xyz@yahoo.com" password="*****" enableSsl="true" defaultCredentials="false" />
        </smtp>
      </mailSettings>
    </system.net>
    

    这还允许更改邮件配置,而无需在每次更改密码时重新编译您的应用程序。然后,您可以像这样实例化您的 SmtpClient:

    var client = new SmtpClient();
    client.Send(mailMessage);
    

    【讨论】:

      【解决方案3】:

      你可以用这个:

      SmtpClient smtpClient = new SmtpClient("smtp.mail.yahoo.com", 465);
      smtpClient.Credentials = new System.Net.NetworkCredential("hsbanga@yahoo.com", "secret");
      smtpClient.EnableSsl = true;
      smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
      MailMessage mail = new MailMessage();
      
      smtpClient.Send(mail);
      

      【讨论】:

        猜你喜欢
        • 2013-01-22
        • 2012-02-14
        • 1970-01-01
        • 1970-01-01
        • 2014-07-05
        • 2020-06-18
        • 1970-01-01
        • 2011-05-18
        • 2016-12-14
        相关资源
        最近更新 更多