简体   繁体   English

关于JAVA邮件传输对象的快速说明

[英]Quick clarification about the JAVA mail Transport object

Am trying to implement a mailing module using Java mail API. 我正在尝试使用Java邮件API实现邮件模块。 So far what I have done is 到目前为止,我所做的是

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();
        }

I am initialising transport only once and trying to send multiple mails in a loop(abve code doesn't shows the looping part). 我只初始化一次传输,并尝试在一个循环中发送多个邮件(上面的代码未显示循环部分)。 is it allowed? 可以吗? can I send multiple mails with single instance of trasport object? 如何使用trasport对象的单个实例发送多封邮件?

I am getting the following error 我收到以下错误

  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

Please help me pointing out the problem here. 请帮助我在这里指出问题。

Yes, you can send multiple mails in a loop after connecting and before closing the transport. 是的,您可以在连接之后和关闭传输之前循环发送多个邮件。

transport.connect();

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

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

transport.close();

You received that error, maybe because you closed the transport before sending all the mails 您收到该错误,可能是因为在发送所有邮件之前关闭了传输

Can you check your console logs? 您可以检查控制台日志吗? It might be that you are missing an exception. 可能是您缺少异常。 Because this: 因为这:

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

only logs the problem to console and then continues on. 仅将问题记录到控制台,然后继续。 If the connect fails, you wont even see it in your normal log. 如果连接失败,您甚至不会在常规日志中看到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM