这是我的代码:
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 = [email protected]
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("[email protected]", "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)
最佳答案
首先像您一样从CC
文件中获取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
和bcc
是null
时,请在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]));
}
我希望这能解决您的问题。