下面列出了怎么用javax.mail.util.ByteArrayDataSource的API类实例代码及写法,或者点击链接到github查看源代码。
private MimeMessage parseToMimeMessage(Email email) {
MimeMessage message = javaMailSender.createMimeMessage();
try {
MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
if(StringUtils.isBlank(email.getFrom())) {
messageHelper.setFrom(SendEmailAppJointConfiguration.DEFAULT_EMAIL_FROM().getValue());
} else {
messageHelper.setFrom(email.getFrom());
}
messageHelper.setSubject(email.getSubject());
messageHelper.setTo(email.getTo());
messageHelper.setCc(email.getCc());
messageHelper.setBcc(email.getBcc());
for(Attachment attachment: email.getAttachments()){
messageHelper.addAttachment(attachment.getName(), new ByteArrayDataSource(attachment.getBase64Str(), attachment.getMediaType()));
}
messageHelper.setText(email.getContent(), true);
} catch (Exception e) {
logger.error("Send mail failed", e);
}
return message;
}
/**
* Set the content for a part using the encoding assigned to the handler.
* @param part the part to assign.
* @param buf the formatted data.
* @param type the mime type or null, meaning text/plain.
* @throws MessagingException if there is a problem.
*/
private void setContent(MimePart part, CharSequence buf, String type) throws MessagingException {
final String charset = getEncodingName();
if (type != null && !"text/plain".equalsIgnoreCase(type)) {
type = contentWithEncoding(type, charset);
try {
DataSource source = new ByteArrayDataSource(buf.toString(), type);
part.setDataHandler(new DataHandler(source));
} catch (final IOException IOE) {
reportError(IOE.getMessage(), IOE, ErrorManager.FORMAT_FAILURE);
part.setText(buf.toString(), charset);
}
} else {
part.setText(buf.toString(), MimeUtility.mimeCharset(charset));
}
}
public void sendEmail( String msgBody, String subject ) {
Properties prop = new Properties( System.getProperties() );
prop.put( "mail.smtp.host", emailHost );
Session session = Session.getDefaultInstance( prop, null );
try {
Message msg = new MimeMessage( session );
msg.setFrom( new InternetAddress( "[email protected]", ags ) );
msg.addRecipient( Message.RecipientType.TO, new InternetAddress( mailingList ) );
msg.setSubject( getUpdatedSubject( subject ) );
msg.setDataHandler( new DataHandler( new ByteArrayDataSource( msgBody, "text/plain" ) ) );
javax.mail.Transport.send( msg );
} catch ( Exception ex ) {
log.error( ex.getMessage() );
}
}
public void sendInvoice(final Invoice invoice) throws EmailException, IOException {
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
writeAsPdf(invoice, out);
ByteArrayDataSource dataSource =
new ByteArrayDataSource(out.toByteArray(), "application/pdf");
String fileName = "invoice_" + invoice.getInvoiceNumber() + ".pdf";
MultiPartEmail email = new MultiPartEmail();
email.setAuthentication(smtpUsername, smtpPassword);
email.setHostName(smtpHostname);
email.setSmtpPort(smtpPort);
email.setFrom(smtpFrom);
email.addTo(invoice.getInvoicer().getEmail());
email.setSubject(smtpSubject);
email.setMsg(smtpMessage);
email.attach(dataSource, fileName, "Invoice");
email.send();
}
}
private void attachCSV(Multipart multipart, QrHolder[] attachmentData) throws Exception {
StringBuilder sb = new StringBuilder();
for (QrHolder qrHolder : attachmentData) {
sb.append(qrHolder.token)
.append(",")
.append(qrHolder.deviceId)
.append(",")
.append(qrHolder.dashId)
.append("\n");
}
MimeBodyPart attachmentsPart = new MimeBodyPart();
ByteArrayDataSource source = new ByteArrayDataSource(sb.toString(), "text/csv");
attachmentsPart.setDataHandler(new DataHandler(source));
attachmentsPart.setFileName("tokens.csv");
multipart.addBodyPart(attachmentsPart);
}
@Test
public void isMdnSentAutomaticallyShouldManageItsMimeType() throws Exception {
MimeMessage message = MimeMessageUtil.defaultMimeMessage();
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart scriptPart = new MimeBodyPart();
scriptPart.setDataHandler(
new DataHandler(
new ByteArrayDataSource(
"Disposition: MDN-sent-automatically",
"text/plain")
));
scriptPart.setHeader("Content-Type", "text/plain");
multipart.addBodyPart(scriptPart);
message.setContent(multipart);
message.saveChanges();
FakeMail fakeMail = FakeMail.builder()
.name("mail")
.sender("[email protected]")
.mimeMessage(message)
.build();
assertThat(new AutomaticallySentMailDetectorImpl().isMdnSentAutomatically(fakeMail)).isFalse();
}
public void echoData(Holder<String> text, Holder<DataHandler> data) {
try {
InputStream bis = null;
bis = data.value.getDataSource().getInputStream();
byte[] b = new byte[6];
bis.read(b, 0, 6);
String string = IOUtils.newStringFromBytes(b);
ByteArrayDataSource source =
new ByteArrayDataSource(("test" + string).getBytes(), "application/octet-stream");
data.value = new DataHandler(source);
} catch (IOException e) {
e.printStackTrace();
}
}
public void echoDataWithHeader(Holder<String> text,
Holder<DataHandler> data,
Holder<String> headerText) {
try {
InputStream bis = null;
bis = data.value.getDataSource().getInputStream();
byte[] b = new byte[6];
bis.read(b, 0, 6);
String string = IOUtils.newStringFromBytes(b);
ByteArrayDataSource source =
new ByteArrayDataSource(("test" + string).getBytes(), "application/octet-stream");
data.value = new DataHandler(source);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testSwa() throws Exception {
SwAService service = new SwAService();
SwAServiceInterface port = service.getSwAServiceHttpPort();
setAddress(port, "http://localhost:" + serverPort + "/swa");
Holder<String> textHolder = new Holder<>();
Holder<DataHandler> data = new Holder<>();
ByteArrayDataSource source = new ByteArrayDataSource("foobar".getBytes(), "application/octet-stream");
DataHandler handler = new DataHandler(source);
data.value = handler;
textHolder.value = "Hi";
port.echoData(textHolder, data);
InputStream bis = null;
bis = data.value.getDataSource().getInputStream();
byte[] b = new byte[10];
bis.read(b, 0, 10);
String string = IOUtils.newStringFromBytes(b);
assertEquals("testfoobar", string);
assertEquals("Hi", textHolder.value);
}
@Test
public void testSwaDataStruct() throws Exception {
SwAService service = new SwAService();
SwAServiceInterface port = service.getSwAServiceHttpPort();
setAddress(port, "http://localhost:" + serverPort + "/swa");
Holder<DataStruct> structHolder = new Holder<>();
ByteArrayDataSource source = new ByteArrayDataSource("foobar".getBytes(), "application/octet-stream");
DataHandler handler = new DataHandler(source);
DataStruct struct = new DataStruct();
struct.setDataRef(handler);
structHolder.value = struct;
port.echoDataRef(structHolder);
handler = structHolder.value.getDataRef();
InputStream bis = handler.getDataSource().getInputStream();
byte[] b = new byte[10];
bis.read(b, 0, 10);
String string = IOUtils.newStringFromBytes(b);
assertEquals("testfoobar", string);
bis.close();
}
public void echoData(Holder<String> text, Holder<DataHandler> data) {
try {
InputStream bis = null;
bis = data.value.getDataSource().getInputStream();
byte[] b = new byte[6];
bis.read(b, 0, 6);
String string = IOUtils.newStringFromBytes(b);
ByteArrayDataSource source =
new ByteArrayDataSource(("test" + string).getBytes(), "application/octet-stream");
data.value = new DataHandler(source);
} catch (IOException e) {
e.printStackTrace();
}
}
public void echoDataWithHeader(Holder<String> text,
Holder<DataHandler> data,
Holder<String> headerText) {
try {
InputStream bis = null;
bis = data.value.getDataSource().getInputStream();
byte[] b = new byte[6];
bis.read(b, 0, 6);
String string = IOUtils.newStringFromBytes(b);
ByteArrayDataSource source =
new ByteArrayDataSource(("test" + string).getBytes(), "application/octet-stream");
data.value = new DataHandler(source);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testBadSOAPEnvelopeNamespace() throws Exception {
soapMessage = TestUtil.createEmptySoapMessage(Soap12.getInstance(), chain);
InputStream in = getClass().getResourceAsStream("test-bad-env.xml");
assertNotNull(in);
ByteArrayDataSource bads = new ByteArrayDataSource(in, "test/xml");
soapMessage.setContent(InputStream.class, bads.getInputStream());
ReadHeadersInterceptor r = new ReadHeadersInterceptor(BusFactory.getDefaultBus());
try {
r.handleMessage(soapMessage);
fail("Did not throw exception");
} catch (SoapFault f) {
assertEquals(Soap11.getInstance().getVersionMismatch(), f.getFaultCode());
}
}
@Test
public void testBadSOAPEnvelopeName() throws Exception {
soapMessage = TestUtil.createEmptySoapMessage(Soap12.getInstance(), chain);
InputStream in = getClass().getResourceAsStream("test-bad-envname.xml");
assertNotNull(in);
ByteArrayDataSource bads = new ByteArrayDataSource(in, "test/xml");
soapMessage.setContent(InputStream.class, bads.getInputStream());
ReadHeadersInterceptor r = new ReadHeadersInterceptor(BusFactory.getDefaultBus());
try {
r.handleMessage(soapMessage);
fail("Did not throw exception");
} catch (SoapFault f) {
assertEquals(Soap11.getInstance().getSender(), f.getFaultCode());
}
}
/**
* 发送带附件的邮件
*
* @param sendMail 发件人邮箱
* @param sendMailPwd 发件人密码
* @param sendMailName 发件人昵称(可选)
* @param receiveMail 收件人邮箱
* @param receiveMailName 收件人昵称(可选)
* @param sendSMTPHost 发件人邮箱的 SMTP 服务器地址, 必须准确, 不同邮件服务器地址不同, 一般(只是一般, 绝非绝对)格式为: smtp.xxx.com
* @param title 邮件主题
* @param content 邮件正文
* @author Administrator
* @date 2017年12月13日 下午1:51:38
*/
public static void sendFileMail(String sendMail, String sendMailPwd, String sendMailName, String[] receiveMail, String receiveMailName, String sendSMTPHost, String title, String content,
InputStream is, String fileName, String port) {
Session session = createSSLSession(sendSMTPHost, port, sendMailName, sendMailPwd);
// 3. 创建一封邮件
MimeMessage message;
try {
message = createMimeMessage(session, sendMail, sendMailName, receiveMail, receiveMailName, title, content);
// 5. Content: 邮件正文(可以使用html标签)(内容有广告嫌疑,避免被邮件服务器误认为是滥发广告以至返回失败,请修改发送内容)
MimeMultipart mm = new MimeMultipart();
MimeBodyPart text = new MimeBodyPart();
text.setContent(content, "text/html;charset=UTF-8");
mm.addBodyPart(text);
if (null != is && is.available() > 0) {
MimeBodyPart attachment = new MimeBodyPart();
DataSource source = new ByteArrayDataSource(is, "application/msexcel");
// 将附件数据添加到"节点"
attachment.setDataHandler(new DataHandler(source));
// 设置附件的文件名(需要编码)
attachment.setFileName(MimeUtility.encodeText(fileName));
// 10. 设置文本和 附件 的关系(合成一个大的混合"节点" / Multipart )
// 如果有多个附件,可以创建多个多次添加
mm.addBodyPart(attachment);
}
message.setContent(mm);
message.saveChanges();
// 4. 根据 Session 获取邮件传输对象
Transport transport = session.getTransport("smtp");
transport.connect(sendSMTPHost, sendMail, sendMailPwd);
// // 6. 发送邮件, 发到所有的收件地址, message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
transport.sendMessage(message, message.getAllRecipients());
// 7. 关闭连接
} catch (Exception e) {
log.error("", e);
}
}
private static void addAttachment(MimeMessageHelper helper, EmailAttachment ea) {
try {
DataSource ds = new ByteArrayDataSource(ea.getHeadedContent(), ea.getMimeType());
helper.addAttachment(ea.getFilename() + ea.getExtension(), ds);
} catch (MessagingException | IOException e) {
log.warn("Could not add attachment to email!", e);
}
}
@Override
protected void doDeliver(Report.Type reportType, ExportType exportType, byte[] bytes) throws ReportDeliveryException {
try {
MimeBodyPart msgBodyPart = new MimeBodyPart();
msgBodyPart.setContent(String.format("Please the attached %s report.", exportType), "text/plain");
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
attachmentBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(bytes, exportType.getMimeType())));
attachmentBodyPart.setFileName(String.format("export.%s", exportType.getFileExtension()));
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(msgBodyPart);
multipart.addBodyPart(attachmentBodyPart);
Session session = Session.getDefaultInstance(prop);
Message message = new MimeMessage(session);
message.setFrom(fromAddress);
message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{toAddress});
message.setSubject(String.format("%s delivery", reportType.getDisplayName()));
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException ex) {
throw new ReportDeliveryException("Problem while delivering report export.", ex);
}
}
@Override
protected void doDeliver(Report.Type reportType, ExportType exportType, byte[] bytes) throws ReportDeliveryException {
try {
MimeBodyPart msgBodyPart = new MimeBodyPart();
msgBodyPart.setContent(String.format("Please the attached %s report.", exportType), "text/plain");
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
attachmentBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(bytes, exportType.getMimeType())));
attachmentBodyPart.setFileName(String.format("export.%s", exportType.getFileExtension()));
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(msgBodyPart);
multipart.addBodyPart(attachmentBodyPart);
Session session = Session.getDefaultInstance(prop);
Message message = new MimeMessage(session);
message.setFrom(fromAddress);
message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{toAddress});
message.setSubject(String.format("%s delivery", reportType.getDisplayName()));
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException ex) {
throw new ReportDeliveryException("Problem while delivering report export.", ex);
}
}
@Override
protected void doDeliver(Report.Type reportType, ExportType exportType, byte[] bytes) throws ReportDeliveryException {
try {
MimeBodyPart msgBodyPart = new MimeBodyPart();
msgBodyPart.setContent(String.format("Please the attached %s report.", exportType), "text/plain");
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
attachmentBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(bytes, exportType.getMimeType())));
attachmentBodyPart.setFileName(String.format("export.%s", exportType.getFileExtension()));
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(msgBodyPart);
multipart.addBodyPart(attachmentBodyPart);
Session session = Session.getDefaultInstance(prop);
Message message = new MimeMessage(session);
message.setFrom(fromAddress);
message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{toAddress});
message.setSubject(String.format("%s delivery", reportType.getDisplayName()));
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException ex) {
throw new ReportDeliveryException("Problem while delivering report export.", ex);
}
}
@Override
public void deliver(Report.Type reportType, ExportType exportType, byte[] bytes) throws ReportDeliveryException {
try {
MimeBodyPart msgBodyPart = new MimeBodyPart();
msgBodyPart.setContent(String.format("Please the attached %s report.", exportType), "text/plain");
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
attachmentBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(bytes, exportType.getMimeType())));
attachmentBodyPart.setFileName(String.format("export.%s", exportType.getFileExtension()));
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(msgBodyPart);
multipart.addBodyPart(attachmentBodyPart);
Session session = Session.getDefaultInstance(prop);
Message message = new MimeMessage(session);
message.setFrom(fromAddress);
message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{toAddress});
message.setSubject(String.format("%s delivery", reportType.getDisplayName()));
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException ex) {
throw new ReportDeliveryException("Problem while delivering report export.", ex);
}
}
@Override
protected void doDeliver(Report.Type reportType, ExportType exportType, byte[] bytes) throws ReportDeliveryException {
try {
MimeBodyPart msgBodyPart = new MimeBodyPart();
msgBodyPart.setContent(String.format("Please the attached %s report.", exportType), "text/plain");
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
attachmentBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(bytes, exportType.getMimeType())));
attachmentBodyPart.setFileName(String.format("export.%s", exportType.getFileExtension()));
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(msgBodyPart);
multipart.addBodyPart(attachmentBodyPart);
Session session = Session.getDefaultInstance(prop);
Message message = new MimeMessage(session);
message.setFrom(fromAddress);
message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{toAddress});
message.setSubject(String.format("%s delivery", reportType.getDisplayName()));
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException ex) {
throw new ReportDeliveryException("Problem while delivering report export.", ex);
}
}
@Override
protected void doDeliver(Report.Type reportType, ExportType exportType, byte[] bytes) throws ReportDeliveryException {
try {
MimeBodyPart msgBodyPart = new MimeBodyPart();
msgBodyPart.setContent(String.format("Please the attached %s report.", exportType), "text/plain");
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
attachmentBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(bytes, exportType.getMimeType())));
attachmentBodyPart.setFileName(String.format("export.%s", exportType.getFileExtension()));
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(msgBodyPart);
multipart.addBodyPart(attachmentBodyPart);
Session session = Session.getDefaultInstance(prop);
Message message = new MimeMessage(session);
message.setFrom(fromAddress);
message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{toAddress});
message.setSubject(String.format("%s delivery", reportType.getDisplayName()));
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException ex) {
throw new ReportDeliveryException("Problem while delivering report export.", ex);
}
}
@Override
protected void doDeliver(Report.Type reportType, ExportType exportType, byte[] bytes) throws ReportDeliveryException {
try {
MimeBodyPart msgBodyPart = new MimeBodyPart();
msgBodyPart.setContent(String.format("Please the attached %s report.", exportType), "text/plain");
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
attachmentBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(bytes, exportType.getMimeType())));
attachmentBodyPart.setFileName(String.format("export.%s", exportType.getFileExtension()));
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(msgBodyPart);
multipart.addBodyPart(attachmentBodyPart);
Session session = Session.getDefaultInstance(prop);
Message message = new MimeMessage(session);
message.setFrom(fromAddress);
message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{toAddress});
message.setSubject(String.format("%s delivery", reportType.getDisplayName()));
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException ex) {
throw new ReportDeliveryException("Problem while delivering report export.", ex);
}
}
@Override
protected void doDeliver(Report.Type reportType, ExportType exportType, byte[] bytes) throws ReportDeliveryException {
try {
MimeBodyPart msgBodyPart = new MimeBodyPart();
msgBodyPart.setContent(String.format("Please the attached %s report.", exportType), "text/plain");
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
attachmentBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(bytes, exportType.getMimeType())));
attachmentBodyPart.setFileName(String.format("export.%s", exportType.getFileExtension()));
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(msgBodyPart);
multipart.addBodyPart(attachmentBodyPart);
Session session = Session.getDefaultInstance(prop);
Message message = new MimeMessage(session);
message.setFrom(fromAddress);
message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{toAddress});
message.setSubject(String.format("%s delivery", reportType.getDisplayName()));
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException ex) {
throw new ReportDeliveryException("Problem while delivering report export.", ex);
}
}
@Override
protected void doDeliver(Report.Type reportType, ExportType exportType, byte[] bytes) throws ReportDeliveryException {
try {
MimeBodyPart msgBodyPart = new MimeBodyPart();
msgBodyPart.setContent(String.format("Please the attached %s report.", exportType), "text/plain");
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
attachmentBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(bytes, exportType.getMimeType())));
attachmentBodyPart.setFileName(String.format("export.%s", exportType.getFileExtension()));
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(msgBodyPart);
multipart.addBodyPart(attachmentBodyPart);
Session session = Session.getDefaultInstance(prop);
Message message = new MimeMessage(session);
message.setFrom(fromAddress);
message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{toAddress});
message.setSubject(String.format("%s delivery", reportType.getDisplayName()));
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException ex) {
throw new ReportDeliveryException("Problem while delivering report export.", ex);
}
}
@Override
protected void doDeliver(Report.Type reportType, ExportType exportType, byte[] bytes) throws ReportDeliveryException {
try {
MimeBodyPart msgBodyPart = new MimeBodyPart();
msgBodyPart.setContent(String.format("Please the attached %s report.", exportType), "text/plain");
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
attachmentBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(bytes, exportType.getMimeType())));
attachmentBodyPart.setFileName(String.format("export.%s", exportType.getFileExtension()));
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(msgBodyPart);
multipart.addBodyPart(attachmentBodyPart);
Session session = Session.getDefaultInstance(prop);
Message message = new MimeMessage(session);
message.setFrom(fromAddress);
message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{toAddress});
message.setSubject(String.format("%s delivery", reportType.getDisplayName()));
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException ex) {
throw new ReportDeliveryException("Problem while delivering report export.", ex);
}
}
@Override
protected void doDeliver(Report.Type reportType, ExportType exportType, byte[] bytes) throws ReportDeliveryException {
try {
MimeBodyPart msgBodyPart = new MimeBodyPart();
msgBodyPart.setContent(String.format("Please the attached %s report.", exportType), "text/plain");
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
attachmentBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(bytes, exportType.getMimeType())));
attachmentBodyPart.setFileName(String.format("export.%s", exportType.getFileExtension()));
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(msgBodyPart);
multipart.addBodyPart(attachmentBodyPart);
Session session = Session.getDefaultInstance(prop);
Message message = new MimeMessage(session);
message.setFrom(fromAddress);
message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{toAddress});
message.setSubject(String.format("%s delivery", reportType.getDisplayName()));
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException ex) {
throw new ReportDeliveryException("Problem while delivering report export.", ex);
}
}
@Override
protected void doDeliver(Report.Type reportType, ExportType exportType, byte[] bytes) throws ReportDeliveryException {
try {
MimeBodyPart msgBodyPart = new MimeBodyPart();
msgBodyPart.setContent(String.format("Please the attached %s report.", exportType), "text/plain");
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
attachmentBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(bytes, exportType.getMimeType())));
attachmentBodyPart.setFileName(String.format("export.%s", exportType.getFileExtension()));
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(msgBodyPart);
multipart.addBodyPart(attachmentBodyPart);
Session session = Session.getDefaultInstance(prop);
Message message = new MimeMessage(session);
message.setFrom(fromAddress);
message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{toAddress});
message.setSubject(String.format("%s delivery", reportType.getDisplayName()));
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException ex) {
throw new ReportDeliveryException("Problem while delivering report export.", ex);
}
}
@Override
protected void doDeliver(Report.Type reportType, ExportType exportType, byte[] bytes) throws ReportDeliveryException {
try {
MimeBodyPart msgBodyPart = new MimeBodyPart();
msgBodyPart.setContent(String.format("Please the attached %s report.", exportType), "text/plain");
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
attachmentBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(bytes, exportType.getMimeType())));
attachmentBodyPart.setFileName(String.format("export.%s", exportType.getFileExtension()));
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(msgBodyPart);
multipart.addBodyPart(attachmentBodyPart);
Session session = Session.getDefaultInstance(prop);
Message message = new MimeMessage(session);
message.setFrom(fromAddress);
message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{toAddress});
message.setSubject(String.format("%s delivery", reportType.getDisplayName()));
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException ex) {
throw new ReportDeliveryException("Problem while delivering report export.", ex);
}
}
@Override
protected void doDeliver(Report.Type reportType, ExportType exportType, byte[] bytes) throws ReportDeliveryException {
try {
MimeBodyPart msgBodyPart = new MimeBodyPart();
msgBodyPart.setContent(String.format("Please the attached %s report.", exportType), "text/plain");
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
attachmentBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(bytes, exportType.getMimeType())));
attachmentBodyPart.setFileName(String.format("export.%s", exportType.getFileExtension()));
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(msgBodyPart);
multipart.addBodyPart(attachmentBodyPart);
Session session = Session.getDefaultInstance(prop);
Message message = new MimeMessage(session);
message.setFrom(fromAddress);
message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{toAddress});
message.setSubject(String.format("%s delivery", reportType.getDisplayName()));
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException ex) {
throw new ReportDeliveryException("Problem while delivering report export.", ex);
}
}