问题描述
我一直在寻找如何添加pdf并将其通过邮件发送很长时间..但是所有代码对我都失败了,我终于尝试了这个,对我来说听起来很正确,但是仍然显示错误 这是我的文章:
{
"id": 1,"name": "Test","lines": []
}
在第一时间,它曾经向我显示有关主机的错误信息,但我更正了它们,现在这是显示的错误信息
public static void send(String to,Document document) {
String content = "dummy content"; //this will be the text of the email
String subject = "dummy subject"; //this will be the subject of the email
String receiver="aaa@yahoo.com";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.localhost","https://fr.yahoo.com/");
properties.put("mail.smtp.auth","true");
Session session = Session.getDefaultInstance(properties,new javax.mail.Authenticator() {
protected PasswordAuthentication getpasswordAuthentication() {
return new PasswordAuthentication(username,password);
}
});
//2) compose message
ByteArrayOutputStream outputStream = null;
try {
//construct the text body part
MimeBodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setText(content);
//Now write the PDF content to the output stream
outputStream = new ByteArrayOutputStream();
PdfWriter.getInstance(document,outputStream);
byte[] bytes = outputStream.toByteArray();
//construct the pdf body part
DataSource dataSource = new ByteArrayDataSource(bytes,"application/pdf");
MimeBodyPart pdfBodyPart = new MimeBodyPart();
pdfBodyPart.setDataHandler(new DataHandler(dataSource));
pdfBodyPart.setFileName("test.pdf");
//construct the mime multi part
MimeMultipart mimeMultipart = new MimeMultipart();
mimeMultipart.addBodyPart(textBodyPart);
mimeMultipart.addBodyPart(pdfBodyPart);
//create the sender/recipient addresses
InternetAddress iaSender = new InternetAddress(username);
InternetAddress iaRecipient = new InternetAddress(receiver);
//construct the mime message
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setSender(iaSender);
mimeMessage.setSubject(subject);
mimeMessage.setRecipient(Message.RecipientType.TO,iaRecipient);
mimeMessage.setContent(mimeMultipart);
//send off the email
Transport.send(mimeMessage);
System.out.println("sent from " + username
+ ",to " + to
+ "; server = " + ",port = ");
} catch (Exception ex) {
ex.printstacktrace();
} finally {
//clean off
if (null != outputStream) {
try {
outputStream.close();
outputStream = null;
} catch (Exception ex) {
}
}
}
}
谁能纠正我的代码请求
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)