簡體   English   中英

關於JAVA郵件傳輸對象的快速說明

[英]Quick clarification about the JAVA mail Transport object

我正在嘗試使用Java郵件API實現郵件模塊。 到目前為止,我所做的是

session = Session.getInstance(serverDetails,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(userName, password);
                    }
                });
try {
            transport = session.getTransport("smtp");
            transport.connect();
        } catch (MessagingException e) {
            System.out.println(e.getMessage());
        }
MimeMessage message = new MimeMessage(session);
        // set the mail sender address
        message.setFrom(new InternetAddress(userName));
        // set the recipient addresses
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(toAddr));
        // set subject of the mail
        message.setSubject(subject);
        // Set the body of the message
        message.setText(body);
        // Create a SMTP message object by which we will be able to get the
        // delivery status
        SMTPMessage smtpMsg = new SMTPMessage(message);
        smtpMsg.setReturnOption(SMTPMessage.RETURN_HDRS);
        smtpMsg.setNotifyOptions(SMTPMessage.NOTIFY_SUCCESS
                | SMTPMessage.NOTIFY_FAILURE);
        // attach the listeners for the connection and transmission
        transport.addConnectionListener(this);
        transport.addTransportListener(this);
        // connect to the server and send the message
        try{
            transport.sendMessage(smtpMsg, smtpMsg.getAllRecipients());
        }catch(IllegalStateException e){
            e.printStackTrace();
        }

我只初始化一次傳輸,並嘗試在一個循環中發送多個郵件(上面的代碼未顯示循環部分)。 可以嗎? 如何使用trasport對象的單個實例發送多封郵件?

我收到以下錯誤

  org.quartz.core.ErrorLogger schedulerError
    SEVERE: Job (group1.job1 threw an exception.
    org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: java.lang.IllegalStateException: Not connected]
        at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
        at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525)
    Caused by: java.lang.IllegalStateException: Not connected
        at com.sun.mail.smtp.SMTPTransport.checkConnected(SMTPTransport.java:2263)
        at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1075)
        at org.mail.MailSender.sendMailAction(MailSender.java:237)
        at org.mail.MailSender.sendMail(MailSender.java:100)
        at com.util.PopulateMailQueue.populateQueue(PopulateMailQueue.java:172)
        at org.mail.cronjob.CronJob.execute(CronJob.java:14)
        at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
        ... 1 more

請幫助我在這里指出問題。

是的,您可以在連接之后和關閉傳輸之前循環發送多個郵件。

transport.connect();

for (int i = 0; i < count; i++) {

    //  Create your message
        transport.sendMessage(smtpMsg, smtpMsg.getAllRecipients());
    }

transport.close();

您收到該錯誤,可能是因為在發送所有郵件之前關閉了傳輸

您可以檢查控制台日志嗎? 可能是您缺少異常。 因為這:

....
   transport.connect();
} catch (MessagingException e) {
    System.out.println(e.getMessage());
}

僅將問題記錄到控制台,然后繼續。 如果連接失敗,您甚至不會在常規日志中看到它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM