Example usage for javax.mail.internet MimeMessage setText

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

Introduction

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

Prototype

@Override
public void setText(String text, String charset) throws MessagingException 

Source Link

Document

Convenience method that sets the given String as this part's content, with a MIME type of "text/plain" and the specified charset.

Usage

From source file:com.zimbra.cs.service.mail.SendVerificationCode.java

static void sendVerificationCode(String emailAddr, String code, Mailbox mbox)
        throws MessagingException, ServiceException {
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSmtpSession(mbox.getAccount()));
    mm.setRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(emailAddr));
    mm.setText(L10nUtil.getMessage(L10nUtil.MsgKey.deviceSendVerificationCodeText,
            mbox.getAccount().getLocale(), code), MimeConstants.P_CHARSET_UTF8);
    mm.saveChanges();/*from  w w w  .  j a  v  a2 s .co  m*/
    MailSender mailSender = mbox.getMailSender();
    mailSender.setSaveToSent(false);
    mailSender.sendMimeMessage(null, mbox, mm);
}

From source file:ru.org.linux.user.RegisterController.java

private static void sendEmail(Template tmpl, String nick, String email, boolean isNew)
        throws MessagingException {
    StringBuilder text = new StringBuilder();

    text.append("?!\n\n");
    if (isNew) {/*from w  ww.j ava  2  s. c o  m*/
        text.append(
                "\t   ? http://www.linux.org.ru/ ?? ?? ?,\n");
    } else {
        text.append(
                "\t   ? http://www.linux.org.ru/   ?? ?,\n");
    }

    text.append("     ? ? (e-mail).\n\n");
    text.append(
            "  ?    ? ? ?: '");
    text.append(nick);
    text.append("'\n\n");
    text.append(
            "?   ,     - ?  ? ?!\n\n");

    if (isNew) {
        text.append(
                "?     ???    ? http://www.linux.org.ru/,\n");
        text.append(
                "  ?  ? ?   ?    ?.\n\n");
    } else {
        text.append(
                "?      ? ? ? http://www.linux.org.ru/,\n");
        text.append("  ?  ? .\n\n");
    }

    String regcode = User.getActivationCode(tmpl.getSecret(), nick, email);

    text.append(
            "?    ?? http://www.linux.org.ru/activate.jsp\n\n");
    text.append(" : ").append(regcode).append("\n\n");
    text.append("  ?!\n");

    Properties props = new Properties();
    props.put("mail.smtp.host", "localhost");
    Session mailSession = Session.getDefaultInstance(props, null);

    MimeMessage emailMessage = new MimeMessage(mailSession);
    emailMessage.setFrom(new InternetAddress("no-reply@linux.org.ru"));

    emailMessage.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress(email));
    emailMessage.setSubject("Linux.org.ru registration");
    emailMessage.setSentDate(new Date());
    emailMessage.setText(text.toString(), "UTF-8");

    Transport.send(emailMessage);
}

From source file:org.tizzit.util.mail.MailHelper.java

/**
 * Sends an email in text-format in iso-8859-1-encoding
 * /*  w  ww .ja  va 2  s.  c  o m*/
 * @param subject the subject of the new mail
 * @param message the content of the mail
 * @param from the sender-address
 * @param to the receiver-address
 * @param cc the address of the receiver of a copy of this mail
 * @param bcc the address of the receiver of a blind-copy of this mail
 */
public static void sendMail(String subject, String message, String from, String to, String cc, String bcc) {
    try {
        MimeMessage msg = new MimeMessage(mailSession);
        msg.setFrom(new InternetAddress(from));
        if (cc != null && !cc.equals(""))
            msg.setRecipients(Message.RecipientType.CC, cc);
        if (bcc != null && !bcc.equals(""))
            msg.setRecipients(Message.RecipientType.BCC, bcc);
        msg.addRecipients(Message.RecipientType.TO, to);
        msg.setSubject(subject, "iso-8859-1");
        msg.setSentDate(new Date());
        msg.setText(message, "iso-8859-1");
        Transport.send(msg);
    } catch (Exception e) {
        log.error("Error sending mail: " + e.getLocalizedMessage());
    }
}

From source file:org.tizzit.util.mail.MailHelper.java

