如何克服异常 - 在java中发送电子邮件时字符串``'中的非法地址'

时间:2014-10-20 08:26:33

标签: java selenium selenium-webdriver javamail

这是我的代码:

prop.load(new FileInputStream("config.properties"));
        String emailTo = prop.getProperty("To");
        String emailCC = prop.getProperty("CC");
        String emailBCC = prop.getProperty("BCC")

String[] to = emailTo.trim().split(",");
        String[] cc = emailCC.trim().split(",");
        String[] bcc = emailBCC.trim().split(",");

 --- Note: Value of CC and BCC is blank in properties file

config.properties

To = tarique.khan@test.com
CC =
BCC =

- 我试过了,我认为这是因为CC和BCC的空值,但是如何解决它。我不知道。

发生异常:

DEBUG: setDebug: JavaMail version 1.3.1
javax.mail.internet.AddressException: Illegal address in string ``''
    at javax.mail.internet.InternetAddress.<init>(InternetAddress.java:68)
    at com.neosoft.reporting.SendEmail.sendMail(SendEmail.java:165)
    at com.neosoft.reporting.SendEmail.execute(SendEmail.java:60)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
带有参数的

sendMail()声明:

如果属性文件中CC和BCC的值为空,那么我应该在方法中传递什么。

SendEmail.sendMail("testrobot.personiphi@gmail.com", "xxxx", "smtp.gmail.com",
                            "465", "true", "true", true, 
                            javax.net.ssl.SSLSocketFactory.class.getCanonicalName(),
                            "false", to, cc, bcc, "Automation Test Report",
                            "Hi \n Here is your test report of current run that was initiated.",
                            path, reportFileName);
}

public  static boolean sendMail(String userName, String passWord, String host, String port,
                String starttls, String auth, boolean debug, String socketFactoryClass,
                String fallback, String[] to, String[] cc, String[] bcc,
                String subject, String text, String attachmentPath, String attachmentName)

3 个答案:

答案 0 :(得分:2)

首先抓取CC&amp;来自BCC文件的config.properties

 String emailTo = prop.getProperty("To");
 String emailCC = prop.getProperty("CC");
 String emailBCC = prop.getProperty("BCC")

然后检查他们是否NULL喜欢

String[] cc;
String[] bcc;
if(emailCC.length() != 0){
    cc = emailCC.trim().split(",");
}else if(emailBCC.length() != 0){
    bcc = emailBCC.trim().split(",");
}

cc&amp; bccnull,然后在sendMail()中添加以下条件:

for(int i = 0; i < cc.length; i++) {
    if(!cc[i].isEmpty())
        message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i]));
}
for(int i = 0; i < bcc.length; i++) {
    if(!bcc[i].isEmpty())
        msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[i]));
}

我希望这能解决你的问题。

答案 1 :(得分:0)

从属性文件中获取值时,如果没有任何内容,它总是发送CC的NULL值,并且String []存储在0索引中。 msg.addRecipient添加了相同的NULL值,并且它抛出异常,所以我只是在for循环中添加了条件。它解决了问题。

for(int i=0;i<cc.length;i++){                   
    if(cc[i].equals("")){
        continue;   
    } else {
        msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i]));
    }       
}

答案 2 :(得分:-2)

检查CC和BCC的属性文件,我猜这些不是空白。