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

認証せずにメールを送信しようとしている場合、それは不可能だと思います。Webサイトのユーザーがパスワードなしで電子メールを送信できる場合、それは恐ろしいことです。これにより、ユーザーは他の人のアカウントからメールを送信できるようになります。したがって、セキュリティを考慮すると、メールアドレスとパスワードを提供するためにメールを送信する必要があります

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

[編集] 申し訳ありませんが私の間違い私はそれに気づいていませんでした。それらは両方とも同じ目的で使用されました。.Net Frameworkの上位バージョン(2.0以降)を使用している場合は、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 に答える