2012-03-27 17 views
10

localhostサーバーから電子メールを送信する方法を教えてください。 私はこの例を書いたが、動作しないエラーは"メールの送信に失敗しました"です。ローカルホストサーバー経由でasp.netで電子メールを送信

SmtpClient smtpClient = new SmtpClient(); 
     smtpClient.Host = "localhost"; 
     smtpClient.Port = 25; 
     smtpClient.EnableSsl = false; 
     smtpClient.Credentials = new NetworkCredential("[email protected]", "secret"); 
     smtpClient.Send("[email protected]", "[email protected]", "Let’s eat lunch!", "Lunch at the Steak House?");//here is the error 

web.configで何をすればよいですか?ここで

+1

localhostにSMTPを設定していますか? – Habib

+0

あなたはlocalhostを使用していて、yahooの資格情報を使っていますが、これはうまくいかないと思います。 – Habib

答えて

19

屋:) localhost-with-aspnet-without-smtp-server

は、それはあなたのためにあなたはそれがする必要があるように動作している場合、私が知ってくださいしてみましょう行きます。


上記のリンクは機能しないため、回答を改善します。テスト目的のために

我々はこのようにlocalhostを使用することができます:リンクが再びダウンした場合には、基本的に私たちは、このようにweb.configファイルを変更する必要がHow to Test Email Without Configure SMTP in ASP.NET

<system.net> 
    <mailSettings> 
     <smtp deliveryMethod="SpecifiedPickupDirectory"> 
     <specifiedPickupDirectory pickupDirectoryLocation="C:\Mails\"/> 
     </smtp> 
    </mailSettings> 
    </system.net> 

とC#のコード

MailMessage mailMessage = new MailMessage(); 
    MailAddress fromAddress = new MailAddress("[email protected]"); 
    mailMessage.From = fromAddress; 
    mailMessage.To.Add("[email protected]"); 
    mailMessage.Body = "This is Testing Email Without Configured SMTP Server"; 
    mailMessage.IsBodyHtml = true; 
    mailMessage.Subject = " Testing Email"; 
    SmtpClient smtpClient = new SmtpClient(); 
    smtpClient.Host = "localhost"; 
    smtpClient.Send(mailMessage); 

これは、希望のディレクトリにファイルを出力します。

+0

Sidenote: 'System.Web.Mail.MailMessage'は廃止されました。あなたは 'System.Net.Mail.MailMessage'を使うことができます。 – rst

2

web.configでSMTPサーバーの設定を指定する必要があります。オンラインいくつかの例(例えばthis

<system.net> 
    <mailSettings> 
     <smtp deliveryMethod="Network" from="[email protected]"> 
      <network host="smtp.mail.com" userName="[email protected]" password="pwd" port="25"/> 
     </smtp> 
    </mailSettings> 
</system.net> 

があります次に、あなたは単に送信するSmtpClientクラスを使用することができます。ここ

SmtpClient smtpClient = new SmtpClient(); 
smtpClient.EnableSsl = true; 

MailMessage msg = new MailMessage(); 
msg.To.Add("[email protected]"); 
msg.Subject = "test"; 
msg.Body = "test body"; 

smtpClient.Send(msg); 
+0

余分なSMTPサーバーは必要ありません...;) – walther

+0

localhostにインストールされていると私は同意します。外部プロバイダ(Rackspaceなど)を使用している場合はどうなりますか? – Strillo

2

はサンプルです:

public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body) 
{ 
    // Instantiate a new instance of MailMessage 
    MailMessage mMailMessage = new MailMessage(); 

    // Set the sender address of the mail message 
    mMailMessage.From = new MailAddress(from); 
    // Set the recepient address of the mail message 
    mMailMessage.To.Add(new MailAddress(to)); 

    // Check if the bcc value is null or an empty string 
    if ((bcc != null) && (bcc != string.Empty)) 
    { 
     // Set the Bcc address of the mail message 
     mMailMessage.Bcc.Add(new MailAddress(bcc)); 
    }  // Check if the cc value is null or an empty value 
    if ((cc != null) && (cc != string.Empty)) 
    { 
     // Set the CC address of the mail message 
     mMailMessage.CC.Add(new MailAddress(cc)); 
    }  // Set the subject of the mail message 
    mMailMessage.Subject = subject; 
    // Set the body of the mail message 
    mMailMessage.Body = body; 

    // Set the format of the mail message body as HTML 
    mMailMessage.IsBodyHtml = true; 
    // Set the priority of the mail message to normal 
    mMailMessage.Priority = MailPriority.Normal; 

    // Instantiate a new instance of SmtpClient 
    SmtpClient mSmtpClient = new SmtpClient(); 
    // Send the mail message 
    mSmtpClient.Send(mMailMessage); 
} 

そして関数を呼び出します:

SendMailMessage("[email protected]", "[email protected]", "[email protected]", "[email protected]", "Sample Subject", "Sample body of text for mail message") 
+0

私は 'System.InvalidOperationException:SMTPホストが指定されていませんでした。 – mattalxndr

1
bool ret = true; 

      try 
      { 
       string _smtpServer = ConfigurationSettings.AppSettings["appEmailHost"]; 

       Web.Mail.Mail mail = new Web.Mail.Mail(_smtpServer,   
     System.Web.Mail.MailFormat.Html, System.Web.Mail.MailPriority.Normal); 
       mail._From = [email protected]; 
       mail._To = [email protected]; 
       mail._Subject = subject; 

       mail._Body = body; 
       mail.SendMail(); 
       ret = true; 
      } 
      catch(Exception exp) 
      { 
       _GravaErro(exp); 
       ret = false; 
      } 

      return ret; 
関連する問題