Example usage for javax.mail.internet MimeMessage setHeader

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

Introduction

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

Prototype

@Override
public void setHeader(String name, String value) throws MessagingException 

Source Link

Document

Set the value for this header_name.

Usage

From source file:com.zimbra.qa.unittest.TestStoreManager.java

public static ParsedMessage getMessage() throws Exception {
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession());
    mm.setHeader("From", " Jimi <jimi@example.com>");
    mm.setHeader("To", " Janis <janis@example.com>");
    mm.setHeader("Subject", "Hello");
    mm.setHeader("Message-ID", "<sakfuslkdhflskjch@oiwm.example.com>");
    mm.setText("nothing to see here" + RandomStringUtils.random(1024));
    return new ParsedMessage(mm, false);
}

From source file:com.zimbra.cs.mailbox.MailboxTestUtil.java

public static ParsedMessage generateMessage(String subject) throws Exception {
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession());
    mm.setHeader("From", "Bob Evans <bob@example.com>");
    mm.setHeader("To", "Jimmy Dean <jdean@example.com>");
    mm.setHeader("Subject", subject);
    mm.setText("nothing to see here");
    return new ParsedMessage(mm, false);
}

From source file:com.zimbra.cs.mailbox.MailboxTestUtil.java

public static ParsedMessage generateHighPriorityMessage(String subject) throws Exception {
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession());
    mm.setHeader("From", "Hi Bob <bob@example.com>");
    mm.setHeader("To", "Jimmy Dean <jdean@example.com>");
    mm.setHeader("Subject", subject);
    mm.addHeader("Importance", "high");
    mm.setText("nothing to see here");
    return new ParsedMessage(mm, false);
}

From source file:com.zimbra.cs.mailbox.MailboxTestUtil.java

public static ParsedMessage generateLowPriorityMessage(String subject) throws Exception {
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession());
    mm.setHeader("From", "Lo Bob <bob@example.com>");
    mm.setHeader("To", "Jimmy Dean <jdean@example.com>");
    mm.setHeader("Subject", subject);
    mm.addHeader("Importance", "low");
    mm.setText("nothing to see here");
    return new ParsedMessage(mm, false);
}

From source file:com.buzzdavidson.spork.util.OWAUtils.java

private static void populateHeader(String key, Node mailNode, MimeMessage message) throws MessagingException {
    String data = fetchText(mailNode, PATTERN_MAP.get(key), false);
    if (data != null && data.length() > 0) {
        message.setHeader(HEADER_MAP.get(key), data);
    }// ww  w .j ava  2  s.  c  om
}

From source file:com.zimbra.cs.mailbox.MailboxTestUtil.java

public static ParsedMessage generateMessageWithAttachment(String subject) throws Exception {
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession());
    mm.setHeader("From", "Vera Oliphant <oli@example.com>");
    mm.setHeader("To", "Jimmy Dean <jdean@example.com>");
    mm.setHeader("Subject", subject);
    mm.setText("Good as gold");
    MimeMultipart multi = new ZMimeMultipart("mixed");
    ContentDisposition cdisp = new ContentDisposition(Part.ATTACHMENT);
    cdisp.setParameter("filename", "fun.txt");

    ZMimeBodyPart bp = new ZMimeBodyPart();
    // MimeBodyPart.setDataHandler() invalidates Content-Type and CTE if there is any, so make sure
    // it gets called before setting Content-Type and CTE headers.
    try {/*from   w  w w.  j  av a  2s .  c  om*/
        bp.setDataHandler(new DataHandler(new ByteArrayDataSource("Feeling attached.", "text/plain")));
    } catch (IOException e) {
        throw new MessagingException("could not generate mime part content", e);
    }
    bp.addHeader("Content-Disposition", cdisp.toString());
    bp.addHeader("Content-Type", "text/plain");
    bp.addHeader("Content-Transfer-Encoding", MimeConstants.ET_8BIT);
    multi.addBodyPart(bp);

    mm.setContent(multi);
    mm.saveChanges();

    return new ParsedMessage(mm, false);
}

From source file:jenkins.plugins.mailer.tasks.MimeMessageBuilder.java

public static void setInReplyTo(@Nonnull MimeMessage msg, @Nonnull String inReplyTo) throws MessagingException {
    msg.setHeader("In-Reply-To", inReplyTo);
    msg.setHeader("References", inReplyTo);
}

