Java Code Examples for javax.mail.internet.MimeMessage#setSender()

The following examples show how to use javax.mail.internet.MimeMessage#setSender() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GMail.java    From TwrpBuilder with GNU General Public License v3.0 6 votes vote down vote up
@Nullable
@Override
protected String doInBackground(String... strings) {
    try {
        MimeMessage message = new MimeMessage(session);
        DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes()));
        message.setSender(new InternetAddress(sender));
        message.setSubject(subject);
        message.setDataHandler(handler);
        if (recipients.indexOf(',') > 0)
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
        else
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
        Transport.send(message);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 2
Source File: MailNotificationSender.java    From digdag with Apache License 2.0 6 votes vote down vote up
@Override
public void sendNotification(Notification notification)
        throws NotificationException
{
    Session session = createSession();

    MimeMessage msg = new MimeMessage(session);

    try {
        msg.setFrom(newAddress(from));
        msg.setSender(newAddress(from));

        msg.setRecipients(MimeMessage.RecipientType.TO, addresses(this.to));
        msg.setRecipients(MimeMessage.RecipientType.CC, addresses(this.cc));
        msg.setRecipients(MimeMessage.RecipientType.BCC, addresses(this.bcc));

        msg.setSubject(subject);
        msg.setText(body(notification), "utf-8", isHtml ? "html" : "plain");
        Transport.send(msg);
    }
    catch (MessagingException | IOException | TemplateException ex) {
        throw Throwables.propagate(ex);
    }
}
 
Example 3
Source File: SessionWrapper.java    From email-notifier with Apache License 2.0 5 votes vote down vote up
public MimeMessage createMessage(String fromEmailId, String toEmailId, String subject, String body) throws MessagingException {
    MimeMessage message = new MimeMessage(instance);
    message.setFrom(new InternetAddress(fromEmailId));
    message.setRecipients(TO, toEmailId);
    message.setSubject(subject);
    message.setContent(message, "text/plain");
    message.setSentDate(new Date());
    message.setText(body);
    message.setSender(new InternetAddress(fromEmailId));
    message.setReplyTo(new InternetAddress[]{new InternetAddress(fromEmailId)});
    return message;
}
 
Example 4
Source File: MailSession.java    From gocd with Apache License 2.0 5 votes vote down vote up
public MimeMessage createMessage(String from, String to, String subject, String body)
        throws MessagingException {
    MimeMessage msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(from));
    msg.setRecipients(TO, to);
    msg.setSubject(subject);
    msg.setContent(msg, "text/plain");
    msg.setSentDate(new Date());
    msg.setText(body);
    msg.setSender(new InternetAddress(from));
    msg.setReplyTo(new InternetAddress[]{new InternetAddress(from)});
    return msg;
}
 
Example 5
Source File: AbstractSign.java    From james-project with Apache License 2.0 4 votes vote down vote up
/**
 * Service does the hard work, and signs
 *
 * @param mail the mail to sign
 * @throws MessagingException if a problem arises signing the mail
 */
