List of usage examples for javax.mail.internet MimeMessage getHeader
@Override public String[] getHeader(String name) throws MessagingException
From source file:com.alvexcore.repo.emails.impl.ExtendedEmailMessage.java
protected void parseMessage(MimeMessage msg, UIDFolder folder) { try {//from w w w . j a v a 2 s . c o m if (uid == -1) { uid = folder.getUID(msg); } if (id == null) { id = msg.getMessageID(); } if (inReplyTo == null) { String[] headers = msg.getHeader(IN_REPLY_TO); if (headers != null) { if (headers.length == 1) inReplyTo = headers[0]; else if (headers.length > 1) throw new EmailMessageException(ERR_EXTRACTING_IN_REPLY_TO, "Not more than one " + IN_REPLY_TO + " header expected"); } } } catch (MessagingException e) { throw new EmailMessageException(ERR_EXTRACTING_ATTRS, e.getMessage()); } }
From source file:com.duroty.service.Mailet.java
/** * DOCUMENT ME!//from w w w. ja v a 2 s. c o m * * @param id DOCUMENT ME! * @param message DOCUMENT ME! */ private void saveInReplyTo(String id, MimeMessage message) { try { String[] inReplyTos = message.getHeader(RFC2822Headers.IN_REPLY_TO); String messageId = message.getMessageID(); if ((inReplyTos == null) || (inReplyTos.length <= 0)) { if (messageId != null) { message.addHeader(RFC2822Headers.IN_REPLY_TO, messageId); message.saveChanges(); } } String[] references = message.getHeader(RFC2822Headers.REFERENCES); if ((references == null) || (references.length <= 0)) { message.addHeader(RFC2822Headers.REFERENCES, messageId); message.saveChanges(); } } catch (Exception e) { } }
From source file:com.duroty.service.Mailet.java
/** * DOCUMENT ME!//from w w w . j ava 2 s . c o m * * @param mime DOCUMENT ME! * * @return DOCUMENT ME! */ private boolean isRecent(MimeMessage mime) { boolean control = true; try { String[] aux = mime.getHeader("X-DRecent"); if ((aux != null) && (aux.length > 0)) { for (int i = 0; i < aux.length; i++) { control = Boolean.parseBoolean(aux[i].trim()); break; } } } catch (Exception e) { control = true; } return control; }
From source file:com.stimulus.archiva.store.MessageStore.java
public void saveEmailChanges(MimeMessage message) throws MessagingException { logger.debug("saveEmailChanges"); String[] messageId = message.getHeader("Message-Id"); //try { System.out.println("Content:"+message.getContent()); } catch (Exception e) {} message.saveChanges();/*from ww w. j av a2 s .co m*/ if (messageId != null && messageId.length > 0) message.setHeader("Message-Id", messageId[0]); }
From source file:com.duroty.service.Mailet.java
/** * DOCUMENT ME!/*from w w w .j a v a 2 s. c o m*/ * * @param mime DOCUMENT ME! * * @return DOCUMENT ME! */ private String getBoxName(MimeMessage mime, Users user) { String _box = null; try { if (_box == null) { String[] aux = mime.getHeader(BayesianAnalysis.messageIsSpam); if ((aux != null) && (aux.length > 0)) { if (controlSpam(user, mime)) { _box = "SPAM"; } } else { aux = mime.getHeader("X-DBox"); if ((aux != null) && (aux.length > 0)) { _box = aux[0]; } } } } catch (Exception e) { _box = "INBOX"; } if (_box == null) { _box = "INBOX"; } return _box; }
From source file:com.duroty.service.Mailet.java
/** * DOCUMENT ME!//from w w w. ja v a 2 s .c o m * * @param id DOCUMENT ME! * @param mime DOCUMENT ME! * * @return DOCUMENT ME! */ private String getParentId(String id, MimeMessage mime) { String references = null; try { if (references == null) { String[] aux = mime.getHeader(RFC2822Headers.REFERENCES); //Cal controlar que portem un array if ((aux != null) && (aux.length > 0)) { if (aux[0] != null) { String[] ref = aux[0].split("\\s+"); if ((ref != null) && (ref.length > 1)) { references = ref[0]; } } if (references == null) { references = aux[0]; } } } if (references == null) { String[] aux = mime.getHeader(RFC2822Headers.IN_REPLY_TO); //Cal veure que portem un array if ((aux != null) && (aux.length > 0)) { references = aux[0]; } } if (references == null) { references = mime.getMessageID(); mime.addHeader(RFC2822Headers.IN_REPLY_TO, references); mime.addHeader(RFC2822Headers.REFERENCES, references); } } catch (MessagingException e) { references = null; } return references; }
From source file:com.aimluck.eip.mail.util.ALMailUtils.java
/** * ????? Recieved ??/*from w ww . ja va 2 s . com*/ * * @param msg * @return */ public static Date getReceivedDate(MimeMessage msg) { try { String[] receivedHeaders = msg.getHeader("Received"); if (receivedHeaders == null || receivedHeaders.length == 0) { return null; } Date receivedDate = null; StringTokenizer st = new StringTokenizer(receivedHeaders[0], ";"); if (st.countTokens() == 2) { st.nextToken(); String receivedDateStr = st.nextToken(); if (receivedDateStr != null && !receivedDateStr.equals("")) { receivedDate = MailUtility.parseDate(receivedDateStr); } return receivedDate; } else { return null; } } catch (Exception e) { return null; } }
From source file:jenkins.plugins.mailer.tasks.MimeMessageBuilderTest.java
@Test public void test_construction() throws Exception { MimeMessageBuilder messageBuilder = new MimeMessageBuilder(); messageBuilder.addRecipients("tom.xxxx@gmail.com, tom.yyyy@gmail.com"); MimeMessage mimeMessage = messageBuilder.buildMimeMessage(); // check from and reply-to Address[] from = mimeMessage.getFrom(); Assert.assertNotNull(from);// ww w . ja va 2s . co m Assert.assertEquals(1, from.length); Assert.assertEquals(A, from[0].toString()); Address[] replyTo = mimeMessage.getReplyTo(); Assert.assertNotNull(from); Assert.assertEquals(1, replyTo.length); Assert.assertEquals(A, replyTo[0].toString()); // check the recipient list... Address[] allRecipients = mimeMessage.getAllRecipients(); Assert.assertNotNull(allRecipients); Assert.assertEquals(2, allRecipients.length); Assert.assertEquals(X, allRecipients[0].toString()); Assert.assertEquals(Y, allRecipients[1].toString()); // Make sure we can regen the instance identifier public key String encodedIdent = mimeMessage.getHeader("X-Instance-Identity")[0]; byte[] image = Base64.decodeBase64(encodedIdent); PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(image)); Assert.assertNotNull(publicKey); }
From source file:mitm.application.djigzo.james.mailets.FilterSubjectTest.java
@Test public void testEncodedSubject() throws Exception { MockMailetConfig mailetConfig = new MockMailetConfig("test"); Mailet mailet = new FilterSubject(); mailetConfig.setInitParameter("filter", "/(^(.*)$)/ $1 !@#\\$%^&*()"); mailet.init(mailetConfig);/* www . j a va 2 s . c o m*/ MockMail mail = new MockMail(); MimeMessage message = MailUtils.loadMessage(new File(testBase, "mail/encoded-subject.eml")); assertEquals("test encoded subject ", message.getSubject()); mail.setMessage(message); Collection<MailAddress> recipients = new LinkedList<MailAddress>(); recipients.add(new MailAddress("test@example.com")); mail.setRecipients(recipients); mail.setSender(new MailAddress("sender@example.com")); mailet.service(mail); message = mail.getMessage(); assertEquals("test encoded subject !@#$%^&*()", message.getSubject()); /* * Check if subject is encoded */ assertEquals( "=?UTF-8?Q?test_encoded_subjec?=\r\n =?UTF-8?Q?t_=C3=A4=C3=B6=C3=BC_=C3=84=C3=96=C3=9C_!@#$%^&*()?=", StringUtils.join(message.getHeader("subject"), ',')); }
From source file:fr.gouv.culture.vitam.eml.EmlExtract.java
public static String extractInfoMessage(MimeMessage message, Element root, VitamArgument argument, ConfigLoader config) {//from www.j a v a 2s .c om File oldDir = argument.currentOutputDir; if (argument.currentOutputDir == null) { if (config.outputDir != null) { argument.currentOutputDir = new File(config.outputDir); } } Element keywords = XmlDom.factory.createElement(EMAIL_FIELDS.keywords.name); Element metadata = XmlDom.factory.createElement(EMAIL_FIELDS.metadata.name); String skey = ""; String id = config.addRankId(root); Address[] from = null; Element sub2 = null; try { from = message.getFrom(); } catch (MessagingException e1) { String[] partialResult; try { partialResult = message.getHeader("From"); if (partialResult != null && partialResult.length > 0) { sub2 = XmlDom.factory.createElement(EMAIL_FIELDS.from.name); Element add = XmlDom.factory.createElement(EMAIL_FIELDS.fromUnit.name); add.setText(partialResult[0]); sub2.add(add); } } catch (MessagingException e) { } } Address sender = null; try { sender = message.getSender(); } catch (MessagingException e1) { String[] partialResult; try { partialResult = message.getHeader("Sender"); if (partialResult != null && partialResult.length > 0) { if (sub2 == null) { sub2 = XmlDom.factory.createElement(EMAIL_FIELDS.from.name); Element add = XmlDom.factory.createElement(EMAIL_FIELDS.fromUnit.name); add.setText(partialResult[0]); sub2.add(add); } } } catch (MessagingException e) { } } if (from != null && from.length > 0) { String value0 = null; Element sub = (sub2 != null ? sub2 : XmlDom.factory.createElement(EMAIL_FIELDS.from.name)); if (sender != null) { value0 = addAddress(sub, EMAIL_FIELDS.fromUnit.name, sender, null); } for (Address address : from) { addAddress(sub, EMAIL_FIELDS.fromUnit.name, address, value0); } metadata.add(sub); } else if (sender != null) { Element sub = (sub2 != null ? sub2 : XmlDom.factory.createElement(EMAIL_FIELDS.from.name)); addAddress(sub, EMAIL_FIELDS.fromUnit.name, sender, null); metadata.add(sub); } else { if (sub2 != null) { metadata.add(sub2); } } Address[] replyTo = null; try { replyTo = message.getReplyTo(); if (replyTo != null && replyTo.length > 0) { Element sub = XmlDom.factory.createElement(EMAIL_FIELDS.replyTo.name); for (Address address : replyTo) { addAddress(sub, EMAIL_FIELDS.fromUnit.name, address, null); } metadata.add(sub); } } catch (MessagingException e1) { String[] partialResult; try { partialResult = message.getHeader("ReplyTo"); if (partialResult != null && partialResult.length > 0) { sub2 = XmlDom.factory.createElement(EMAIL_FIELDS.replyTo.name); addAddress(sub2, EMAIL_FIELDS.fromUnit.name, partialResult, null); /*Element add = XmlDom.factory.createElement(EMAIL_FIELDS.fromUnit.name); add.setText(partialResult[0]); sub2.add(add);*/ metadata.add(sub2); } } catch (MessagingException e) { } } Address[] toRecipients = null; try { toRecipients = message.getRecipients(Message.RecipientType.TO); if (toRecipients != null && toRecipients.length > 0) { Element sub = XmlDom.factory.createElement(EMAIL_FIELDS.toRecipients.name); for (Address address : toRecipients) { addAddress(sub, EMAIL_FIELDS.toUnit.name, address, null); } metadata.add(sub); } } catch (MessagingException e1) { String[] partialResult; try { partialResult = message.getHeader("To"); if (partialResult != null && partialResult.length > 0) { sub2 = XmlDom.factory.createElement(EMAIL_FIELDS.toRecipients.name); addAddress(sub2, EMAIL_FIELDS.toUnit.name, partialResult, null); /*for (String string : partialResult) { Element add = XmlDom.factory.createElement(EMAIL_FIELDS.toUnit.name); add.setText(string); sub2.add(add); }*/ metadata.add(sub2); } } catch (MessagingException e) { } } Address[] ccRecipients; try { ccRecipients = message.getRecipients(Message.RecipientType.CC); if (ccRecipients != null && ccRecipients.length > 0) { Element sub = XmlDom.factory.createElement(EMAIL_FIELDS.ccRecipients.name); for (Address address : ccRecipients) { addAddress(sub, EMAIL_FIELDS.ccUnit.name, address, null); } metadata.add(sub); } } catch (MessagingException e1) { String[] partialResult; try { partialResult = message.getHeader("Cc"); if (partialResult != null && partialResult.length > 0) { sub2 = XmlDom.factory.createElement(EMAIL_FIELDS.ccRecipients.name); addAddress(sub2, EMAIL_FIELDS.ccUnit.name, partialResult, null); /*for (String string : partialResult) { Element add = XmlDom.factory.createElement(EMAIL_FIELDS.ccUnit.name); add.setText(string); sub2.add(add); }*/ metadata.add(sub2); } } catch (MessagingException e) { } } Address[] bccRecipients; try { bccRecipients = message.getRecipients(Message.RecipientType.BCC); if (bccRecipients != null && bccRecipients.length > 0) { Element sub = XmlDom.factory.createElement(EMAIL_FIELDS.bccRecipients.name); for (Address address : bccRecipients) { addAddress(sub, EMAIL_FIELDS.bccUnit.name, address, null); } metadata.add(sub); } } catch (MessagingException e1) { String[] partialResult; try { partialResult = message.getHeader("Cc"); if (partialResult != null && partialResult.length > 0) { sub2 = XmlDom.factory.createElement(EMAIL_FIELDS.bccRecipients.name); addAddress(sub2, EMAIL_FIELDS.bccUnit.name, partialResult, null); /*for (String string : partialResult) { Element add = XmlDom.factory.createElement(EMAIL_FIELDS.bccUnit.name); add.setText(string); sub2.add(add); }*/ metadata.add(sub2); } } catch (MessagingException e) { } } try { String subject = message.getSubject(); if (subject != null) { Element sub = XmlDom.factory.createElement(EMAIL_FIELDS.subject.name); sub.setText(StringUtils.unescapeHTML(subject, true, false)); metadata.add(sub); } Date sentDate = message.getSentDate(); if (sentDate != null) { Element sub = XmlDom.factory.createElement(EMAIL_FIELDS.sentDate.name); sub.setText(sentDate.toString()); metadata.add(sub); } Date receivedDate = message.getReceivedDate(); if (receivedDate != null) { Element sub = XmlDom.factory.createElement(EMAIL_FIELDS.receivedDate.name); sub.setText(receivedDate.toString()); metadata.add(sub); } String[] headers = message.getHeader("Received"); if (headers != null) { Element sub = XmlDom.factory.createElement(EMAIL_FIELDS.receptionTrace.name); MailDateFormat mailDateFormat = null; long maxTime = 0; if (receivedDate == null) { mailDateFormat = new MailDateFormat(); } for (String string : headers) { Element sub3 = XmlDom.factory.createElement(EMAIL_FIELDS.trace.name); sub3.setText(StringUtils.unescapeHTML(string, true, false)); sub.add(sub3); if (receivedDate == null) { int pos = string.lastIndexOf(';'); if (pos > 0) { String recvdate = string.substring(pos + 2).replaceAll("\t\n\r\f", "").trim(); try { Date date = mailDateFormat.parse(recvdate); if (date.getTime() > maxTime) { maxTime = date.getTime(); } } catch (ParseException e) { } } } } if (receivedDate == null) { Element subdate = XmlDom.factory.createElement(EMAIL_FIELDS.receivedDate.name); Date date = new Date(maxTime); subdate.setText(date.toString()); metadata.add(subdate); } metadata.add(sub); } int internalSize = message.getSize(); if (internalSize > 0) { Element sub = XmlDom.factory.createElement(EMAIL_FIELDS.emailSize.name); sub.setText(Integer.toString(internalSize)); metadata.add(sub); } String encoding = message.getEncoding(); if (encoding != null) { Element sub = XmlDom.factory.createElement(EMAIL_FIELDS.encoding.name); sub.setText(StringUtils.unescapeHTML(encoding, true, false)); metadata.add(sub); } String description = message.getDescription(); if (description != null) { Element sub = XmlDom.factory.createElement(EMAIL_FIELDS.description.name); sub.setText(StringUtils.unescapeHTML(description, true, false)); metadata.add(sub); } String contentType = message.getContentType(); if (contentType != null) { Element sub = XmlDom.factory.createElement(EMAIL_FIELDS.contentType.name); sub.setText(StringUtils.unescapeHTML(contentType, true, false)); metadata.add(sub); } headers = message.getHeader("Content-Transfer-Encoding"); if (headers != null) { Element sub = XmlDom.factory.createElement(EMAIL_FIELDS.contentTransferEncoding.name); StringBuilder builder = new StringBuilder(); for (String string : headers) { builder.append(StringUtils.unescapeHTML(string, true, false)); builder.append(' '); } sub.setText(builder.toString()); metadata.add(sub); } String[] contentLanguage = message.getContentLanguage(); if (contentLanguage != null) { Element sub = XmlDom.factory.createElement(EMAIL_FIELDS.contentLanguage.name); StringBuilder builder = new StringBuilder(); for (String string : contentLanguage) { builder.append(StringUtils.unescapeHTML(string, true, false)); builder.append(' '); } sub.setText(builder.toString()); metadata.add(sub); } String contentId = message.getContentID(); if (contentId != null) { Element sub = XmlDom.factory.createElement(EMAIL_FIELDS.contentId.name); sub.setText(StringUtils.removeChevron(StringUtils.unescapeHTML(contentId, true, false))); metadata.add(sub); } String disposition = message.getDisposition(); if (disposition != null) { Element sub = XmlDom.factory.createElement(EMAIL_FIELDS.disposition.name); sub.setText(StringUtils.removeChevron(StringUtils.unescapeHTML(disposition, true, false))); metadata.add(sub); } headers = message.getHeader("Keywords"); if (headers != null) { Element sub = XmlDom.factory.createElement(EMAIL_FIELDS.msgKeywords.name); StringBuilder builder = new StringBuilder(); for (String string : headers) { builder.append(StringUtils.unescapeHTML(string, true, false)); builder.append(' '); } sub.setText(builder.toString()); metadata.add(sub); } String messageId = message.getMessageID(); if (messageId != null) { messageId = StringUtils.removeChevron(StringUtils.unescapeHTML(messageId, true, false)).trim(); if (messageId.length() > 1) { Element sub = XmlDom.factory.createElement(EMAIL_FIELDS.messageId.name); sub.setText(messageId); metadata.add(sub); } } headers = message.getHeader("In-Reply-To"); String inreplyto = null; if (headers != null) { StringBuilder builder = new StringBuilder(); for (String string : headers) { builder.append(StringUtils.removeChevron(StringUtils.unescapeHTML(string, true, false))); builder.append(' '); } inreplyto = builder.toString().trim(); if (inreplyto.length() > 0) { Element sub = XmlDom.factory.createElement(EMAIL_FIELDS.inReplyTo.name); sub.setText(inreplyto); if (messageId != null && messageId.length() > 1) { String old = filEmls.get(inreplyto); if (old == null) { old = messageId; } else { old += "," + messageId; } filEmls.put(inreplyto, old); } metadata.add(sub); } } headers = message.getHeader("References"); if (headers != null) { Element sub = XmlDom.factory.createElement(EMAIL_FIELDS.references.name); StringBuilder builder = new StringBuilder(); for (String string : headers) { builder.append(StringUtils.removeChevron(StringUtils.unescapeHTML(string, true, false))); builder.append(' '); } String[] refs = builder.toString().trim().split(" "); for (String string : refs) { if (string.length() > 0) { Element ref = XmlDom.factory.createElement(EMAIL_FIELDS.reference.name); ref.setText(string); sub.add(ref); } } metadata.add(sub); } Element prop = XmlDom.factory.createElement(EMAIL_FIELDS.properties.name); headers = message.getHeader("X-Priority"); if (headers == null) { headers = message.getHeader("Priority"); if (headers != null && headers.length > 0) { prop.addAttribute(EMAIL_FIELDS.priority.name, headers[0]); } } else if (headers != null && headers.length > 0) { String imp = headers[0]; try { int Priority = Integer.parseInt(imp); switch (Priority) { case 5: imp = "LOWEST"; break; case 4: imp = "LOW"; break; case 3: imp = "NORMAL"; break; case 2: imp = "HIGH"; break; case 1: imp = "HIGHEST"; break; default: imp = "LEV" + Priority; } } catch (NumberFormatException e) { // ignore since imp will be used as returned } prop.addAttribute(EMAIL_FIELDS.priority.name, imp); } headers = message.getHeader("Sensitivity"); if (headers != null && headers.length > 0) { prop.addAttribute(EMAIL_FIELDS.sensitivity.name, headers[0]); } headers = message.getHeader("X-RDF"); if (headers != null && headers.length > 0) { System.err.println("Found X-RDF"); StringBuilder builder = new StringBuilder(); for (String string : headers) { builder.append(string); builder.append("\n"); } try { byte[] decoded = org.apache.commons.codec.binary.Base64.decodeBase64(builder.toString()); String rdf = new String(decoded); Document tempDocument = DocumentHelper.parseText(rdf); Element xrdf = prop.addElement("x-rdf"); xrdf.add(tempDocument.getRootElement()); } catch (Exception e) { System.err.println("Cannot decode X-RDF: " + e.getMessage()); } } try { File old = argument.currentOutputDir; if (config.extractFile) { File newOutDir = new File(argument.currentOutputDir, id); newOutDir.mkdirs(); argument.currentOutputDir = newOutDir; } if (argument.extractKeyword) { skey = handleMessage(message, metadata, prop, id, argument, config); // should have hasAttachment if (prop.hasContent()) { metadata.add(prop); } if (metadata.hasContent()) { root.add(metadata); } ExtractInfo.exportMetadata(keywords, skey, "", config, null); if (keywords.hasContent()) { root.add(keywords); } } else { handleMessage(message, metadata, prop, id, argument, config); // should have hasAttachment if (prop.hasContent()) { metadata.add(prop); } if (metadata.hasContent()) { root.add(metadata); } } argument.currentOutputDir = old; } catch (IOException e) { System.err.println(StaticValues.LBL.error_error.get() + e.toString()); } try { message.getInputStream().close(); } catch (IOException e) { System.err.println(StaticValues.LBL.error_error.get() + e.toString()); } root.addAttribute(EMAIL_FIELDS.status.name, "ok"); } catch (MessagingException e) { System.err.println(StaticValues.LBL.error_error.get() + e.toString()); e.printStackTrace(); String status = "Error during identification"; root.addAttribute(EMAIL_FIELDS.status.name, status); } catch (Exception e) { System.err.println(StaticValues.LBL.error_error.get() + e.toString()); e.printStackTrace(); String status = "Error during identification"; root.addAttribute(EMAIL_FIELDS.status.name, status); } argument.currentOutputDir = oldDir; return skey; }