From source file:com.zxy.commons.email.MailMessageUtils.java

/**
 * ??eml/*from  w  w w. j  a  v  a 2 s.c  om*/
 * 
 * @param inputStream inputStream
 * @param from from
 * @param tos tos
 * @param properties 
 * @param isCloseInputStream ???InputStream
 * @throws EmailException EmailException
 * @throws MessagingException MessagingException
 */
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
public static void sendEml(InputStream inputStream, String from, List<String> tos,
        Map<String, String> properties, boolean isCloseInputStream) throws EmailException, MessagingException {
    try {
        // inputStream = new SharedFileInputStream(mailPath);
        Session session = getEmail().getMailSession();
        // session.getProperties().setProperty("mail.smtp.ehlo", "true");
        MimeMessage message = new MimeMessage(session, inputStream);
        if (!Strings.isNullOrEmpty(from)) {
            message.setFrom(new InternetAddress(from));
        }
        // 
        for (Map.Entry<String, String> entry : properties.entrySet()) {
            String name = entry.getKey();
            String value = entry.getValue();
            message.setHeader(name, value);
        }

        if (tos == null || tos.isEmpty()) {
            Transport.send(message);
        } else {
            InternetAddress[] internetAddresses = new InternetAddress[tos.size()];
            int index = 0;
            for (String to : tos) {
                internetAddresses[index] = new InternetAddress(to);
                index++;
            }
            Transport.send(message, internetAddresses);
        }
    } finally {
        if (isCloseInputStream) {
            IOUtils.closeQuietly(inputStream);
        }
    }
}

From source file:net.indialend.attendance.compnent.Email.java

/**
 * This method triggers the email in background thread i.e. the mail is sent
 * asynchronously//from   ww w . jav a 2  s.c  o  m
 *
 * @param from Email from which mail is triggered
 * @param to Email to whom mail has to be sent
 * @param subject Subject of the email Messages
 * @param msg Message body to be sent along with email
 * @throws InterruptedException
 */
@Async
public void sendMail(String from, String to, String subject, String msg) throws InterruptedException {
    try {

        MimeMessage message = mailSender.createMimeMessage();
        message.setSubject(subject);
        message.setHeader("Content-Type", "text/plain; charset=UTF-8");
        MimeMessageHelper helper;
        helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setText(msg, true);
        mailSender.send(message);
    } catch (MessagingException ex) {
        ex.printStackTrace();
    }
}

From source file:io.kamax.mxisd.threepid.connector.email.EmailSmtpConnector.java

@Override
public void send(String senderAddress, String senderName, String recipient, String content) {
    if (StringUtils.isBlank(senderAddress)) {
        throw new FeatureNotAvailable("3PID Email identity: sender address is empty - "
                + "You must set a value for notifications to work");
    }//from w  w  w . j  a  va 2s  .c  o  m

    if (StringUtils.isBlank(content)) {
        throw new InternalServerError("Notification content is empty");
    }

    try {
        InternetAddress sender = new InternetAddress(senderAddress, senderName);
        MimeMessage msg = new MimeMessage(session, IOUtils.toInputStream(content, StandardCharsets.UTF_8));
        msg.setHeader("X-Mailer", "mxisd"); // FIXME set version
        msg.setSentDate(new Date());
        msg.setFrom(sender);
        msg.setRecipients(Message.RecipientType.TO, recipient);
        msg.saveChanges();

        log.info("Sending invite to {} via SMTP using {}:{}", recipient, cfg.getHost(), cfg.getPort());
        SMTPTransport transport = (SMTPTransport) session.getTransport("smtp");
        transport.setStartTLS(cfg.getTls() > 0);
        transport.setRequireStartTLS(cfg.getTls() > 1);

        log.info("Connecting to {}:{}", cfg.getHost(), cfg.getPort());
        transport.connect(cfg.getHost(), cfg.getPort(), cfg.getLogin(), cfg.getPassword());
        try {
            transport.sendMessage(msg, InternetAddress.parse(recipient));
            log.info("Invite to {} was sent", recipient);
        } finally {
            transport.close();
        }
    } catch (UnsupportedEncodingException | MessagingException e) {
        throw new RuntimeException("Unable to send e-mail invite to " + recipient, e);
    }
}