我有問題,就是當我打電話multipart.addBodyPart(msgbodypart);
它說,我應該在BodyPart msgbodypart = new MimeBodyPart();
改變msgbodypart
類型MimeBodyPart
和msgbodypart = new MimeBodyPart();
Java郵件發送者無法正常工作
好吧,我改變它,那麼它滴警告:
在類型多部分的方法addBodyPart(BodyPart的)不適用於參數(MimeBodyPart的)
現在怎麼辦?
try {
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(username));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject("Test Java Sending Zip File");
msg.setText("This is a message from a java program");
BodyPart msgbodypart = new MimeBodyPart();
msgbodypart.setText("this is msg Body");
Multipart multipart = new MimeMultipart();
msgbodypart = new MimeBodyPart();
String filename = "zips.txt";
DataSource source = new FileDataSource(filename);
msgbodypart.setDataHandler(new DataHandler(source));
msg.setFileName(filename);
multipart.addBodyPart(msgbodypart);
msg.setContent(multipart);
System.out.println("Sending");
Transport.send(msg);
System.out.println("Sent!");
} catch (MessagingException mex) {
mex.printStackTrace();
}
您可能需要檢查您的導入。確保它們全部來自'javax.mail ...'包。 –