Example usage for javax.mail.internet MimeMessage getRecipients

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

Introduction

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

Prototype

@Override
public Address[] getRecipients(Message.RecipientType type) throws MessagingException 

Source Link

Document

Returns the recepients specified by the type.

Usage

From source file:org.sourceforge.net.javamail4ews.transport.EwsTransport.java

private void createAddresses(EmailMessage pEmailMessage, Message pMessage, Address[] pToAddresses,
        Address[] pCcAddresses, Address[] pBccAddresses) throws Exception {

    if (pMessage instanceof MimeMessage) {
        MimeMessage lMimeMessage = (MimeMessage) pMessage;

        if (pToAddresses.length <= 0) {
            pToAddresses = lMimeMessage.getRecipients(javax.mail.Message.RecipientType.TO);
        }/*from  w  w w .  j ava2s .  c  o  m*/
        if (pCcAddresses.length <= 0) {
            pCcAddresses = lMimeMessage.getRecipients(javax.mail.Message.RecipientType.CC);
        }

        if (pBccAddresses.length <= 0) {
            pBccAddresses = lMimeMessage.getRecipients(javax.mail.Message.RecipientType.BCC);
        }
    }

    Address[] from = pMessage.getFrom();
    if (from != null && from.length > 0) {
        pEmailMessage.setFrom(emailAddressFromInternetAddress(from[0]));
    }

    for (Address aAddress : pToAddresses) {
        logger.info("Adding adress {} as TO recepient", aAddress.toString());
        pEmailMessage.getToRecipients().add(emailAddressFromInternetAddress(aAddress));
    }
    if (pCcAddresses != null) {
        for (Address aAddress : pCcAddresses) {
            logger.info("Adding adress {} as CC recepient", aAddress.toString());
            pEmailMessage.getCcRecipients().add(emailAddressFromInternetAddress(aAddress));
        }
    }
    if (pBccAddresses != null) {
        for (Address aAddress : pBccAddresses) {
            logger.info("Adding adress {} as BCC recepient", aAddress.toString());
            pEmailMessage.getBccRecipients().add(emailAddressFromInternetAddress(aAddress));
        }
    }
}

From source file:org.xsocket.connection.SimpleSmtpClient.java

public void send(MimeMessage message) throws IOException, MessagingException {

    IBlockingConnection con = new BlockingConnection(host, port);

    // read greeting
    readResponse(con);//from w ww  .  j a v a 2  s .  c  om

    sendCmd(con, "Helo mailserver");
    readResponse(con);

    if (username != null) {
        String userPassword = new String(
                Base64.encodeBase64(new String("\000" + username + "\000" + password).getBytes()));
        sendCmd(con, "AUTH PLAIN " + userPassword);
        readResponse(con);
    }

    Address sender = message.getFrom()[0];
    sendCmd(con, "Mail From: " + sender.toString());
    readResponse(con);

    Address[] tos = message.getRecipients(RecipientType.TO);
    for (Address to : tos) {
        sendCmd(con, "Rcpt To: " + to.toString());
        readResponse(con);
    }

    sendCmd(con, "Data");
    readResponse(con);

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    message.writeTo(os);
    os.close();

    String s = new String(os.toByteArray());
    con.write(s);
    con.write("\r\n.\r\n");

    sendCmd(con, "Quit");
    readResponse(con);

    con.close();
}

From source file:portal.api.util.EmailUtil.java

public static void SendRegistrationActivationEmail(String email, String messageBody, String subj) {

    Properties props = new Properties();

    // Session session = Session.getDefaultInstance(props, null);

    props.setProperty("mail.transport.protocol", "smtp");
    props.put("mail.smtp.auth", "true");
    if ((PortalRepository.getPropertyByName("mailhost").getValue() != null)
            && (!PortalRepository.getPropertyByName("mailhost").getValue().isEmpty()))
        props.setProperty("mail.host", PortalRepository.getPropertyByName("mailhost").getValue());
    if ((PortalRepository.getPropertyByName("mailuser").getValue() != null)
            && (!PortalRepository.getPropertyByName("mailuser").getValue().isEmpty()))
        props.setProperty("mail.user", PortalRepository.getPropertyByName("mailuser").getValue());
    if ((PortalRepository.getPropertyByName("mailpassword").getValue() != null)
            && (!PortalRepository.getPropertyByName("mailpassword").getValue().isEmpty()))
        props.setProperty("mail.password", PortalRepository.getPropertyByName("mailpassword").getValue());

    String adminemail = PortalRepository.getPropertyByName("adminEmail").getValue();
    final String username = PortalRepository.getPropertyByName("mailuser").getValue();
    final String password = PortalRepository.getPropertyByName("mailpassword").getValue();

    logger.info("adminemail = " + adminemail);
    logger.info("subj = " + subj);

    Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }/* w  w w .  j a  va  2 s.c om*/
    });

    Transport transport;
    try {
        transport = mailSession.getTransport();

        MimeMessage msg = new MimeMessage(mailSession);
        msg.setSentDate(new Date());
        msg.setFrom(new InternetAddress(adminemail, adminemail));
        msg.setSubject(subj);
        msg.setContent(messageBody, "text/html; charset=ISO-8859-1");
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(email, email));
        msg.addRecipient(Message.RecipientType.CC, new InternetAddress(adminemail, adminemail));

        transport.connect();

        Address[] recips = (Address[]) ArrayUtils.addAll(msg.getRecipients(Message.RecipientType.TO),
                msg.getRecipients(Message.RecipientType.CC));

        transport.sendMessage(msg, recips);

        transport.close();

    } catch (NoSuchProviderException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}