2009-07-27 13 views
0

私はJavamail APIを使用して自分のJavaコードで電子メールを送信しようとしています。問題は特定の行で発生しています。コードのJavaで電子メールを送信しようとする接続例外

一部です:

URLConnection c = u.openConnection();   
c.setDoInput(false);        
c.setDoOutput(true);    
c.connect(); 

エラーコードのc.connect()ラインで発生しています。私が得るエラーは:

connect. Timeout = -1 

java.net.ConnectException: Connection refused: connect 

これは私が得るすべての記述です。この問題を解決する方法がわかりません。助けてください。

+0

なぜURLConnectionを使用して電子メールを送信していますか? – skaffman

答えて

1

this pageをご覧になり、電子メールの送信方法の例をご覧ください。 電子メールを送信するには、トランスポートクラスを使用する必要があります。

public static void send(String smtpServer, String to, String from 
    , String subject, String body) throws Exception 
    { 

     Properties props = System.getProperties(); 
     // -- Attaching to default Session, or we could start a new one -- 
     props.put("mail.smtp.host", smtpServer); 
     Session session = Session.getDefaultInstance(props, null); 
     // -- Create a new message -- 
     Message msg = new MimeMessage(session); 
     // -- Set the FROM and TO fields -- 
     msg.setFrom(new InternetAddress(from)); 
     msg.setRecipients(Message.RecipientType.TO, 
     InternetAddress.parse(to, false)); 
     // -- We could include CC recipients too -- 
     // if (cc != null) 
     // msg.setRecipients(Message.RecipientType.CC 
     // ,InternetAddress.parse(cc, false)); 
     // -- Set the subject and body text -- 
     msg.setSubject(subject); 
     msg.setText(body); 
     // -- Set some other header information -- 
     msg.setHeader("X-Mailer", "LOTONtechEmail"); 
     msg.setSentDate(new Date()); 
     // -- Send the message -- 
     Transport.send(msg); 
     System.out.println("Message sent OK."); 
    } 

EDIT:

ので、あなたのコメントの...

チェック誰かブロックポート25であれば(ファイアウォール、他のアプリケーション)

認証も問題になる可能性、あなたは良いexamplを見つけることができるよりもhere

props.put("mail.smtp.auth", "true"); 
Authenticator auth = new SMTPAuthenticator("[email protected]", "mypassword"); 
+0

私はこのような例を試しましたが、 "SMTPホストに接続できませんでした:...ポート:25"というエラーメッセージが表示され、Transport.sned(msg)行に表示されます –

+0

次にもう一度やり直してください。接続プロパティを確認してください。ファイアウォールがあるかどうかを確認します。これはそれを行う方法です。 – Stroboskop

関連する問題