/**
 * Sends an email in text-format in iso-8859-1-encoding
 * /*from  w w  w. j a va 2 s .  co m*/
 * @param subject the subject of the new mail
 * @param message the content of the mail
 * @param from the sender-address
 * @param to the receiver-address(es)
 * @param cc the address of the receiver of a copy of this mail
 * @param bcc the address of the receiver of a blind-copy of this mail
 */
public static void sendMail(String subject, String message, String from, String[] to, String cc, String bcc) {
    try {
        MimeMessage msg = new MimeMessage(mailSession);
        msg.setFrom(new InternetAddress(from));
        if (cc != null && !cc.equals(""))
            msg.setRecipients(Message.RecipientType.CC, cc);
        if (bcc != null && !bcc.equals(""))
            msg.setRecipients(Message.RecipientType.BCC, bcc);
        for (int i = to.length - 1; i >= 0; i--) {
            msg.addRecipients(Message.RecipientType.TO, to[i]);
        }
        msg.setSubject(subject, "iso-8859-1");
        msg.setSentDate(new Date());
        msg.setText(message, "iso-8859-1");
        Transport.send(msg);
    } catch (Exception e) {
        log.error("Error sending mail: " + e.getLocalizedMessage());
    }
}

From source file:nu.mine.kino.jenkins.plugins.projectmanagement.utils.PMUtils.java

public static void sendMail(String[] addresses, String subject, String message)
        throws UnsupportedEncodingException, MessagingException {

    MimeMessage mimeMessage = new MimeMessage(Mailer.descriptor().createSession());
    String adminAddress = JenkinsLocationConfiguration.get().getAdminAddress();

    InternetAddress[] to = new InternetAddress[addresses.length];
    for (int i = 0; i < addresses.length; i++) {
        to[i] = new InternetAddress(addresses[i], true);
    }//from ww w  .  j  av  a  2  s .  c  om
    mimeMessage.setSender(new InternetAddress(adminAddress));
    mimeMessage.setRecipients(Message.RecipientType.TO, to);
    mimeMessage.setSubject(subject, "ISO-2022-JP");
    mimeMessage.setText(message, "ISO-2022-JP");
    Transport.send(mimeMessage);
}

From source file:org.apache.james.transport.mailets.SMIMEDecrypt.java

/**
 * @see org.apache.mailet.Mailet#service(org.apache.mailet.Mail)
 *//* w  w  w  .j a  v a2  s  .c  o m*/
@SuppressWarnings("unchecked")
public void service(Mail mail) throws MessagingException {
    MimeMessage message = mail.getMessage();
    Part strippedMessage = null;
    log("Starting message decryption..");
    if (message.isMimeType("application/x-pkcs7-mime") || message.isMimeType("application/pkcs7-mime")) {
        try {
            SMIMEEnveloped env = new SMIMEEnveloped(message);
            RecipientInformationStore informationStore = env.getRecipientInfos();
            Collection<RecipientInformation> recipients = informationStore.getRecipients();
            for (RecipientInformation info : recipients) {
                RecipientId id = info.getRID();
                if (id.match(certificateHolder)) {
                    try {
                        JceKeyTransEnvelopedRecipient recipient = new JceKeyTransEnvelopedRecipient(
                                keyHolder.getPrivateKey());
                        // strippedMessage contains the decrypted message.
                        strippedMessage = SMIMEUtil.toMimeBodyPart(info.getContent(recipient));
                        log("Encrypted message decrypted");
                    } catch (Exception e) {
                        throw new MessagingException("Error during the decryption of the message", e);
                    }
                } else {
                    log("Found an encrypted message but it isn't encrypted for the supplied key");
                }
            }
        } catch (CMSException e) {
            throw new MessagingException("Error during the decryption of the message", e);
        }
    }

    // if the decryption has been successful..
    if (strippedMessage != null) {
        // I put the private key's public certificate as a mailattribute.
        // I create a list of certificate because I want to minic the
        // behavior of the SMIMEVerifySignature mailet. In that way
        // it is possible to reuse the same matchers to analyze
        // the result of the operation.
        ArrayList<X509Certificate> list = new ArrayList<X509Certificate>(1);
        list.add(keyHolder.getCertificate());
        mail.setAttribute(mailAttribute, list);

        // I start the message stripping.
        try {
            MimeMessage newMessage = new MimeMessage(message);
            newMessage.setText(text(strippedMessage), Charsets.UTF_8.name());
            if (!strippedMessage.isMimeType("multipart/*")) {
                newMessage.setDisposition(null);
            }
            newMessage.saveChanges();
            mail.setMessage(newMessage);
        } catch (IOException e) {
            log("Error during the strip of the encrypted message");
            throw new MessagingException("Error during the stripping of the encrypted message", e);
        }
    }
}