@Override
public void service(Mail mail) throws MessagingException {
    
    try {
        if (!isOkToSign(mail)) {
            return;
        }
        
        MimeBodyPart wrapperBodyPart = getWrapperBodyPart(mail);
        
        MimeMessage originalMessage = mail.getMessage();
        
        // do it
        MimeMultipart signedMimeMultipart;
        if (wrapperBodyPart != null) {
            signedMimeMultipart = getKeyHolder().generate(wrapperBodyPart);
        } else {
            signedMimeMultipart = getKeyHolder().generate(originalMessage);
        }
        
        MimeMessage newMessage = new MimeMessage(Session.getDefaultInstance(System.getProperties(),
        null));
        Enumeration<String> headerEnum = originalMessage.getAllHeaderLines();
        while (headerEnum.hasMoreElements()) {
            newMessage.addHeaderLine(headerEnum.nextElement());
        }
        
        newMessage.setSender(new InternetAddress(getKeyHolder().getSignerAddress(), getSignerName()));
  
        if (isRebuildFrom()) {
            // builds a new "mixed" "From:" header
            InternetAddress modifiedFromIA = new InternetAddress(getKeyHolder().getSignerAddress(), mail.getMaybeSender().asString());
            newMessage.setFrom(modifiedFromIA);
            
            // if the original "ReplyTo:" header is missing sets it to the original "From:" header
            newMessage.setReplyTo(originalMessage.getReplyTo());
        }
        
        newMessage.setContent(signedMimeMultipart, signedMimeMultipart.getContentType());
        String messageId = originalMessage.getMessageID();
        newMessage.saveChanges();
        if (messageId != null) {
            newMessage.setHeader(RFC2822Headers.MESSAGE_ID, messageId);
        }
        
        mail.setMessage(newMessage);
        
        // marks this mail as server-signed
        mail.setAttribute(new Attribute(SMIMEAttributeNames.SMIME_SIGNING_MAILET, AttributeValue.of(this.getClass().getName())));
        // it is valid for us by definition (signed here by us)
        mail.setAttribute(new Attribute(SMIMEAttributeNames.SMIME_SIGNATURE_VALIDITY, AttributeValue.of("valid")));
        
        // saves the trusted server signer address
        // warning: should be same as the mail address in the certificate, but it is not guaranteed
        mail.setAttribute(new Attribute(SMIMEAttributeNames.SMIME_SIGNER_ADDRESS, AttributeValue.of(getKeyHolder().getSignerAddress())));
        
        if (isDebug()) {
            LOGGER.debug("Message signed, reverse-path: {}, Id: {}", mail.getMaybeSender().asString(), messageId);
        }
        
    } catch (MessagingException me) {
        LOGGER.error("MessagingException found - could not sign!", me);
        throw me;
    } catch (Exception e) {
        LOGGER.error("Exception found", e);
        throw new MessagingException("Exception thrown - could not sign!", e);
    }
    
}
 
Example 6
Source File: MimeMessageBuilder.java    From james-project with Apache License 2.0 4 votes vote down vote up
public MimeMessage build() throws MessagingException {
    Preconditions.checkState(!(text.isPresent() && content.isPresent()), "Can not get at the same time a text and a content");
    MimeMessage mimeMessage = new MimeMessage(Session.getInstance(new Properties()));
    if (text.isPresent()) {
        mimeMessage.setContent(text.get(), textContentType.orElse(DEFAULT_TEXT_PLAIN_UTF8_TYPE));
    }
    if (content.isPresent()) {
        mimeMessage.setContent(content.get());
    }
    if (sender.isPresent()) {
        mimeMessage.setSender(sender.get());
    }
    if (subject.isPresent()) {
        mimeMessage.setSubject(subject.get());
    }
    ImmutableList<InternetAddress> fromAddresses = from.build();
    if (!fromAddresses.isEmpty()) {
        mimeMessage.addFrom(fromAddresses.toArray(InternetAddress[]::new));
    }
    List<InternetAddress> toAddresses = to.build();
    if (!toAddresses.isEmpty()) {
        mimeMessage.setRecipients(Message.RecipientType.TO, toAddresses.toArray(InternetAddress[]::new));
    }
    List<InternetAddress> ccAddresses = cc.build();
    if (!ccAddresses.isEmpty()) {
        mimeMessage.setRecipients(Message.RecipientType.CC, ccAddresses.toArray(InternetAddress[]::new));
    }
    List<InternetAddress> bccAddresses = bcc.build();
    if (!bccAddresses.isEmpty()) {
        mimeMessage.setRecipients(Message.RecipientType.BCC, bccAddresses.toArray(InternetAddress[]::new));
    }

    MimeMessage wrappedMessage = MimeMessageWrapper.wrap(mimeMessage);

    List<Header> headerList = headers.build();
    for (Header header: headerList) {
        if (header.name.equals("Message-ID") || header.name.equals("Date")) {
            wrappedMessage.setHeader(header.name, header.value);
        } else {
            wrappedMessage.addHeader(header.name, header.value);
        }
    }
    wrappedMessage.saveChanges();

    return wrappedMessage;
}