如何知道构建是否已经完成而没有失败

时间:2015-08-19 07:01:12

标签: java swing apache-commons

我是java的新手,我正在尝试使用apache commons邮件库构建一个简单的邮件客户端, 一切正常,但我想让JPaneOption或任何其他窗口通知我邮件是否已发送或失败。

这是我正在使用的代码:

package test;

import import org.apache.commons.mail.*;
   public class Mailer{
   public static void main(String[] args){

    Email email = new SimpleEmail();
    email.setHostName("smtp.googlemail.com");
    email.setSmtpPort(465);
    email.setAuthenticator("username", "password");
    email.setSSLOnConnect(true);
    email.setFrom("user@gmail.com");
    email.setSubject("TestMail");
    email.setMsg("This is a test mail ... :-)");
    email.addTo("foo@bar.com");
    email.send();
    }
}
谢谢你!

2 个答案:

答案 0 :(得分:1)

尝试以下

package test;

import import org.apache.commons.mail.*;
   public class Mailer{
   public static void main(String[] args){
    try{
    Email email = new SimpleEmail();
    email.setHostName("smtp.googlemail.com");
    email.setSmtpPort(465);
    email.setAuthenticator("username", "password");
    email.setSSLOnConnect(true);
    email.setFrom("user@gmail.com");
    email.setSubject("TestMail");
    email.setMsg("This is a test mail ... :-)");
    email.addTo("foo@bar.com");
    email.send();
JOptionPane.showConfirmDialog(this, "Email sent succeefully");
}catch(Exception e){
   JOptionPane.showMessageDialog(this,
    e.getMessage(),
    "Error",
    JOptionPane.ERROR_MESSAGE);
}
    }
}

答案 1 :(得分:0)

String message;
try {
    email.send();
    message = JOptionPane.showInputDialog(null, "Success");
    resultMessage = "The e-mail was sent successfully";
}
catch (Exception ex) {
    ex.printStackTrace();
    message = JOptionPane.showInputDialog(null,  ex.getMessage());
}
JOptionPane.showMessageDialog(null, message);