From source file:org.obm.imap.archive.services.MailerImpl.java

@Override
public void send(ObmDomain domain, ArchiveTreatmentRunId runId, State state, Mailing mailing)
        throws MessagingException, URISyntaxException {
    try {//w ww  .j a  va  2s  . c o m
        if (!mailing.getEmailAddresses().isEmpty()) {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(from(domain));
            message.addRecipients(RecipientType.TO, internetAddresses(mailing));
            message.setSubject("End of IMAP Archive for domain " + domain.getName());
            message.setText(text(domain, runId, state), Charsets.UTF_8.name());

            smtpService.sendEmail(message, session);
        }
    } catch (MessagingException | URISyntaxException e) {
        logger.error("Error when mailing", e);
        throw e;
    }
}

From source file:davmail.smtp.TestSmtp.java

public void testSendMessage() throws IOException, MessagingException, InterruptedException {
    String body = "Test message\r\n" + "Special characters: \r\n" + "Chinese: " + ((char) 0x604F)
            + ((char) 0x7D59);
    MimeMessage mimeMessage = new MimeMessage((Session) null);
    mimeMessage.addHeader("To", Settings.getProperty("davmail.to"));
    mimeMessage.setSubject("Test subject");
    mimeMessage.setText(body, "UTF-8");
    sendAndCheckMessage(mimeMessage);/*www  .j  av  a  2s.co  m*/
}

From source file:de.blizzy.documentr.mail.SubscriptionMailer.java

private void sendMail(String subject, String text, String senderEmail, String senderName,
        Set<String> subscriberEmails, JavaMailSender sender) {

    for (String subscriberEmail : subscriberEmails) {
        try {/*  w  w  w  . java2  s. co  m*/
            MimeMessage msg = sender.createMimeMessage();
            msg.setFrom(createAddress(senderEmail, senderName));
            msg.setRecipient(RecipientType.TO, createAddress(subscriberEmail, null));
            msg.setSubject(subject, Charsets.UTF_8.name());
            msg.setText(text, Charsets.UTF_8.name());
            sender.send(msg);
        } catch (MessagingException e) {
            log.error("error while sending mail", e); //$NON-NLS-1$
        }
    }
}

From source file:ru.org.linux.util.EmailService.java

public void sendEmail(String nick, String email, boolean isNew) throws MessagingException {
    StringBuilder text = new StringBuilder();

    text.append("?!\n\n");
    if (isNew) {// w  w w .ja  va  2s.  c om
        text.append(
                "\t?   ? http://www.linux.org.ru/ ?? ?? ?,\n");
    } else {
        text.append(
                "\t?   ? http://www.linux.org.ru/   ?? ?,\n");
    }

    text.append("     ? ? (e-mail).\n\n");
    text.append(
            "  ?    ? ? ?: '");
    text.append(nick);
    text.append("'\n\n");
    text.append(
            "?   ,     - ?  ? ?!\n\n");

    if (isNew) {
        text.append(
                "?     ???    ? http://www.linux.org.ru/,\n");
        text.append(
                "  ?  ? ?   ?    ?.\n\n");
    } else {
        text.append(
                "?      ? ? ? http://www.linux.org.ru/,\n");
        text.append("  ?  ? .\n\n");
    }

    String regcode = User.getActivationCode(configuration.getSecret(), nick, email);

    text.append(
            "?    ?? http://www.linux.org.ru/activate.jsp\n\n");
    text.append(" : ").append(regcode).append("\n\n");
    text.append("  ?!\n");

    Properties props = new Properties();
    props.put("mail.smtp.host", "localhost");
    Session mailSession = Session.getDefaultInstance(props, null);

    MimeMessage emailMessage = new MimeMessage(mailSession);
    emailMessage.setFrom(new InternetAddress("no-reply@linux.org.ru"));

    emailMessage.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress(email));
    emailMessage.setSubject("Linux.org.ru registration");
    emailMessage.setSentDate(new Date());
    emailMessage.setText(text.toString(), "UTF-8");

    Transport.send(emailMessage);
}