我有以下程序,它会继续接收要发送的电子邮件。它运行良好,没有任何错误。突然间我注意到没有发生任何错误就发送电子邮件。下面是代码片段。在我的主要内容中,我已将其初始化为new Thread(new MailProcessor()).start();
,在我将其置于私有LinkedBlockingQueue<String> eMailQueue = new LinkedBlockingQueue<String>();
之外
从另一个班级我称之为eMailQueue.add(sendEmailMessage);
。我重新启动它恢复正常运行,我想知道为什么它停止了几个小时?
class MailProcessor implements Runnable {
public void run() {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("*****","******");
}
});
try
{
while (true) {
try
{
int count=0;
String emailMessageString = "";
emailMessageString = eMailQueue.take();
if (emailMessageString.equals(null)) {
System.out.println("Email Processor is shutting down");
break; // exit while loop, ends run() method
}
try
{
String[] eMArray = null;
eMArray = emailMessageString.split("@EmL@");
Message emailMessage = new MimeMessage(session);
emailMessage.setFrom(new InternetAddress("scvmgr@gmail.com"));
emailMessage.setRecipients(Message.RecipientType.TO,InternetAddress.parse(eMArray[1]));
//System.out.println("EArray Length is :"+eMArray.length+" "+"Second Email :"+eMArray[2]);
if(eMArray.length==3)
{
String cc1 = eMArray[2];
emailMessage.addRecipient(Message.RecipientType.CC,new InternetAddress(cc1));
}
//emailMessage.setRecipients(Message.RecipientType.CC,InternetAddress.parse(eMArray[2]));
//emailMessage.addRecipient(Message.RecipientType.CC,new InternetAddress(cc1));
String cc2 = "******";
emailMessage.addRecipient(Message.RecipientType.CC,new InternetAddress(cc2));
System.out.println("\n\nAlready in add receiptntTypeCC : "+emailMessageString);
emailMessage.setRecipients(Message.RecipientType.BCC,InternetAddress.parse("shaimyhtc@gmail.com"));
emailMessage.setSubject("******");
emailMessage.setText(eMArray[0]);
//eMailQueue.add(emailMessage);
Transport.send(emailMessage);
System.out.println("\n\nSend email :"+eMArray[0]);
}
catch (Exception e)
{
System.out.println("Transport Problem");
e.printStackTrace();
}
}
//System.out.println("Processing " + message);
//catch (IOException ex)
catch (Exception e)
{
e.printStackTrace();
}
}
}
catch(Throwable th)
{
th.printStackTrace();
}
}
}
答案 0 :(得分:0)
在你的while循环之外放一个(抓住Throwable)并记录下来。如果发生这种情况,请退出并重新启动程序。
答案 1 :(得分:0)
我建议你用日志框架替换你的sysouts(看看这个question)。通过检查日志文件找出发生的事情会好得多。