我一直在尝试使用 javamail api 发送电子邮件。来自 smtp 服务器 (smtp.live.com) 的调试显示 550 5.3.4 未采取请求的操作;要继续发送消息,请登录您的帐户。
它似乎可以很好地创建消息,但不允许它发送。任何想法为什么?
try
{
// Setup properties for e-mail server
Properties props = System.getProperties();
props.put("mail.smtp.host", mConfig.getEmailHost());
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
// Get a Session object
Session session = Session.getInstance(props, new MyAuthenticator());
session.setDebug(true);
Transport transport = session.getTransport("smtp");
// Create message
MimeMessage message = new MimeMessage(session);
// Add the to/from fields
message.setFrom(new InternetAddress(mFromAddr, mFromName));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(mToAddr));
if (mCCAddrs != null)
{
for (int i=0; i<mCCAddrs.length; i++)
message.addRecipient(Message.RecipientType.CC, new InternetAddress(mCCAddrs[i]));
}
// Add Subject
message.setSubject(mEmailSubject);
// Setup multipart message for including the attachment
Multipart multipart = new MimeMultipart();
// Create message body
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(mEmailBody);
multipart.addBodyPart(messageBodyPart);
if (mAttachmentName != null)
{
// Create message attachment
BodyPart messageAttachmentPart = new MimeBodyPart();
messageAttachmentPart.setDataHandler(new DataHandler(new ByteArrayDatasource(data)));
messageAttachmentPart.setFileName(mAttachmentName);
multipart.addBodyPart(messageAttachmentPart);
}
// Send message
message.setContent(multipart);
transport.connect(mConfig.getEmailHost(), mConfig.getEmailUser(), mConfig.getEmailPassword());
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch (Exception ex)
{
ex.printStackTrace();
throw new Exception("Failed to send e-mail: " + ex.getMessage());
}
`