List of usage examples for javax.mail Multipart addBodyPart
public synchronized void addBodyPart(BodyPart part) throws MessagingException
From source file:org.topazproject.ambra.email.impl.FreemarkerTemplateMailer.java
private Multipart createPartForMultipart(final String templateFilename, final Map<String, Object> context, final String multipartType, final String mimeType) throws IOException, MessagingException { final Multipart multipart = new MimeMultipart(multipartType); multipart.addBodyPart(createBodyPart(mimeType, templateFilename, context)); return multipart; }
From source file:com.silverpeas.mailinglist.service.job.TestMessageCheckerWithStubs.java
@Test public void testCheckNewMessages() throws MessagingException, IOException { org.jvnet.mock_javamail.Mailbox.clearAll(); MessageChecker messageChecker = getMessageChecker(); messageChecker.removeListener("componentId"); messageChecker.removeListener("thesimpsons@silverpeas.com"); messageChecker.removeListener("theflanders@silverpeas.com"); StubMessageListener mockListener1 = new StubMessageListener("thesimpsons@silverpeas.com"); StubMessageListener mockListener2 = new StubMessageListener("theflanders@silverpeas.com"); messageChecker.addMessageListener(mockListener1); messageChecker.addMessageListener(mockListener2); MimeMessage mail = new MimeMessage(messageChecker.getMailSession()); InternetAddress bart = new InternetAddress("bart.simpson@silverpeas.com"); InternetAddress theSimpsons = new InternetAddress("thesimpsons@silverpeas.com"); mail.addFrom(new InternetAddress[] { bart }); mail.addRecipient(RecipientType.TO, theSimpsons); mail.setSubject("Plain text Email test with attachment"); MimeBodyPart attachment = new MimeBodyPart( TestMessageCheckerWithStubs.class.getResourceAsStream("lemonde.html")); attachment.setDisposition(Part.INLINE); attachment.setFileName("lemonde.html"); MimeBodyPart body = new MimeBodyPart(); body.setText(textEmailContent);// w w w.j a v a2s . c o m Multipart multiPart = new MimeMultipart(); multiPart.addBodyPart(body); multiPart.addBodyPart(attachment); mail.setContent(multiPart); mail.setSentDate(new Date()); Date sentDate1 = new Date(mail.getSentDate().getTime()); Transport.send(mail); mail = new MimeMessage(messageChecker.getMailSession()); bart = new InternetAddress("bart.simpson@silverpeas.com"); theSimpsons = new InternetAddress("thesimpsons@silverpeas.com"); mail.addFrom(new InternetAddress[] { bart }); mail.addRecipient(RecipientType.TO, theSimpsons); mail.setSubject("Plain text Email test"); mail.setText(textEmailContent); mail.setSentDate(new Date()); Date sentDate2 = new Date(mail.getSentDate().getTime()); Transport.send(mail); //Unauthorized email mail = new MimeMessage(messageChecker.getMailSession()); bart = new InternetAddress("marge.simpson@silverpeas.com"); theSimpsons = new InternetAddress("thesimpsons@silverpeas.com"); mail.addFrom(new InternetAddress[] { bart }); mail.addRecipient(RecipientType.TO, theSimpsons); mail.setSubject("Plain text Email test"); mail.setText(textEmailContent); mail.setSentDate(new Date()); Transport.send(mail); assertThat(org.jvnet.mock_javamail.Mailbox.get("thesimpsons@silverpeas.com").size(), is(3)); messageChecker.checkNewMessages(new Date()); assertThat(mockListener2.getMessageEvent(), is(nullValue())); MessageEvent event = mockListener1.getMessageEvent(); assertThat(event, is(notNullValue())); assertThat(event.getMessages(), is(notNullValue())); assertThat(event.getMessages(), hasSize(2)); Message message = event.getMessages().get(0); assertThat(message.getSender(), is("bart.simpson@silverpeas.com")); assertThat(message.getTitle(), is("Plain text Email test with attachment")); assertThat(message.getBody(), is(textEmailContent)); assertThat(message.getSummary(), is(textEmailContent.substring(0, 200))); assertThat(message.getSentDate().getTime(), is(sentDate1.getTime())); assertThat(message.getAttachmentsSize(), greaterThan(0L)); assertThat(message.getAttachments(), hasSize(1)); String path = MessageFormat.format(theSimpsonsAttachmentPath, new Object[] { messageChecker.getMailProcessor().replaceSpecialChars(message.getMessageId()) }); Attachment attached = message.getAttachments().iterator().next(); assertThat(attached.getPath(), is(path)); assertThat(message.getComponentId(), is("thesimpsons@silverpeas.com")); message = event.getMessages().get(1); assertThat(message.getSender(), is("bart.simpson@silverpeas.com")); assertThat(message.getTitle(), is("Plain text Email test")); assertThat(message.getBody(), is(textEmailContent)); assertThat(message.getSummary(), is(textEmailContent.substring(0, 200))); assertThat(message.getAttachmentsSize(), is(0L)); assertThat(message.getAttachments(), hasSize(0)); assertThat(message.getComponentId(), is("thesimpsons@silverpeas.com")); assertThat(message.getSentDate().getTime(), is(sentDate2.getTime())); }
From source file:eagle.common.email.EagleMailClient.java
private boolean _send(String from, String to, String cc, String title, String content, List<MimeBodyPart> attachments) { MimeMessage mail = new MimeMessage(session); try {/*from www .j a va 2 s . c om*/ mail.setFrom(new InternetAddress(from)); mail.setSubject(title); if (to != null) { mail.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); } if (cc != null) { mail.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc)); } //mail.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(DEFAULT_BCC_ADDRESS)); MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeBodyPart.setContent(content, "text/html;charset=utf-8"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(mimeBodyPart); for (MimeBodyPart attachment : attachments) { multipart.addBodyPart(attachment); } mail.setContent(multipart); // mail.setContent(content, "text/html;charset=utf-8"); LOG.info(String.format("Going to send mail: from[%s], to[%s], cc[%s], title[%s]", from, to, cc, title)); Transport.send(mail); return true; } catch (AddressException e) { LOG.info("Send mail failed, got an AddressException: " + e.getMessage(), e); return false; } catch (MessagingException e) { LOG.info("Send mail failed, got an AddressException: " + e.getMessage(), e); return false; } }
From source file:uk.ac.cam.cl.dtg.util.Mailer.java
/** * Utility method to allow us to send multipart messages using HTML and plain text. * * //from w w w . j a v a 2s . c o m * @param recipient * - string array of recipients that the message should be sent to * @param from * - the e-mail address that should be used as the sending address * @param replyTo * - (nullable) the e-mail address that should be used as the reply-to address * @param replyToName * - (nullable) the name that should be used as the reply-to name * @param subject * - The message subject * @param plainText * - The message body * @param html * - The message body in html * @throws MessagingException * - if we cannot send the message for some reason. * @throws AddressException * - if the address is not valid. */ public void sendMultiPartMail(final String[] recipient, final String from, @Nullable final String replyTo, @Nullable final String replyToName, final String subject, final String plainText, final String html) throws MessagingException, AddressException, UnsupportedEncodingException { Message msg = this.setupMessage(recipient, from, replyTo, replyToName, subject); // Create the text part MimeBodyPart textPart = new MimeBodyPart(); textPart.setText(plainText, "utf-8"); MimeBodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(html, "text/html; charset=utf-8"); Multipart multiPart = new MimeMultipart("alternative"); multiPart.addBodyPart(textPart); multiPart.addBodyPart(htmlPart); msg.setContent(multiPart); Transport.send(msg); }
From source file:app.logica.gestores.GestorEmail.java
/** * Create a MimeMessage using the parameters provided. * * @param to//from w w w .ja va 2 s.c om * Email address of the receiver. * @param from * Email address of the sender, the mailbox account. * @param subject * Subject of the email. * @param bodyText * Body text of the email. * @param file * Path to the file to be attached. * @return MimeMessage to be used to send email. * @throws MessagingException */ private MimeMessage createEmailWithAttachment(String to, String from, String subject, String bodyText, File file) throws MessagingException, IOException { Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); MimeMessage email = new MimeMessage(session); email.setFrom(new InternetAddress(from)); email.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to)); email.setSubject(subject); MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeBodyPart.setContent(bodyText, "text/plain"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(mimeBodyPart); mimeBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(file); mimeBodyPart.setDataHandler(new DataHandler(source)); mimeBodyPart.setFileName(file.getName()); multipart.addBodyPart(mimeBodyPart); email.setContent(multipart); return email; }
From source file:nz.co.testamation.common.mail.MultipartMessageFactoryImpl.java
@Override public Multipart create(Email email) throws MessagingException { Multipart multipart = isNotBlank(email.getHtmlBody()) ? new MimeMultipart("alternative") : new MimeMultipart(); MimeBodyPart textPart = new MimeBodyPart(); textPart.setText(StringUtil.toNotNullString(email.getTextBody())); multipart.addBodyPart(textPart); if (isNotBlank(email.getHtmlBody())) { MimeBodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(email.getHtmlBody(), "text/html"); multipart.addBodyPart(htmlPart); }/*from ww w . j av a 2s . c o m*/ if (email.getAttachments() != null) { for (EmailAttachment attachment : email.getAttachments()) { multipart.addBodyPart(attachmentBodyPartFactory.create(attachment)); } } return multipart; }
From source file:com.basicservice.service.MailService.java
public void sendEmail(String from, String to, String subject, String messageHtml) throws Exception { try {//from ww w . j ava2 s. c o m Properties props = new Properties(); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.host", smtpHost); props.put("mail.smtp.port", 587); props.put("mail.smtp.auth", "true"); Authenticator auth = new SMTPAuthenticator(); Session mailSession = Session.getDefaultInstance(props, auth); // mailSession.setDebug(true); Transport transport = mailSession.getTransport(); MimeMessage message = new MimeMessage(mailSession); Multipart multipart = new MimeMultipart("alternative"); BodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(new String(messageHtml.getBytes("UTF8"), "ISO-8859-1"), "text/html"); multipart.addBodyPart(htmlPart); message.setContent(multipart); message.setFrom(new InternetAddress(from)); message.setSubject(subject, "UTF-8"); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); transport.connect(); transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); transport.close(); } catch (Exception e) { LOG.debug("Exception while sending email: ", e); throw e; } }
From source file:com.threewks.thundr.mail.JavaMailMailer.java
private void addBody(Multipart multipart, String content, String contentType) throws MessagingException { BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setContent(content, contentType); multipart.addBodyPart(messageBodyPart); }
From source file:rescustomerservices.GmailQuickstart.java
/** * Create a MimeMessage using the parameters provided. * * @param to Email address of the receiver. * @param from Email address of the sender, the mailbox account. * @param subject Subject of the email./*from w w w . j a v a 2s . com*/ * @param bodyText Body text of the email. * @param fileDir Path to the directory containing attachment. * @param filename Name of file to be attached. * @return MimeMessage to be used to send email. * @throws MessagingException */ public MimeMessage createEmailWithAttachment(String to, String from, String subject, String bodyText, String fileDir, String filename) throws MessagingException, IOException { Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); MimeMessage email = new MimeMessage(session); InternetAddress tAddress = new InternetAddress(to); InternetAddress fAddress = new InternetAddress(from); email.setFrom(fAddress); email.addRecipient(javax.mail.Message.RecipientType.TO, tAddress); email.setSubject(subject); MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeBodyPart.setContent(bodyText, "text/plain"); mimeBodyPart.setHeader("Content-Type", "text/plain; charset=\"UTF-8\""); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(mimeBodyPart); mimeBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(fileDir + filename); mimeBodyPart.setDataHandler(new DataHandler(source)); mimeBodyPart.setFileName(filename); String contentType = Files.probeContentType(FileSystems.getDefault().getPath(fileDir, filename)); mimeBodyPart.setHeader("Content-Type", contentType + "; name=\"" + filename + "\""); mimeBodyPart.setHeader("Content-Transfer-Encoding", "base64"); multipart.addBodyPart(mimeBodyPart); email.setContent(multipart); return email; }
From source file:org.talend.dataprep.api.service.mail.MailFeedbackSender.java
@Override public void send(String subject, String body, String sender) { try {//from w w w .j a v a 2 s. c om final String recipientList = StringUtils.join((new HashSet<>(Arrays.asList(recipients))).toArray(), ','); subject = subjectPrefix + subject; body = bodyPrefix + "<br/>" + body + "<br/>" + bodySuffix; InternetAddress from = new InternetAddress(this.sender); InternetAddress replyTo = new InternetAddress(sender); Properties p = new Properties(); p.put("mail.smtp.host", smtpHost); p.put("mail.smtp.port", smtpPort); p.put("mail.smtp.starttls.enable", "true"); p.put("mail.smtp.auth", "true"); MailAuthenticator authenticator = new MailAuthenticator(userName, password); Session sendMailSession = Session.getInstance(p, authenticator); MimeMessage msg = new MimeMessage(sendMailSession); msg.setFrom(from); msg.setReplyTo(new Address[] { replyTo }); msg.addRecipients(Message.RecipientType.TO, recipientList); msg.setSubject(subject, "UTF-8"); msg.setSentDate(new Date()); Multipart mainPart = new MimeMultipart(); BodyPart html = new MimeBodyPart(); html.setContent(body, "text/html; charset=utf-8"); mainPart.addBodyPart(html); msg.setContent(mainPart); Transport.send(msg); LOGGER.debug("Sending mail:'{}' to '{}'", subject, recipients); } catch (Exception e) { throw new TDPException(APIErrorCodes.UNABLE_TO_SEND_MAIL, e); } }