0

Javamail APIを使用して、Javaコードでメールを送信しようとしています。問題は特定の回線で発生しています。

コードの一部は次のとおりです。

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

エラーはコードのc.connect()行で発生しています。私が得るエラーは次のとおりです。

connect. Timeout = -1

java.net.ConnectException: Connection refused: connect

そして、これが私が得るすべての説明です。この問題を解決する方法がわかりません。助けてください。

4

1 に答える 1

1

電子メールの送信方法の例については、このページをご覧ください。メールを送信するには、クラスTransportを使用する必要があります。

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.");
  }

編集:

あなたのコメントのために...

誰かがポート25をブロックしていないか確認してください(ファイアウォール、他のアプリケーション)

ここで良い例を見つけることができるよりも、認証も問題になる可能性があります

props.put( "mail.smtp.auth", "true" );
Authenticator auth = new SMTPAuthenticator( "me@sender.net", "mypassword" );
于 2009-07-27T11:05:49.780 に答える