2012-09-04 48 views
0

因此,最初我編寫了一個營銷電子郵件應用程序,可將電子郵件發送給數千個收件人。我天真地做這樣的事情:從Apache HtmlEmail中刪除所有地址

for(all emails) 
{ 
    HtmlEmail email = new HtmlEmail(); 
    email.setBody(theHtml); 
    email.addTo(currentEmail); 
    email.send(); 
} 

問題與上面的是,這麼多的郵件之後,垃圾收集器必須踢,除去吃了一堆CPU的陳舊的HTML電子郵件的對象。現在我試圖做類似如下:

HtmlEmail email = new HtmlEmail(); 
email.setBody(theHtml); 

for(all emails) 
{ 
    //Option1: Use below line of code but need to remove the previous "current email"; that is, not send this to all the previous recipients AND the new one 
    //Line to remove previous email from HtmlEmail object 
    email.addTo(currentEmail, currentName); 
    //or option 2: email.setTo(new String[]{currentEmail}); 
} 

未註釋的方法的問題是,AddTo就不會刪除先前添加的電子郵件地址。我當然不想將相同的電子郵件發送給相同的收件人。非常非常有趣。所以,如果我採用這種方法,我需要一種刪除以前電子郵件的方法。我想這樣做

email.setTo(Arrays.asList(new String[]{})); 

這樣做的問題是,API規定,所有包含字符串必須是有效的電子郵件地址,否則會拋出異常。另一種選擇是每次使用email.setTo,但不能包含收件人名稱。任何人都可以提出一種方法來做上述之一?如果你需要澄清,我明白,這是有點難以言詞。提前致謝。

回答

1

這很醜陋,但似乎HtmlEmail暴露了getToAddresses()方法中的內部List。因此,email.getToAddresses()。clear()實際上會清除所有收件人,然後您可以再次使用addTo()。

但是這依賴於HtmlEmail類的內部,這絕對不是良好的編碼習慣。

+0

嘿,非常感謝你的發現。我看了一下,你確實是對的。奇怪的API肯定。 – thatidiotguy