2

次の方法を使用して、プログラムから電子メールを送信しようとしています。

    void sendEmails(Tutor t, Date date, Time time, String tuteeName, String tuteeEmail){
    System.out.println("sending emails");
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "465");

            SimpleDateFormat timeFmt =  new SimpleDateFormat("hh:mm a");
            SimpleDateFormat dateFmt =  new SimpleDateFormat("EEE, MMMM dd");         
            String datePrint = dateFmt.format(date);
            String timePrint = timeFmt.format(time);                
            Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
                   @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
      });

            try {

        Message tutorMessage = new MimeMessage(session);

        tutorMessage.setFrom(new InternetAddress("laneycodingclub@gmail.com"));

        tutorMessage.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse(t.getEmail()));

        tutorMessage.setSubject("Tutoring Appointment Scheduled");
        tutorMessage.setText("Hey " + t.getName() + 
                            "\n \t You have a new appointment scheduled on "  + datePrint + " at " + timePrint + 
                            "with " + tuteeName + ". \n If you cannot make this appointment, please contact club leadership immediately. "
                            + "\n Thanks for helping out!");

        Transport.send(tutorMessage);

        System.out.println("Done sending");

    } catch (MessagingException e) {
                    System.out.println("messagingerror");
                    e.printStackTrace();
    }
}

しかし、プログラムがこのメソッドに到達すると、GUI がロックアップし、プログラムがフリーズします。Java プログラムで電子メールを使用しようとしたのはこれが初めてなので、問題がどこにあるのかわかりません。

4

3 に答える 3

1

電子メールを同期的に送信することは避けたほうがよいでしょう。

一般に、アプリケーションの動作が遅くなり、さらに悪いことに、メール サーバーが到達不能または応答しない場合などに、アプリケーションがフリーズする可能性があります。

いくつかの非同期メカニズムを使用します。

この場合、メール サーバーに問題があると確信しています。

スタンドアロンの Java プログラムを使用して、これらのサーバー パラメータを使用して電子メールを送信できることを確認してください。

于 2013-03-16T04:22:14.200 に答える