Java邮件发件人无法正常工作

时间:2014-03-06 15:47:39

标签: java javamail

我遇到的问题是,当我致电multipart.addBodyPart(msgbodypart);时,我应该在msgbodypart和{{1}处将MimeBodyPart的类型更改为BodyPart msgbodypart = new MimeBodyPart(); }

好的,我改变它,然后它会发出警告:

  

Multipart类型中的方法addBodyPart(BodyPart)不适用于参数(MimeBodyPart)

现在怎么办?

msgbodypart = new MimeBodyPart();

2 个答案:

答案 0 :(得分:0)

使用较旧的lib。 JavaMail API 1.4具有此选项

答案 1 :(得分:0)

final Session connection = Session.getInstance(props, null);
final Message message = new MimeMessage(connection);
final BodyPart bodyPart1 = new MimeBodyPart();
final BodyPart bodyPart2 = new MimeBodyPart();
final Multipart multiPart = new MimeMultipart();
connection.setDebug(true);
try {
    final Address toAddress = new InternetAddress(
    "RECEIVER-EMAIL-ADDRESS");
    final Address fromAddress = new InternetAddress(senderUserName);
    final String content = "Please check your attachments for a zip file";

    message.setSubject("Test Java Sending Zip File");

    bodyPart1.setText(content);
    DataSource dataSource = new FileDataSource("D:\\Data.zip");
    bodyPart2.setDataHandler(new DataHandler(dataSource));
    bodyPart2.setFileName(dataSource.getName());

    multiPart.addBodyPart(bodyPart1);
    multiPart.addBodyPart(bodyPart2);
    message.setContent(multiPart);

    message.setFrom(fromAddress);
    message.setRecipient(javax.mail.Message.RecipientType.TO, toAddress);
    Transport transport = connection.getTransport("smtp");
    transport.connect(smtpServer, senderUserName, senderPassword);
    transport.sendMessage(message, message.getAllRecipients());
    System.out.println("DONE !");
} catch (AddressException e) {
    e.printStackTrace();
    throw new Exception(e);
}