发邮件出现IOException和MessagingException如何解决,求大神们帮忙!
方法代码如下,跑出如图的异常
public void sendmail(Collection file) throws MailException { if (!notifyFlag) { return; }
this.props = System.getProperties(); this.props.put("mail.smtp.host", getHost()); this.props.put("mail.smtp.auth", "false"); this.props.put("mail.transport.protocol", "smtp"); if (this.bolSendPartial) { this.props.put("mail.smtp.sendpartial", "true"); }
Authenticator auth = new SMTPAuthenticator(this.account, this.password); this.session = Session.getDefaultInstance(this.props, auth);
this.session.setDebug(this.debug); this.msg = new MimeMessage(this.session); try { if ((this.from != null) && (!this.from.trim().equals(""))) { this.msg.setFrom(new InternetAddress(paserEmailAddr(this.from), paserEmailName(this.from), this.strCharSet)); } else this.msg.setFrom();
if ((this.to == null) || (this.to.size() == 0)) { throw new MailException(""); }
for (Iterator it = this.to.iterator(); it.hasNext(); ) { String toMail = (String)it.next(); InternetAddress[] address = { new InternetAddress(paserEmailAddr(toMail), paserEmailName(toMail), this.strCharSet) };
this.msg.addRecipients(Message.RecipientType.TO, address); } Iterator it; if ((this.cc != null) && (this.cc.size() != 0)) for (it = this.cc.iterator(); it.hasNext(); ) { String ccMail = (String)it.next(); InternetAddress[] address = { new InternetAddress(paserEmailAddr(ccMail), paserEmailName(ccMail), this.strCharSet) };
this.msg.addRecipients(Message.RecipientType.CC, address); } Iterator it; if ((this.bcc != null) && (this.bcc.size() != 0)) { for (it = this.bcc.iterator(); it.hasNext(); ) { InternetAddress[] address = { new InternetAddress((String)it.next()) };
this.msg.addRecipients(Message.RecipientType.BCC, address); } }
this.msg.setSubject(this.subject, this.strCharSet);
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setContent(this.content, this.strMailType + ";charset=" + this.strCharSet);
Multipart mp = new MimeMultipart(); mp.addBodyPart(mbp1); Iterator it; if (file != null) { for (it = file.iterator(); it.hasNext(); ) { MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(it.next().toString().trim());
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(MimeUtility.encodeText(fds.getName(), this.strCharSet, "B"));
mp.addBodyPart(mbp2); }
}
this.msg.setContent(mp);
if (this.sendDate == null) this.sendDate = new Date(); this.msg.setSentDate(this.sendDate);
Transport.send(this.msg, this.msg.getAllRecipients()); } catch (SendFailedException ex) { if ((ex.getCause() instanceof SMTPAddressFailedException)) { throw new MailException(ex.getCause().getMessage()); } throw new MailException(ex.getMessage()); } catch (AddressException ex) { throw new MailException("AddressException while setting the mail address " + ex.getMessage()); } catch (MessagingException ex) { throw new MailException("MessagingException while send the mail " + ex.getMessage()); } catch (UnsupportedEncodingException ex) { throw new MailException("UnsupportedEncodingException while send the mail " + ex.getMessage()); } }
服务器返回“550: Invalid User”、“550:local user only”或者“551 delivery not allowed to non-local recipient”。
如果出现在您使用普通方式发送邮件时,这是由于服务器对发件人地址进行检查,对于发件人地址不是由本服务器提供的邮件,不予已发送。
解决的办法是:打开foxmail“帐户属性”中对话框,在“个人信息”的“电子邮件地址”中填写SMTP服务器正确的邮件地址。如果该提示出现在您使用特快专递发送邮件时,则表明对方的邮件服务器不接收这种特快专递的投递方式,请针对这次发送改用普通的方式。