2012-12-03 25 views
2

Grails世界的新手。我有問題將表單逗號分隔的表單值傳遞給grails郵件插件的'TO'屬性。無論我嘗試什麼,我都可以將它變成數組或接受多個電子郵件地址。Grail郵件插件 - 無法添加多個TO地址?

我得到以下錯誤;我已經刪除了域名,所以請忽略它們。

Could not parse mail; nested exception is javax.mail.internet.AddressException: 
Illegal address in string ``"[email protected]","[email protected]"'' 

現在抓頭,任何想法,我會去哪裏錯了下面的代碼。我完全被難住了!

in emailpublication GSP form I have。這些字段將自動從文檔的數據庫和標識中填充。

<g:form action="emailpublication"> 

<g:field type="text" name="whogetsemail" value="${publicationInstance?.portfolio?.emailtemplates?.toemailtemplate}" /> 
<g:field type="text" name="publicationName" value="${publicationInstance?.publicationName}" /> 
<g:textArea type="text" name="publicationContent" value="${publicationInstance?.publicationContent}" cols="20" rows="20"/> 
<g:hiddenField name="id" value="${publicationInstance?.id}" /> 

<input type="submit" value="Send Email"/> 
</g:form> 

在出版物控制器中我有以下內容;我需要提供給在該emailpublication GSP形式從whogetsemail字段中填充

def emailpublication(){ 

    List<String> recipients = request.getParameterValues("whogetsemail") 
    try { 
    sendMail{ 
      to (recipients.toArray()) 
      from "[email protected]" 
      subject params.publicationName 
      text params.emailbodyheader + "\n"+"\n" + params.publicationContent + "\n"+"\n" + params.footeremailtemplate 
     } 

    } 
    catch (MailException e) { 
     log.error "Failed to send emails: $e.message", e 
    } 
    catch (MessagingException e) { 
     log.error "Failed to send emails: $e.message", e 
    } 
    redirect(uri: "/publication/show/${params}") 
      flash.message = "${params.publicationName} sent to ${params.emailto}" 
} 

感謝所有一個或多個電子郵件地址,期待回覆,林沮喪

回答

4

該插件支持多個地址,他們可以在Object[]數組或List。但是,由於只有一個whogetsemail字段,因此您會得到一個逗號分隔的字符串。如果有多個具有相同名稱的輸入,則request.getParameterValues()僅返回多個值。

如果改成這樣它應該工作:

List<String> recipients = params.whogetsemail.split(',').collect { it.trim() } 

to recipients 
+0

伯特和凱利感謝你的幫助,完美地工作。 ++ – IanN

0

嘗試取出括號圍繞解決:

to recipients.toArray() 

我做的正是這一點的地方很多的...

+0

負幽靈騎士 - 仍然得到錯誤無法解析的郵件;嵌套的異常是javax.mail.internet.AddressException:字符串中的非法地址'''[email protected]「,」[email protected]「'' – IanN

+1

請參閱Burt的回答。實際上我剛剛注意到它是一個單獨的'whogetsemail'字段,而不是多個字段。但令人敬畏的Top Gun參考! – Kelly