私のシナリオはこれです:
私は Web アプリケーションを持っています。ユーザーはメッセージを書き、ファイルを添付してメールを送信します。
私は JavaMail を使用してこのようにメールを送信しますが、ファイルをメッセージに添付する際に問題があります (私のファイルはセッションにあります):
if (request.getSession().getAttribute("EMAIL_ATTACHMENT") != null) {
UploadFile file = (UploadFile) request.getSession().getAttribute("EMAIL_ATTACHMENT");
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setContent(text, "text/html;charset=UTF-8");
MimeMultipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
MyMailAttachmentDataSource fds = new MyMailAttachmentDataSource(file);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
mp.addBodyPart(mbp2);
msg.setContent(mp, "text/plain");
}
MyMailAttachmentDataSource のコードは次のとおりです。
public class MyMailAttachmentDataSource implements DataSource{
private UploadFile file;
public MyMailAttachmentDataSource(UploadFile file){
this.file=file;
}
@Override
public InputStream getInputStream() throws IOException {
return file.getInpuStream();
}
@Override
public OutputStream getOutputStream() throws IOException {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public String getContentType() {
return file.getContentType();
}
@Override
public String getName() {
return file.getFileName();
}
}
メールを送信しようとすると、この例外が発生します
java.io.IOException: "text/plain" DataContentHandler requires String object, was given object of type class javax.mail.internet.MimeMultipart
at com.sun.mail.handlers.text_plain.writeTo(text_plain.java:97)
at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:884)
at javax.activation.DataHandler.writeTo(DataHandler.java:317)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1089)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1527)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:321)
at admin.email.JavaMail.SendEmail(JavaMail.java:403)
at admin.email.MailSend.SendMail(MailSend.java:86)
msg.contentType を「text/html」に変更しようとしましたが、それでも上記の例外が発生します
"text/html" DataContentHandler requires String object, was given object of type class javax.mail.internet.MimeMultipart
このエラーの原因を知っている人はいますか?どうすれば修正できますか?