Example usage for javax.mail.internet MimeMessage getHeader

List of usage examples for javax.mail.internet MimeMessage getHeader

Introduction

In this page you can find the example usage for javax.mail.internet MimeMessage getHeader.

Prototype

@Override
public String getHeader(String name, String delimiter) throws MessagingException 

Source Link

Document

Get all the headers for this header name, returned as a single String, with headers separated by the delimiter.

Usage

From source file:Main.java

public static String getRawFrom(MimeMessage msg) {
    String rawFrom = null;/*  w  w  w.ja  va  2 s  . c  om*/
    try {
        rawFrom = msg.getHeader("From", ",");
        if (null == rawFrom) {
            rawFrom = msg.getHeader("Sender", ",");
        }
    } catch (MessagingException ignore) {
    }
    return null != rawFrom ? rawFrom : "";
}

From source file:Main.java

public static String getRawReplyTo(MimeMessage msg) {
    String rawReplyto = null;/*w  w w.  j a  v  a  2 s  .  c om*/
    try {
        rawReplyto = msg.getHeader("Reply-To", ",");

        if (rawReplyto == null) {
            rawReplyto = getRawFrom(msg);
        }
    } catch (MessagingException ignore) {
    }
    return null != rawReplyto ? rawReplyto : "";
}

From source file:io.lavagna.service.MailTicketService.java

private String getDeliveredTo(MimeMessage message) throws MessagingException {
    return message.getHeader("Delivered-To", "");
}

From source file:net.duckling.ddl.service.mail.impl.MailServiceImpl.java

private void cheat(MimeMessage mimeMessage, String serverDomain) throws MessagingException {
    mimeMessage.saveChanges();/*from ww  w.jav  a2s.c  o  m*/
    mimeMessage.setHeader("User-Agent", "Thunderbird 2.0.0.16 (Windows/20080708)");
    String messageid = mimeMessage.getHeader("Message-ID", null);
    messageid = messageid.replaceAll("\\.JavaMail.*", "@" + serverDomain + ">");
    mimeMessage.setHeader("Message-ID", messageid);
}

From source file:com.zimbra.cs.imap.ImapMessage.java

static void serializeEnvelope(PrintStream ps, MimeMessage mm) throws MessagingException {
    // 7.4.2: "The fields of the envelope structure are in the following order: date, subject,
    //         from, sender, reply-to, to, cc, bcc, in-reply-to, and message-id.  The date,
    //         subject, in-reply-to, and message-id fields are strings.  The from, sender,
    //         reply-to, to, cc, and bcc fields are parenthesized lists of address structures."
    InternetAddress[] from = Mime.parseAddressHeader(mm, "From", false);
    InternetAddress[] sender = Mime.parseAddressHeader(mm, "Sender", false),
            replyTo = Mime.parseAddressHeader(mm, "Reply-To", false);
    ps.write('(');
    nstring(ps, mm.getHeader("Date", ","));
    ps.write(' ');
    nstring2047(ps, Mime.getSubject(mm));
    ps.write(' ');
    naddresses(ps, from);/*from   w w w  .  jav  a2s . c  o  m*/
    ps.write(' ');
    naddresses(ps, sender.length == 0 ? from : sender);
    ps.write(' ');
    naddresses(ps, replyTo.length == 0 ? from : replyTo);
    ps.write(' ');
    naddresses(ps, Mime.parseAddressHeader(mm, "To", false));
    ps.write(' ');
    naddresses(ps, Mime.parseAddressHeader(mm, "CC", false));
    ps.write(' ');
    naddresses(ps, Mime.parseAddressHeader(mm, "BCC", false));
    ps.write(' ');
    nstring(ps, mm.getHeader("In-Reply-To", " "));
    ps.write(' ');
    nstring(ps, mm.getMessageID());
    ps.write(')');
}

From source file:mitm.application.djigzo.ca.PFXMailBuilderTest.java

@Test
public void testReplacePFXMissingMarker() throws Exception {
    byte[] pfx = IOUtils.toByteArray(new FileInputStream(testPFX));

    PFXMailBuilder builder = new PFXMailBuilder(
            IOUtils.toString(new FileInputStream("test/resources/templates/mail-pfx-no-marker.ftl")),
            templateBuilder);// www  .j a v a2  s. c o m

    String from = "123@test.com";

    builder.setFrom(new InternetAddress(from, "test user"));
    builder.setPFX(pfx);
    builder.addProperty("test", "new value");

    MimeMessage message = builder.createMessage();

    assertNotNull(message);

    MailUtils.writeMessage(message, new File(tempDir, "testReplacePFXMissingMarker.eml"));

    assertEquals("new value", message.getHeader("X-TEST", ","));

    /*
     * Check if the PFX has really been replaced
     */
    byte[] newPFX = getPFX(message);

    KeyStore keyStore = SecurityFactoryFactory.getSecurityFactory().createKeyStore("PKCS12");

    keyStore.load(new ByteArrayInputStream(newPFX), "test".toCharArray());

    assertEquals(22, keyStore.size());
}

From source file:mitm.application.djigzo.james.mailets.DKIMSignTest.java

@Test
public void testDKIMSignNonStandardHeader() throws Exception {
    MockMailetConfig mailetConfig = new MockMailetConfig("test");

    Mailet mailet = new DKIMSign();

    mailetConfig.setInitParameter("privateKey", KEY_PAIR_PEM);
    mailetConfig.setInitParameter("signatureTemplate",
            "v=1; s=selector; d=example.com; " + "h=from:to; a=rsa-sha256; bh=; b=;");
    mailetConfig.setInitParameter("dkimHeader", "X-Djigzo-DKIM");

    mailet.init(mailetConfig);//  w  w  w  .  j a v a 2s  . c  o m

    MockMail mail = new MockMail();

    MimeMessage message = loadMessage("simple-text-message.eml");

    mail.setMessage(message);

    mailet.service(mail);

    File resultFile = saveMessage(mail.getMessage(), "testDKIMSignNonStandardHeader.eml");

    assertTrue(StringUtils.isNotEmpty(message.getHeader("X-Djigzo-DKIM", " ")));

    assertTrue(verify(resultFile, "X-Djigzo-DKIM"));
}

From source file:mitm.common.mail.repository.hibernate.MailRepositoryItemEntity.java

private void initSubject(MimeMessage message) {
    try {/*ww w. j a  v a 2s . co m*/
        try {
            subject = message.getSubject();
        } catch (MessagingException e) {
            /*
             * Fallback to raw headers
             */
            subject = message.getHeader("subject", ", ");
        }

        subject = StringUtils.abbreviate(subject, SUBJECT_MAX_LENGTH);
    } catch (MessagingException e) {
        logger.debug("Error getting subject.", e);
    }
}

From source file:com.zimbra.cs.mime.Mime.java

/**
 * Returns the decoded value of the <tt>Subject</tt> header, or
 * <tt>null</tt> if the header does not exist.
 *//*from ww  w. j av a  2s  .  c om*/
public static String getSubject(MimeMessage mm) throws MessagingException {
    String subject = mm.getHeader("Subject", null);
    return subject == null ? null : ZInternetHeader.decode(subject);
}

From source file:com.zimbra.cs.mime.Mime.java

/** Returns the message-ids from the specified message header.  The
 *  enclosing angle brackets and any embedded comments and quoted-strings
 *  are stripped.  No duplicate elimination is performed. */
public static List<String> getReferences(MimeMessage mm, String header) {
    try {//from w w  w . ja v a2 s .  c om
        return getReferences(mm.getHeader(header, " "));
    } catch (MessagingException e) {
        return new ArrayList<String>(0);
    }
}