List of usage examples for javax.mail.internet MimeMessage getFrom
@Override public Address[] getFrom() throws MessagingException
From source file:com.meltmedia.cadmium.email.TestingMessageTransformer.java
public MimeBodyPart newSentToBodyPart(MimeMessage message) throws MessagingException { // get information about the original message. Address[] originalFromRecipient = message.getFrom(); Address[] originalToRecipient = message.getRecipients(Message.RecipientType.TO); Address[] originalCcRecipient = message.getRecipients(Message.RecipientType.CC); Address[] originalBccRecipient = message.getRecipients(Message.RecipientType.BCC); String originalSubject = message.getSubject(); // create the html for the string buffer. StringBuffer html = new StringBuffer(); html.append("<html><body><table style=\"width: 100%;\"><tr><td>header</td><td>value</td></tr>"); html.append("<tr><td>Subject:</td><td>").append(escape(originalSubject)).append("</td></tr>"); // iterate over the addresses. if (originalFromRecipient != null) { for (int i = 0; i < originalFromRecipient.length; i++) { html.append("<tr><td>FROM:</td><td>").append(escape(originalFromRecipient[i])).append("</td></tr>"); }/* w ww . java 2 s .co m*/ } if (originalToRecipient != null) { for (int i = 0; i < originalToRecipient.length; i++) { html.append("<tr><td>TO:</td><td>").append(escape(originalToRecipient[i])).append("</td></tr>"); } } if (originalCcRecipient != null) { for (int i = 0; i < originalCcRecipient.length; i++) { html.append("<tr><td>CC:</td><td>").append(escape(originalCcRecipient[i])).append("</td></tr>"); } } if (originalBccRecipient != null) { for (int i = 0; i < originalBccRecipient.length; i++) { html.append("<tr><td>BCC:</td><td>").append(escape(originalBccRecipient[i])).append("</td></tr>"); } } html.append("</table></body></html>"); MimeBodyPart sentToBodyPart = new MimeBodyPart(); sentToBodyPart.setContent(html.toString(), "text/html"); sentToBodyPart.setHeader("Content-ID", "original-addresses"); sentToBodyPart.setDisposition("inline"); return sentToBodyPart; }
From source file:org.silverpeas.components.mailinglist.service.job.MailProcessor.java
/** * Process an email, extracting attachments and constructing a Message. * @param mail the email to be processed. * @param mailingList the mailing list it is going to be affected to. * @param event the event which will be send at the end of all processing. * @throws MessagingException//ww w. j a v a 2 s. co m * @throws IOException */ public void prepareMessage(MimeMessage mail, MessageListener mailingList, MessageEvent event) throws MessagingException, IOException { String sender = ((InternetAddress[]) mail.getFrom())[0].getAddress(); if (!mailingList.checkSender(sender)) { return; } Message message = new Message(); message.setComponentId(mailingList.getComponentId()); message.setSender(sender); message.setSentDate(mail.getSentDate()); message.setMessageId(mail.getMessageID()); String[] referenceId = mail.getHeader(MAIL_HEADER_IN_REPLY_TO); if (referenceId == null || referenceId.length == 0) { referenceId = mail.getHeader(MAIL_HEADER_REFERENCES); } if (referenceId == null || referenceId.length == 0) { message.setReferenceId(null); } else { message.setReferenceId(referenceId[0]); } message.setTitle(mail.getSubject()); Object content = mail.getContent(); if (content instanceof Multipart) { processMultipart((Multipart) content, message); } else if (content instanceof String) { processBody((String) content, mail.getContentType(), message); } event.addMessage(message); }
From source file:com.silverpeas.mailinglist.service.job.MailProcessor.java
/** * Process an email, extracting attachments and constructing a Message. * * @param mail the email to be processed. * @param mailingList the mailing list it is going to be affected to. * @param event the event which will be send at the end of all processing. * @throws MessagingException/* w w w . jav a 2 s . co m*/ * @throws IOException */ public void prepareMessage(MimeMessage mail, MessageListener mailingList, MessageEvent event) throws MessagingException, IOException { String sender = ((InternetAddress[]) mail.getFrom())[0].getAddress(); if (!mailingList.checkSender(sender)) { return; } Message message = new Message(); message.setComponentId(mailingList.getComponentId()); message.setSender(sender); message.setSentDate(mail.getSentDate()); message.setMessageId(mail.getMessageID()); String[] referenceId = mail.getHeader(MAIL_HEADER_IN_REPLY_TO); if (referenceId == null || referenceId.length == 0) { referenceId = mail.getHeader(MAIL_HEADER_REFERENCES); } if (referenceId == null || referenceId.length == 0) { message.setReferenceId(null); } else { message.setReferenceId(referenceId[0]); } message.setTitle(mail.getSubject()); SilverTrace.info("mailingList", "MailProcessor.prepareMessage()", "mailinglist.notification.error", "Processing message " + mail.getSubject()); Object content = mail.getContent(); if (content instanceof Multipart) { processMultipart((Multipart) content, message); } else if (content instanceof String) { processBody((String) content, mail.getContentType(), message); } event.addMessage(message); }
From source file:com.joshlong.lazyblogger.integrations.EmailToBlogTransformer.java
public Message<?> transformFromMimeMessageToBlogPost(Message<?> msg) { Message<?> response = null; // if this is null, the message will just get dropped. BlogPost blogPost = new BlogPost(); try {/* w ww . ja va2 s . c o m*/ MimeMessage mimeMessage = (MimeMessage) msg.getPayload(); blogPost.setSubject(mimeMessage.getSubject()); blogPost.setDate(mimeMessage.getSentDate()); blogPost.setBody(IOUtils.toString(mimeMessage.getInputStream())); if (mimeMessage.getSender() != null) blogPost.addSender(emailFromAddress(mimeMessage.getSender())); if (mimeMessage.getFrom() != null) for (Address address : mimeMessage.getFrom()) blogPost.addSender(emailFromAddress(address)); response = MessageBuilder.withPayload(blogPost).copyHeadersIfAbsent(msg.getHeaders()).build(); } catch (Throwable e) { utilities.log(ExceptionUtils.getFullStackTrace(e)); } return response; }
From source file:org.drools.task.service.TaskServiceDeadlinesBaseTest.java
public void testDelayedEmailNotificationOnDeadline() throws Exception { Map vars = new HashedMap(); vars.put("users", users); vars.put("groups", groups); vars.put("now", new Date()); DefaultEscalatedDeadlineHandler notificationHandler = new DefaultEscalatedDeadlineHandler(getConf()); WorkItemManager manager = new DefaultWorkItemManager(null); notificationHandler.setManager(manager); MockUserInfo userInfo = new MockUserInfo(); userInfo.getEmails().put(users.get("tony"), "tony@domain.com"); userInfo.getEmails().put(users.get("darth"), "darth@domain.com"); userInfo.getLanguages().put(users.get("tony"), "en-UK"); userInfo.getLanguages().put(users.get("darth"), "en-UK"); notificationHandler.setUserInfo(userInfo); taskService.setEscalatedDeadlineHandler(notificationHandler); String string = toString(/*from www .j av a 2 s. co m*/ new InputStreamReader(getClass().getResourceAsStream("../DeadlineWithNotification.mvel"))); BlockingAddTaskResponseHandler addTaskResponseHandler = new BlockingAddTaskResponseHandler(); Task task = (Task) eval(new StringReader(string), vars); client.addTask(task, null, addTaskResponseHandler); long taskId = addTaskResponseHandler.getTaskId(); Content content = new Content(); content.setContent("['subject' : 'My Subject', 'body' : 'My Body']".getBytes()); BlockingSetContentResponseHandler setContentResponseHandler = new BlockingSetContentResponseHandler(); client.setDocumentContent(taskId, content, setContentResponseHandler); long contentId = setContentResponseHandler.getContentId(); BlockingGetContentResponseHandler getResponseHandler = new BlockingGetContentResponseHandler(); client.getContent(contentId, getResponseHandler); content = getResponseHandler.getContent(); assertEquals("['subject' : 'My Subject', 'body' : 'My Body']", new String(content.getContent())); // emails should not be set yet assertEquals(0, getWiser().getMessages().size()); Thread.sleep(100); // nor yet assertEquals(0, getWiser().getMessages().size()); long time = 0; while (getWiser().getMessages().size() != 2 && time < 15000) { Thread.sleep(500); time += 500; } // 1 email with two recipients should now exist assertEquals(2, getWiser().getMessages().size()); List<String> list = new ArrayList<String>(2); list.add(getWiser().getMessages().get(0).getEnvelopeReceiver()); list.add(getWiser().getMessages().get(1).getEnvelopeReceiver()); assertTrue(list.contains("tony@domain.com")); assertTrue(list.contains("darth@domain.com")); MimeMessage msg = ((WiserMessage) getWiser().getMessages().get(0)).getMimeMessage(); assertEquals("My Body", msg.getContent()); assertEquals("My Subject", msg.getSubject()); assertEquals("from@domain.com", ((InternetAddress) msg.getFrom()[0]).getAddress()); assertEquals("replyTo@domain.com", ((InternetAddress) msg.getReplyTo()[0]).getAddress()); assertEquals("tony@domain.com", ((InternetAddress) msg.getRecipients(RecipientType.TO)[0]).getAddress()); assertEquals("darth@domain.com", ((InternetAddress) msg.getRecipients(RecipientType.TO)[1]).getAddress()); }
From source file:org.nuxeo.ecm.core.convert.plugins.text.extractors.RFC822ToTextConverter.java
protected Blob extractTextFromMessage(Blob blob) { if (blob == null) { return null; }/*from www . ja va 2 s . co m*/ File f = null; OutputStream fo = null; try { MimeMessage msg = new MimeMessage((Session) null, blob.getStream()); f = File.createTempFile("rfc822totext", ".txt"); fo = new FileOutputStream(f); List<Part> parts = getAttachmentParts(msg); writeInfo(fo, msg.getSubject()); writeInfo(fo, msg.getFrom()); writeInfo(fo, msg.getRecipients(RecipientType.TO)); writeInfo(fo, msg.getRecipients(RecipientType.CC)); for (Part part : parts) { writeInfo(fo, part.getFileName()); writeInfo(fo, part.getDescription()); byte[] extracted = extractTextFromMessagePart(part); if (extracted != null) { writeInfo(fo, extracted); } } Blob outblob = new FileBlob(new FileInputStream(f)); outblob.setMimeType(descriptor.getDestinationMimeType()); return outblob; } catch (Exception e) { log.error(e); } finally { if (fo != null) { try { fo.close(); } catch (IOException e) { log.error(e); } } if (f != null) { f.delete(); } } return null; }
From source file:mitm.common.mail.repository.hibernate.MailRepositoryItemEntity.java
private void initFrom(MimeMessage message) { try {//from w ww . j ava 2 s .c om String decodedFrom = null; try { decodedFrom = StringUtils.join( EmailAddressUtils.addressesToStrings(message.getFrom(), true /* mime decode */), ", "); } catch (MessagingException e) { /* * Fallback to raw headers */ decodedFrom = message.getHeader("from", ", "); } fromHeader = StringUtils.abbreviate(decodedFrom, FROM_MAX_LENGTH); } catch (MessagingException e) { logger.debug("Error getting from.", e); } }
From source file:org.alfresco.email.server.impl.subetha.SubethaEmailMessage.java
private void processMimeMessage(MimeMessage mimeMessage) { if (from == null) { Address[] addresses = null;/*from w ww. j a v a 2 s . com*/ try { addresses = mimeMessage.getFrom(); } catch (MessagingException e) { throw new EmailMessageException(ERR_EXTRACTING_FROM_ADDRESS, e.getMessage()); } if (addresses == null || addresses.length == 0) { //throw new EmailMessageException(ERR_NO_FROM_ADDRESS); } else { if (addresses[0] instanceof InternetAddress) { from = ((InternetAddress) addresses[0]).getAddress(); } else { from = addresses[0].toString(); } } } if (to == null) { Address[] addresses = null; try { addresses = mimeMessage.getAllRecipients(); } catch (MessagingException e) { throw new EmailMessageException(ERR_EXTRACTING_TO_ADDRESS, e.getMessage()); } if (addresses == null || addresses.length == 0) { //throw new EmailMessageException(ERR_NO_TO_ADDRESS); } else { if (addresses[0] instanceof InternetAddress) { to = ((InternetAddress) addresses[0]).getAddress(); } else { to = addresses[0].toString(); } } } if (cc == null) { try { ArrayList<String> list = new ArrayList<String>(); Address[] cca = mimeMessage.getRecipients(RecipientType.CC); if (cca != null) { for (Address a : cca) { list.add(a.toString()); } } cc = list; } catch (MessagingException e) { // Do nothing - this is not a show-stopper. cc = null; } } try { subject = mimeMessage.getSubject(); //subject = encodeSubject(mimeMessage.getSubject()); } catch (MessagingException e) { throw new EmailMessageException(ERR_EXTRACTING_SUBJECT, e.getMessage()); } //if (subject == null) //{ // subject = ""; // Just anti-null stub :) //} try { sentDate = mimeMessage.getSentDate(); } catch (MessagingException e) { throw new EmailMessageException(ERR_EXTRACTING_SENT_DATE, e.getMessage()); } if (sentDate == null) { sentDate = new Date(); // Just anti-null stub :) } parseMessagePart(mimeMessage); attachments = new EmailMessagePart[attachmentList.size()]; attachmentList.toArray(attachments); attachmentList = null; }
From source file:org.silverpeas.core.mail.TestSmtpMailSending.java
private void assertMailSent(MailToSend verifiedMailToSend) throws Exception { assertThat("assertMailSent is compatible with one receiver only...", verifiedMailToSend.getTo(), hasSize(1));/*from www. j a va 2 s . c o m*/ assertThat(verifiedMailToSend.getTo().getRecipientType().getTechnicalType(), is(Message.RecipientType.TO)); MimeMessage[] messages = greenMailRule.getReceivedMessages(); assertThat(messages, arrayWithSize(1)); MimeMessage sentMessage = messages[0]; MailAddress originalReceiverMailAddress = verifiedMailToSend.getTo().iterator().next(); assertThat(sentMessage.getFrom().length, is(1)); assertThat(sentMessage.getFrom()[0], instanceOf(InternetAddress.class)); InternetAddress internetAddressFrom = (InternetAddress) sentMessage.getFrom()[0]; assertThat(internetAddressFrom.getAddress(), is(verifiedMailToSend.getFrom().getEmail())); assertThat(internetAddressFrom.getPersonal(), is(StringUtil.defaultStringIfNotDefined(verifiedMailToSend.getFrom().getName(), null))); assertThat(sentMessage.getRecipients(Message.RecipientType.TO).length, is(1)); assertThat(sentMessage.getRecipients(Message.RecipientType.CC), nullValue()); assertThat(sentMessage.getRecipients(Message.RecipientType.BCC), nullValue()); assertThat(sentMessage.getRecipients(Message.RecipientType.TO)[0], instanceOf(InternetAddress.class)); InternetAddress internetAddressTo = (InternetAddress) sentMessage .getRecipients(Message.RecipientType.TO)[0]; assertThat(internetAddressTo.getAddress(), is(originalReceiverMailAddress.getEmail())); assertThat(internetAddressTo.getPersonal(), nullValue()); assertThat(sentMessage.getSubject(), is(verifiedMailToSend.getSubject())); if (verifiedMailToSend.getContent().getValue() instanceof Multipart) { assertThat(sentMessage.getContent(), instanceOf(verifiedMailToSend.getContent().getValue().getClass())); } else { assertThat(sentMessage.getContent().toString().replaceAll("[\n\r]*$", ""), is(verifiedMailToSend.getContent().getValue())); } assertThat(DateUtils.addSeconds(sentMessage.getSentDate(), 10), greaterThanOrEqualTo(new Date())); assertThat(sentMessage.getReplyTo().length, is(1)); if (verifiedMailToSend.isReplyToRequired()) { assertThat(sentMessage.getHeader("Reply-To"), notNullValue()); assertThat(sentMessage.getReplyTo()[0], instanceOf(InternetAddress.class)); InternetAddress internetAddressReplyTo = (InternetAddress) sentMessage.getReplyTo()[0]; assertThat(internetAddressReplyTo.getAddress(), is(verifiedMailToSend.getFrom().getEmail())); assertThat(internetAddressReplyTo.getPersonal(), is(StringUtil.defaultStringIfNotDefined(verifiedMailToSend.getFrom().getName(), null))); } else { assertThat(sentMessage.getHeader("Reply-To"), nullValue()); assertThat(sentMessage.getReplyTo()[0].toString(), is(internetAddressFrom.toString())); } }
From source file:org.apache.james.mailetcontainer.impl.JamesMailetContext.java
/** * Place a mail on the spool for processing * * @param message the message to send/*from ww w . ja v a 2 s. c om*/ * @throws MessagingException if an exception is caught while placing the mail on the spool */ @Override public void sendMail(MimeMessage message) throws MessagingException { MailAddress sender = new MailAddress((InternetAddress) message.getFrom()[0]); Collection<MailAddress> recipients = new HashSet<MailAddress>(); Address addresses[] = message.getAllRecipients(); if (addresses != null) { for (Address address : addresses) { // Javamail treats the "newsgroups:" header field as a // recipient, so we want to filter those out. if (address instanceof InternetAddress) { recipients.add(new MailAddress((InternetAddress) address)); } } } sendMail(sender, recipients, message); }