2012-02-22 9 views
0

以下は、Javaを使用して電子メールを送信するために書いた簡単なテストクラスです。私はローカルホストからメッセージを送信しようとしています。しかし、私は次のようなエラーメッセージが出ます:Javaでメールを送信する

javax.mail.MessagingException: Unknown SMTP host: http://localhost:8080/; 
nested exception is: 
java.net.UnknownHostException: http://localhost:8080/ 
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1280) 

を私は単純に「localhost」をに値をホストするために変更するが、私は同じ問題を取得します。修正に関するアイデアはありますか?実際のサーバーは機能しますか?

import java.util.Properties; 
import javax.mail.*; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage;  

public class MyEmail { 

public static void main(String... args) { 
    String to = "[email protected]"; 
    String from = "[email protected]"; 
    String host = "http://localhost:8080/"; 

    Properties properties = System.getProperties(); 
    properties.setProperty("mail.smtp.host",host); 
    Session session = Session.getDefaultInstance(properties); 

    try{ 
     MimeMessage message = new MimeMessage(session); 
     message.setFrom(new InternetAddress(from)); 
     message.addRecipient(Message.RecipientType.TO,new InternetAddress(to)); 
     message.setSubject("This is a subject"); 
     message.setText("The text is what it is the text"); 
     Transport.send(message); 
     System.out.println("Successful"); 
    }catch(MessagingException mx){ 
     mx.printStackTrace(); 
    } 
} 
} 
+0

あなたはどこでメールデーモンを実行していますか? –

答えて

3

ホストの値は、ホスト名またはIPアドレスにする必要があります。これはHTTPではありません。

は、ポートを設定するあなたはどんなメッセージを送信するために指定したホストで実行されているメール(SMTP)サーバーが必要です(文字列として)あなたのポート番号へのプロパティ mail.smtp.port

+0

私はそれを「25」に変更しても動作しませんでした。応答していただきありがとうございます。 – tribal

+0

実行しているSMTPサーバーの種類は?ブラウザで使用できるものは動作しません。 –

+0

私は答えを最終的に見つけた[ここ](http://stackoverflow.com/questions/46663/how-do-you-send-email-from-a-java-app-using-gmail#47452) – tribal

0

を設定します。ホストはSMTPサーバのアドレス(http://なし)に対応し、ポートは設定されたsmtpポートに対応します。あなたの問題を見ると、SMTPサーバとの接続に問題があります。実行中のsmtpサーバに従ってホストとポートのパラメータを修正してから、再試行してください。

+0

私は最終的に答え[ここ](http://stackoverflow.com/questions/46663/how-do-you-send-email-from-a-java-app-using-gmail#47452) – tribal