0

したがって、私の問題は次のとおりです。Javaで作成したメールクライアントがあり、POP3でメールをチェックした後、SMTP経由でメールを送信できません。

私が捕らえた例外は、トランスポートプロトコル=nullであると言っています。

POP3接続の前に問題がないため、コードは正常に機能しています。私はその接続を閉じ、それらはすべてプライベート関数であると確信しているので、変数は互いに効果的ではありません。

私がすべてを語ったことを願っています。

アイデアをありがとう。

コード:

pop3接続

  // Connect to the POP3 server
  Session session = Session.getDefaultInstance(props, null);
  Store store = session.getStore("pop3");
  store.connect(host, username, password);

  // Open the folder
  Folder inbox = store.getFolder("INBOX");

  inbox.open(Folder.READ_ONLY);

  // Get the messages from the server
  Message[] messages = inbox.getMessages();


  fromMailAddress.setText(userAccount);

  // Close the connection
  // but don't remove the messages from the server
  inbox.close(false);
  store.close();
  props.clear();
    }
catch (Exception ex) {
     JOptionPane.showMessageDialog(null,"user input error", "error", JOptionPane.ERROR_MESSAGE);


    }

smtp-メール送信

    Properties property = new Properties();
    property.setProperty("mail.transport.protocol", "smtp");
    property.setProperty("mail.host", "mymailserver");
    property.setProperty("mail.user", "myusername");
    property.setProperty("mail.password", "mypassword");
    Session mailSession = Session.getDefaultInstance(property, null);
    mailSession.setDebug(true);
try{
    Transport transport = mailSession.getTransport();

    MimeMessage message = new MimeMessage(mailSession);
    message.setSubject("HTML  mail with images");
    message.setFrom(new InternetAddress("myaddress@gmail.com"));
    message.setContent("<h1>Hello world</h1>", "text/html");
    message.addRecipient(Message.RecipientType.TO,
    new InternetAddress("xyz@gmail.com"));

    transport.connect();
    transport.sendMessage(message,
        message.getRecipients(Message.RecipientType.TO));
    transport.close();
    property.clear();
    }
    catch(Exception ex)
    {

            JOptionPane.showMessageDialog(null,"e-mail sending failed", "Error", JOptionPane.ERROR_MESSAGE);
    }
4

1 に答える 1

1

popモジュールとsmtpモジュールは独立していません。デフォルトのセッションを共有しています。javamailのデフォルトセッション(Session.getDefaultInstance)に依存する代わりに、pop用とsmtp用の独自のセッションを作成することをお勧めします。

于 2009-12-13T21:50:41.513 に答える