3

我想用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发送邮件?

4

3 回答 3

4

我使用此代码。

 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);
于 2013-02-05T10:05:59.887 回答
1

您可能需要提供凭据。

例子:

smtpMail.Credentials = new NetworkCredential("username", "password")
于 2013-02-02T13:53:29.920 回答
0

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

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);
于 2013-02-02T14:04:57.263 回答