Java 当其中一个地址无效时,Transport.sendMessage为什么会失败?
我用以下代码发送邮件Java 当其中一个地址无效时,Transport.sendMessage为什么会失败?,java,email,Java,Email,我用以下代码发送邮件 public void send() throws MessagingException { // create some properties and get the Session Properties props = new Properties(); props.put("mail.smtp.host", this.getSMTPServer()); if( this.getDebugMode
public void send() throws MessagingException
{
// create some properties and get the Session
Properties props = new Properties();
props.put("mail.smtp.host", this.getSMTPServer());
if( this.getDebugMode() )
props.put("mail.debug", "true");
else
props.put("mail.debug", "false");
//Comment by Sandip for FIRSTMEDIA-578
//props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props, null);
MimeMessage mail = new MimeMessage(session);
//Set Mailer string in Header..
mail.setHeader("X-Mailer", this.getMailer() );
//Set TO Recipients, toList would be of comma separated if multiple addresses are there
if( this.getTo() != null )
{
StringTokenizer st = new StringTokenizer(this.getTo(),",");
InternetAddress[] recList = new InternetAddress[st.countTokens()];
for (int r = 0; st.hasMoreTokens(); r++)
recList[r] = new InternetAddress(st.nextToken().trim());
if(recList.length != 0 )
{
mail.setRecipients(Message.RecipientType.TO, recList);
}
}
//Set CC Recipients, bccList would be of comma separated if multiple addresses are there
if( this.getCc() != null )
{
StringTokenizer st1 = new StringTokenizer(this.getCc(),",");
InternetAddress[] copyList = new InternetAddress[st1.countTokens()];
for (int c = 0; st1.hasMoreTokens(); c++)
copyList[c] = new InternetAddress(st1.nextToken().trim());
if(copyList.length != 0 )
mail.setRecipients(Message.RecipientType.CC, copyList);
}
//Set BCC Recipients, bccList would be of comma separated if multiple addresses are there
if( this.getBcc() != null )
{
StringTokenizer st2 = new StringTokenizer(this.getBcc(),",");
InternetAddress[] bcopyList = new InternetAddress[st2.countTokens()];
for (int c = 0; st2.hasMoreTokens(); c++)
bcopyList[c] = new InternetAddress(st2.nextToken().trim());
if(bcopyList.length != 0 )
mail.setRecipients(Message.RecipientType.BCC, bcopyList);
}
// Create a mime message
mail.setFrom(new InternetAddress(this.getFrom()));
mail.setSubject(subject);
//create mulitple parts to added
Multipart mp = new MimeMultipart();
MimeBodyPart mbp1 = new MimeBodyPart();
//messageMIME can be "text/plain" or "text/html" or anything related to mime
mbp1.setContent(this.getMsgText(),this.getContentType());
mp.addBodyPart(mbp1);
//Adding attachments to mail
if( this.attachmentList != null )
{
for(int i=0; i < this.attachmentList.size() ; i++ )
{
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds = new FileDataSource((File)this.attachmentList.elementAt(i));
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
}
}
mail.setContent(mp);
mail.saveChanges();
mail.setSentDate(new Date());
// Send the message
Transport trans = session.getTransport("smtp");
trans.connect(this.getSMTPServer(), this.getSMTPUsername(), this.getSMTPPassword());
trans.sendMessage(mail, mail.getAllRecipients());
trans.close();
}
public void send()抛出MessaginException
{
//创建一些属性并获取会话
Properties props=新属性();
put(“mail.smtp.host”,this.getSMTPServer());
if(this.getDebugMode())
props.put(“mail.debug”,“true”);
其他的
props.put(“mail.debug”、“false”);
//Sandip对FIRSTMEDIA-578的评论
//props.put(“mail.smtp.auth”,“true”);
Session Session=Session.getInstance(props,null);
mimessage mail=新mimessage(会话);
//在标头中设置邮件程序字符串。。
setHeader(“X-Mailer”,this.getMailer());
//设置为Recipients时,如果有多个地址,则toList将使用逗号分隔
if(this.getTo()!=null)
{
StringTokenizer st=新的StringTokenizer(this.getTo(),“,”);
InternetAddress[]recList=新的InternetAddress[st.countTokens()];
for(int r=0;st.hasMoreTokens();r++)
recList[r]=新的Internet地址(st.nextToken().trim());
如果(recList.length!=0)
{
mail.setRecipients(Message.RecipientType.TO,recList);
}
}
//设置抄送收件人,如果有多个地址,则bccList将使用逗号分隔
if(this.getCc()!=null)
{
StringTokenizer st1=新的StringTokenizer(this.getCc(),“,”);
InternetAddress[]copyList=新的InternetAddress[st1.countTokens()];
for(int c=0;st1.hasMoreTokens();c++)
copyList[c]=新的InternetAddress(st1.nextToken().trim());
如果(copyList.length!=0)
mail.setRecipients(Message.RecipientType.CC,copyrist);
}
//设置密件抄送收件人,如果存在多个地址,则密件抄送收件人将使用逗号分隔
if(this.getBcc()!=null)
{
StringTokenizer st2=新的StringTokenizer(this.getBcc(),“,”);
InternetAddress[]bcopyList=新的InternetAddress[st2.countTokens()];
for(int c=0;st2.hasMoreTokens();c++)
bcopyList[c]=新的InternetAddress(st2.nextToken().trim());
if(bcopyList.length!=0)
mail.setRecipients(Message.RecipientType.BCC,bcopyList);
}
//创建mime消息
mail.setFrom(新的InternetAddress(this.getFrom());
mail.setSubject(主题);
//创建要添加的多个零件
Multipart mp=新的MimeMultipart();
MimeBodyPart mbp1=新的MimeBodyPart();
//messageMIME可以是“text/plain”或“text/html”或任何与mime相关的内容
mbp1.setContent(this.getMsgText(),this.getContentType());
mp.addBodyPart(mbp1);
//向邮件添加附件
if(this.attachmentList!=null)
{
对于(int i=0;i
当邮件发送到收件人列表中的一个收件人失败时,所有收件人邮件发送失败
向列表中所有正确的邮件收件人发送邮件是否需要任何更改?如果选中“发送”消息,则会显示:
将消息发送到指定的地址列表。指示传递状态的适当TransportEvent将传递给在此传输上注册的任何TransportListener。此外,如果任何地址无效,将引发SendFailedException。消息是否仍成功发送到任何有效地址取决于运输实施。”
您可以尝试:
- 在你的代码中通过一个或多个发送它
- 您可以捕获异常
并检查SendFailedException
发送到具有 没有被发送GetValidSentAddresses
检查收件人地址是否无效?你们能提供一个失败的例子吗?不是所有的列表,而是列表中的一个是无效的