簡體   English   中英

如何克服異常-在Java中發送電子郵件時字符串``'中的非法地址

[英]How to overcome exception — Illegal address in string ``' while sending email in java'

這是我的代碼:

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]));
}

我希望這能解決您的問題。

從屬性文件中獲取值時,如果不存在任何值且String []存儲在0索引中,則始終發送CC的NULL值。 msg.addRecipient添加了相同的NULL值,並拋出異常,因此我只在if循環中添加了condition。 解決了問題。

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

檢查CC和BCC的屬性文件,我想這些不是空白。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM