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) throws MessagingException 

Source Link

Document

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

Usage

From source file:ch.entwine.weblounge.kernel.mail.SmtpService.java

/**
 * Method to send a test message.//  w ww.j  av a2  s. c o m
 * 
 * @throws MessagingException
 *           if sending the message failed
 */
private void sendTestMessage(String recipient) throws MessagingException {
    MimeMessage message = createMessage();
    message.addRecipient(RecipientType.TO, new InternetAddress(recipient));
    message.setSubject("Test from Weblounge");
    message.setText("Hello world");
    message.saveChanges();
    send(message);
}

From source file:mx.unam.pixel.controller.MailController.java

public void enviaMensaje(String mensaje) {
    try {//www.ja  va2  s  .c  om
        Properties props = new Properties();

        // Nombre del host de correo, es smtp.gmail.com
        props.setProperty("mail.smtp.host", "smtp.gmail.com");

        // TLS si est disponible
        props.setProperty("mail.smtp.starttls.enable", "true");

        // Puerto de gmail para envio de correos
        props.setProperty("mail.smtp.port", "587");

        // Nombre del usuario
        props.setProperty("mail.smtp.user", "ejemplo@gmail.com");

        // Si requiere o no usuario y password para conectarse.
        props.setProperty("mail.smtp.auth", "true");

        // Preparamos la sesion
        Session session = Session.getDefaultInstance(props);
        //session.setDebug(true);

        //Construimos el mensage
        MimeMessage message = new MimeMessage(session);
        InternetAddress cuenta = new InternetAddress("enrique.wps@gmail.com");
        message.setFrom(); //Correo electronico que manda el mensaja
        message.addRecipient(Message.RecipientType.TO, new InternetAddress("vampa@ciencias.unam.mx"));
        message.setSubject("Test MUFFIN");
        message.setText("Test Muffin Cuerpo");
        // Abrimos la comunicacion 
        Transport t = session.getTransport("smtp");

        t.connect("dasds@gmail.com", "asdsadas"); // Ususario y contrasea
        t.sendMessage(message, message.getAllRecipients());
        // Cierre
        t.close();

    } catch (AddressException ex) {
        System.err.println("er");
    } catch (MessagingException ex) {
        Logger.getLogger(MailController.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:com.bia.yahoomailjava.YahooMailService.java

/**
 *
 * @param addressTo/*from w  w  w  .ja  v  a2  s.  c o  m*/
 * @param subject
 * @param message
 *
 */
private void send(InternetAddress[] addressTo, String subject, String message) {
    try {
        MimeMessage msg = new MimeMessage(createSession());
        msg.setFrom(new InternetAddress(USERNAME));
        msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
        msg.setSubject(subject);
        msg.setText(message);
        Transport.send(msg);
    } catch (Exception ex) {
        //logger.warn(ex.getMessage(), ex);
        throw new RuntimeException(ex);
    }
}

From source file:davmail.smtp.TestSmtp.java

public void testInvalidFrom() throws IOException, MessagingException, InterruptedException {
    String body = "Test message";
    MimeMessage mimeMessage = new MimeMessage((Session) null);
    mimeMessage.addHeader("From", "guessant@loca.net");
    mimeMessage.addHeader("To", Settings.getProperty("davmail.to"));
    mimeMessage.setSubject("Test subject");
    mimeMessage.setText(body);
    sendAndCheckMessage(mimeMessage, "guessant@loca.net", null);
}

From source file:org.modelibra.util.Emailer.java

/**
 * Sends an email./*ww w.j av a  2  s  .co m*/
 * 
 * @throws dmLite
 *             exception if there is a problem
 */
public void send() throws ModelibraException {
    try {
        MimeMessage message = new MimeMessage(emailSession);
        InternetAddress fromIA = new InternetAddress(from);
        message.setFrom(fromIA);
        InternetAddress toIA = new InternetAddress(to);
        message.addRecipient(Message.RecipientType.TO, toIA);
        message.setSubject(subject);
        message.setText(content);
        emailStore.connect(outServer, code, password);
        Transport.send(message);
        emailStore.close();
    } catch (MessagingException e) {
        throw new ModelibraException("Could not send an email: " + e.getMessage());
    } catch (IllegalStateException e) {
        throw new ModelibraException("Could not send an email: " + e.getMessage());
    }
}

From source file:ste.xtest.mail.BugFreeFileTransport.java

private void sendSimpleMessage(String from, String to, String subject, String body) throws Exception {
    Session session = Session.getInstance(config);

    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
    message.setSubject(subject);/*from   ww  w. j a  v  a 2  s .com*/
    message.setText(body);

    session.getTransport().sendMessage(message, message.getAllRecipients());
}

From source file:org.apache.james.protocols.smtp.netty.NettyStartTlsSMTPServerTest.java

@Test
public void startTlsShouldWorkWhenUsingJavamail() throws Exception {
    TestMessageHook hook = new TestMessageHook();
    server = createServer(createProtocol(Optional.<ProtocolHandler>of(hook)),
            Encryption.createStartTls(BogusSslContextFactory.getServerContext()));
    server.bind();//  w  w  w .ja va 2s.com
    SMTPTransport transport = null;

    try {
        InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress();

        Properties mailProps = new Properties();
        mailProps.put("mail.smtp.from", "test@localhost");
        mailProps.put("mail.smtp.host", bindedAddress.getHostName());
        mailProps.put("mail.smtp.port", bindedAddress.getPort());
        mailProps.put("mail.smtp.socketFactory.class", BogusSSLSocketFactory.class.getName());
        mailProps.put("mail.smtp.socketFactory.fallback", "false");
        mailProps.put("mail.smtp.starttls.enable", "true");

        Session mailSession = Session.getDefaultInstance(mailProps);

        InternetAddress[] rcpts = new InternetAddress[] { new InternetAddress("valid@localhost") };
        MimeMessage message = new MimeMessage(mailSession);
        message.setFrom(new InternetAddress("test@localhost"));
        message.setRecipients(Message.RecipientType.TO, rcpts);
        message.setSubject("Testmail", "UTF-8");
        message.setText("Test.....");

        transport = (SMTPTransport) mailSession.getTransport("smtps");

        transport.connect(new Socket(bindedAddress.getHostName(), bindedAddress.getPort()));
        transport.sendMessage(message, rcpts);

        assertThat(hook.getQueued()).hasSize(1);
    } finally {
        if (transport != null) {
            transport.close();
        }
    }
}

From source file:org.josso.selfservices.password.EMailPasswordDistributor.java

protected void sendMail(final String mailTo, final String mailFrom, final String text)
        throws PasswordManagementException {

    MimeMessagePreparator preparator = new MimeMessagePreparator() {

        public void prepare(MimeMessage mimeMessage) throws Exception {
            mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
            mimeMessage.setFrom(new InternetAddress(mailFrom));
            mimeMessage.setSubject(getMailSubject());
            mimeMessage.setText(text);
        }/*from   ww w. ja va 2s  .  c  om*/
    };

    try {
        this.mailSender.send(preparator);
    } catch (MailException e) {
        throw new PasswordManagementException(
                "Cannot distribute password to [" + mailTo + "] " + e.getMessage(), e);
    }
}

From source file:com.pinterest.deployservice.email.SMTPMailManagerImpl.java

public void send(String to, String title, String message) throws Exception {
    Session session = Session.getDefaultInstance(properties, getAuthenticator());
    // Create a default MimeMessage object.
    MimeMessage mimeMessage = new MimeMessage(session);
    // Set From: header field of the header.
    mimeMessage.setFrom(new InternetAddress(adminAddress));
    // Set To: header field of the header.
    mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
    // Set Subject: header field
    mimeMessage.setSubject(title);//from   w  ww .  j  ava 2  s. c  o  m
    // Now set the actual message
    mimeMessage.setText(message);
    // Send message
    Transport.send(mimeMessage);
}

From source file:com.thinkbiganalytics.metadata.sla.SlaEmailService.java

/**
 * Send an email/*from  www  .ja v a 2  s  .co m*/
 *
 * @param to      the user(s) to send the email to
 * @param subject the subject of the email
 * @param body    the email body
 */
public void sendMail(String to, String subject, String body) {

    try {
        if (testConnection()) {
            MimeMessage message = mailSender.createMimeMessage();
            String fromAddress = StringUtils.defaultIfBlank(emailConfiguration.getFrom(),
                    emailConfiguration.getUsername());
            message.setFrom(new InternetAddress(fromAddress));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            message.setSubject(subject);
            message.setText(body);
            mailSender.send(message);
            log.debug("Email send to {}", to);
        }
    } catch (MessagingException ex) {
        log.error("Exception while sending mail : {}", ex.getMessage());
        Throwables.propagate(ex);

    }
}