【问题标题】:Send email with System.Web.Mail使用 System.Web.Mail 发送电子邮件
【发布时间】:2013-02-05 18:05:59
【问题描述】:

我想用asp发送邮件。

我用这个代码

using System.Web.Mail;

MailMessage msg = new MailMessage();
msg.To = "aspnet@yahoo.com";
msg.From = "info@mysite.com";
msg.Subject = "Send mail sample";
msg.BodyFormat = MailFormat.Html;
string msgBody="Hello My Friend. This is a test.";
msg.Body = msgBody ;
SmtpMail.SmtpServer = "localhost";
SmtpMail.Send(msg);

但我得到错误:

错误的命令顺序。服务器响应是:此邮件服务器在尝试发送到非本地电子邮件地址时需要身份验证。请检查您的邮件客户端设置或联系您的管理员以验证是否为此服务器定义了域或地址。

如何用asp发送邮件?

【问题讨论】:

  • 错误消息非常清楚地指出:"This mail server requires authentication"。您似乎没有提供任何身份验证。如何提供它取决于邮件服务器的要求。它需要什么?
  • 您没有在系统上运行 smtp 服务,或者您提供了无效的凭据(我打赌前者)。

标签: asp.net


【解决方案1】:

我使用此代码。

 MailMessage msg = new MailMessage();
 msg.Body = "Body";

 string smtpServer = "mail.DomainName";
 string userName = "info@mysite.com";
 string password = "MyPassword";
 int cdoBasic = 1;
 int cdoSendUsingPort = 2;
 if (userName.Length > 0)
  {
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", smtpServer);
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25);
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", cdoSendUsingPort);
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", cdoBasic);
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
    }
    msg.To = user.Email;
    msg.From = "info@Mysite.com";
    msg.Subject = "Subject";
    msg.BodyEncoding = System.Text.Encoding.UTF8;
    SmtpMail.SmtpServer = smtpServer;
   SmtpMail.Send(msg);

【讨论】:

    【解决方案2】:

    您可能需要提供凭据。

    示例:

    smtpMail.Credentials = new NetworkCredential("username", "password")
    

    【讨论】:

    • System.Web.MailSmtpMail中没有.Credentials
    【解决方案3】:

    如果您尝试在不进行身份验证的情况下发送电子邮件,恐怕这是不可能的。如果您网站中的任何用户可以在没有密码的情况下发送电子邮件,那就太可怕了。它将允许用户从其他个人帐户发送电子邮件。所以考虑到安全性,发送邮件需要提供邮箱地址和密码

    var fromAddress = "";      // Email Address here. This will be the sender.
    string fromPassword = ""; // Password for above mentioned email address.
    var toAddress = "";//   Receiver email address here
    string subject = "Hi";
    string body = "Body Text here";
    var smtp = new System.Net.Mail.SmtpClient();
        {
            smtp.Host = "smtp.gmail.com"; // this is for gmail.
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
            smtp.Timeout = 20000;
        }
    smtp.Send(fromAddress, toAddress, subject, body); 
    

    [已编辑] 对不起我的错误我没有注意到。他们都用于相同的目的。如果您使用更高版本(2.0 或更高版本)的 .Net 框架,请使用 System.Net.Mail。如果您使用 System.Web.Mail,它只会显示一条警告,说明不推荐使用。但这会奏效。

    这是 System.web.mail 的答案

      MailMessage mail = new MailMessage();
      mail.To.Add("to@domain.com");
      mail.From = new MailAddress("from@domain.com");
      mail.Subject = "Email using Gmail";
      mail.Body = "";
    
    
      mail.IsBodyHtml = true;
      SmtpClient smtp = new SmtpClient();
      smtp.Host = "smtp.gmail.com";
    smtp.EnableSsl = true;
    smtp.Credentials = new System.Net.NetworkCredential(mail.From,"YourPassword");
        smtp.Send(mail);
    

    【讨论】:

    • 答案是关于System.Web.Mail 而不是System.Net.Mail
    猜你喜欢
    • 1970-01-01
    • 2016-04-17
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多