2016-05-13 11 views
-1

電子メールアドレス、送信ボタン、エラーラベルのテキストボックスを持つテストページを作成しました。私はdon`ASP.NETからクライアントに電子メールを送信できない

System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: The remote name could not be resolved: 'smtp.google.com' at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6) at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback) at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message) --- End of inner exception stack trace --- at System.Net.Mail.SmtpClient.Send(MailMessage message) at email.btnSend_Click(Object sender, EventArgs e) in d:\WebDev\Test\email.aspx.cs:line 28

しかし、それは私のために働いていない:それは私に示して完全なエラーがある

try 
{ 
    MailMessage mail = new MailMessage("[email protected]", (string)txtEmail.Text); 
    SmtpClient client = new SmtpClient(); 
    client.Port = 25; 
    client.DeliveryMethod = SmtpDeliveryMethod.Network; 
    client.UseDefaultCredentials = false; 
    client.Host = "smtp.google.com"; 
    mail.Subject = "this is a test email."; 
    mail.Body = "this is my test email body"; 
    client.Send(mail); 

    lblError.Text = "Message sent!"; 
} 
catch (Exception ex) 
{ 
    lblError.Text = ex.ToString(); 
} 

:ここ

は、私はフォーラムで、ここで見てきたC#コードであります何をすべきか知っています!

+0

「それはありません「働く」という意味ですか?エラーメッセージが表示されますか? –

+1

あなたはどんなエラーがありますか? – user1666620

+0

申し訳ありませんが、私は質問本体に完全なエラーを追加しました – D4NieLDev

答えて

1

あなたは次に、あなたのコードを送信するには、この

<system.net> 
    <mailSettings> 
     <smtp deliveryMethod="Network" from="[email protected]"> 
       <network host="smtp.host.net" userName="yourUsername" password="yourPassword" port="587" enableSsl="true" /> 
     </smtp> 
    </mailSettings> 
</system.net> 

を追加することにより、web.configファイル内のすべてのSMTPクライアントの資格情報を設定することができます電子メールはより簡単になります

try 
{ 
    using (var client = new SmtpClient()) 
    { 
     MailMessage mail = new MailMessage("[email protected]", (string)txtEmail.Text); 
     mail.Subject = "this is a test email."; 
     mail.Body = "this is my test email body"; 
     client.Send(mail); 
    } 

    lblError.Text = "Message sent!"; 
} 
catch (Exception ex) 
{ 
    lblError.Text = ex.ToString(); 
} 
+0

WOWありがとう!!!!あなたは本当に私を助けました! – D4NieLDev

3

あなたのホスト名とポートが正しくありません - Gmailはsmtp.gmail.comないsmtp.google.comを使用し、ポートが587ではない25

あなたのコードはまた、任意のネットワーク資格情報を提供していないです。 UseDefaultCredentialsを正しくfalseに設定しましたが、新しいNetworkCredentialsも指定する必要があります。

ます。また、SSLを有効にする必要が
client.UseDefaultCredentials = false; 
client.Credentials = new System.Net.NetworkCredential("yourusername", "yourpassword"); 

client.EnableSsl = true; 
+0

特別なクラスをインポートする必要がありますが、 'NetworkCredential'(私はこのクラスのオブジェクトを定義することはできません)にエラーが表示されますか? – D4NieLDev

+1

@ D4NieLDevが名前空間の参照を追加しました。 – user1666620

+0

今私はこのエラーがあります: ** System.Net.Mail.SmtpException:SMTPサーバーがセキュリティで保護された接続を必要とするか、クライアントが認証されていません。サーバーの応答は:5.7.0 STARTTLSコマンドを最初に発行する必要があります** – D4NieLDev

関連する問題