在我的场景中,我使用jasper报告生成了一个pdf。我已经发送邮件给我的上司并附上pdf但邮件发送过程需要一分多钟。那么我用一秒钟发送邮件的方法,你可以使用javamail api帮助编写任何编码格式。在这里,我已经为您提供了输入邮件发送编码结构。感谢
public boolean sendMailForVacation(String subject, String bodyContent,String emailAddress,byte[] pdfFile,String pdfFileName,String company){
boolean isMailsent=false;
final String SMTP_HOST= getText("email.smtp.host");
final String SOCKET_FACTORY_PORT= getText("email.socket.factory.port");
final String SMTP=getText("email.smtp.port");
final String MAIL_USER_EMAIL_ADDRESS=getText("email.username");
final String MAIL_USER_PASSWORD= getText("email.password");
final String EMAIL_FROMNAME= company;//getText("email.fromname");
try{
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST);
props.put("mail.smtp.socketFactory.port",SOCKET_FACTORY_PORT);
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", SMTP);
Authenticator auth = new SMTPAuthenticator(MAIL_USER_EMAIL_ADDRESS, MAIL_USER_PASSWORD);
Session session = Session.getInstance(props, auth);
Message message = new MimeMessage(session);
InternetAddress from = new InternetAddress(MAIL_USER_EMAIL_ADDRESS,EMAIL_FROMNAME);
message.setFrom(from);
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailAddress));
message.setSubject(subject);
MimeMultipart multipart = new MimeMultipart("related");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(bodyContent, "text/html");
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
ByteArrayDataSource baDS=new ByteArrayDataSource(pdfFile,"application/pdf");//application/vnd.ms-excel
messageBodyPart.setDataHandler(new DataHandler(baDS));
messageBodyPart.setFileName(pdfFileName);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
isMailsent=true;
}catch(Exception e){
LOGGER.error(e);
}
return isMailsent;
}