gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:27:58 24 4
gpt4 key购买 nike

这是我的代码:

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)

最佳答案

首先像您一样从 config.properties 文件中获取 CCBCC

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

ccbccnull时,然后在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]));
}

希望这能解决您的问题。

关于java - 如何克服异常——在java中发送电子邮件时字符串 ``中的非法地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26461344/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com