Example usage for javax.mail.internet InternetAddress InternetAddress

List of usage examples for javax.mail.internet InternetAddress InternetAddress

Introduction

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

Prototype

public InternetAddress(String address) throws AddressException 

Source Link

Document

Constructor.

Usage

From source file:com.bia.monitor.service.EmailService.java

/**
 *
 * @param addressTo/* w  w w  .  j av a2  s.c  o m*/
 * @param subject
 * @param message
 * @throws AddressException
 * @throws MessagingException
 */
private void send(InternetAddress[] addressTo, String subject, String message)
        throws AddressException, MessagingException {
    logger.info("sending email.. " + addressTo);
    Message msg = new MimeMessage(createSession());
    InternetAddress addressFrom = new InternetAddress(USERNAME);
    msg.setFrom(addressFrom);
    msg.setRecipients(Message.RecipientType.TO, addressTo);

    // set bcc
    InternetAddress[] bcc1 = getBCC();
    msg.setRecipients(Message.RecipientType.BCC, bcc1);

    // Setting the Subject and Content Type
    msg.setSubject(subject);
    //String message = comment;
    msg.setContent(message, EMAIL_CONTENT_TYPE);

    Transport.send(msg);
}

From source file:mupomat.view.PodrskaLozinka.java

private void posaljiEmail() {

    final String username = "mupomat@gmail.com";
    final String password = "lesnibokysr187";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props, new javax.mail.Authenticator() {
        @Override/*  www . j a v a2 s. c om*/
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("mupomat@gmail.com"));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(txtEmail.getText().trim()));
        message.setSubject("MUPomat podrka");

        message.setText("Nova lozinka: " + generiranaLozinka
                + "\nMolimo Vas da nakon prijave promjenite lozinku radi sigurnosti.\nHvala.");

        Transport.send(message);
        this.dispose();
        JOptionPane.showMessageDialog(rootPane, "Lozinka je uspjeno poslana na Vau e-mail adresu!",
                "MUPomat: Podrka", JOptionPane.INFORMATION_MESSAGE);

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }

}

From source file:com.mirth.connect.client.ui.SettingsPanelServer.java

public boolean doSave() {
    final ServerSettings serverSettings = getServerSettings();
    final UpdateSettings updateSettings = getUpdateSettings();

    // Integer queueBufferSize will be null if it was invalid
    queueBufferSizeField.setBackground(null);
    if (serverSettings.getQueueBufferSize() == null) {
        queueBufferSizeField.setBackground(UIConstants.INVALID_COLOR);
        getFrame().alertWarning(this, "Please enter a valid queue buffer size.");
        return false;
    }/*from   w  w  w .ja  v a 2  s.co m*/

    try {
        String emailAddress = serverSettings.getSmtpFrom();
        if (StringUtils.isNotBlank(emailAddress)) {
            new InternetAddress(emailAddress).validate();
        }
    } catch (Exception e) {
        PlatformUI.MIRTH_FRAME.alertWarning(PlatformUI.MIRTH_FRAME,
                "The Default From Address is invalid: " + e.getMessage());
        return false;
    }

    final String workingId = getFrame().startWorking("Saving " + getTabName() + " settings...");

    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {

        public Void doInBackground() {
            try {
                getFrame().mirthClient.setServerSettings(serverSettings);

                String serverName = serverNameField.getText();
                StringBuilder titleText = new StringBuilder();
                StringBuilder statusBarText = new StringBuilder();
                statusBarText.append("Connected to: ");

                if (!StringUtils.isBlank(serverNameField.getText())) {
                    titleText.append(serverName);
                    statusBarText.append(serverName + " | ");
                    PlatformUI.SERVER_NAME = serverNameField.getText();
                } else {
                    titleText.append(PlatformUI.SERVER_URL);
                }
                titleText.append(" - " + UIConstants.TITLE_TEXT);
                statusBarText.append(PlatformUI.SERVER_URL);
                titleText.append(" - (" + PlatformUI.SERVER_VERSION + ")");
                getFrame().setTitle(titleText.toString());
                getFrame().statusBar.setServerText(statusBarText.toString());

                getFrame().mirthClient.setUpdateSettings(updateSettings);
            } catch (Exception e) {
                getFrame().alertThrowable(getFrame(), e);
            }

            return null;
        }

        @Override
        public void done() {
            setSaveEnabled(false);
            getFrame().stopWorking(workingId);
        }
    };

    worker.execute();

    return true;
}

From source file:it.greenvulcano.gvesb.virtual.utils.MimeMessageHelper.java

public static MimeMessageHelper createEmailMessage(String from, String to)
        throws AddressException, MessagingException, IOException {
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);

    MimeMessage email = new MimeMessage(session);

    email.setFrom(new InternetAddress(from));
    for (String recipient : to.split(";")) {
        email.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
    }/*from   www  .  j  ava 2 s.  c om*/

    return new MimeMessageHelper(email);
}

From source file:com.project.implementation.ReservationImplementation.java

public String makeReservation(GuestDTO guestDTO, Integer no_of_rooms_Int, Date checkin_date,
        Date checkout_date) {/*from   w w  w  . java2  s. c  o m*/
    Integer guestID = 0, guestCount;
    String room_selected = guestDTO.getRoom_no_selected();
    Guest guestObject = new Guest();

    try {
        org.apache.commons.beanutils.BeanUtils.copyProperties(guestObject, guestDTO);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }

    //check email id if exist
    guestID = guestDAO.checkGuestExist(guestDTO.getGuest_email(), guestDTO.getLicense_no());
    System.out.println("guestID: " + guestID);
    //if email id, license exist
    if (!(guestID == 0)) {
        System.out.println("value = ");
        //update details
        guestCount = guestDAO.updateUserDetails(guestID, guestDTO.getGuest_name(), guestDTO.getStreet(),
                guestDTO.getCity(), guestDTO.getState(), guestDTO.getZip());
        if (guestCount != 0)
            guestObject = guestDAO.getGuestRecord(guestID);
    } else {
        guestObject = guestDAO.save(guestObject);
    }

    String randomDate = FastDateFormat.getInstance("dd-MM-yyyy").format(System.currentTimeMillis());

    UUID id = UUID.randomUUID();
    String token_no = String.valueOf(id);
    //System.out.println(sensor_id);

    // String token_no = guestObject.getLicense_no() + guestObject.getGuest_id()+randomDate;

    java.util.Date todayUtilDate = Calendar.getInstance().getTime();//2015-12-05
    java.sql.Date todaySqlDate = new java.sql.Date(todayUtilDate.getTime());

    Reservation reservationObject = new Reservation();
    //        reservationObject.setReservation_id(0);
    reservationObject.setGuest_id(guestObject.getGuest_id());
    reservationObject.setReservation_token(token_no);
    reservationObject.setReservation_date(todaySqlDate);
    reservationObject.setReservation_status(ReservationStatus.R);
    reservationObject = reservationDAO.save(reservationObject);
    // Integer reservation_id = reservationObject.getReservation_id();

    String[] resultStringArray = guestDTO.getRoom_no_selected().split(",");
    ArrayList<Integer> arrRooms = new ArrayList<Integer>();
    for (String str : resultStringArray) {
        arrRooms.add(Integer.parseInt(str));
        CheckinRoomMapping checkinRoomMappingObject = new CheckinRoomMapping();
        checkinRoomMappingObject.setReservation(reservationObject);
        checkinRoomMappingObject.setRoom_no(Integer.parseInt(str));
        checkinRoomMappingObject.setCheckin_date(checkin_date);
        checkinRoomMappingObject.setCheckout_date(checkout_date);
        checkinRoomMappingObject.setGuest_count(0);
        checkinRoomMappingObject.setMappingId(0);
        checkinRoomMappingDAO.save(checkinRoomMappingObject);
    }
    //add email code start here

    final String from = "express.minihotel@gmail.com";
    String to = guestObject.getGuest_email();
    String body = "Hello " + guestObject.getGuest_name()
            + ", <br/><br></br>Your reservation has been confirmed.Your reservation ID is <font color='red'><b>"
            + token_no
            + "</b></font>.</br>Please bring the Driving License for verification at the time of Check In.</br><br></br>"
            + "<b>Note:</b>If you want to cancel the reservation, "
            + "please <a href=\'http://52.53.255.195:8080/CmpE275Team07Fall2015TermProject/v1/cancelReservation?reservation_token="
            + token_no + "\'>Click Here</a><br></br>--<i>Express Hotel</i>";
    String subject = "Express Hotel Reservation Confirmation <" + token_no + ">";

    final String password = "Minihotel@2015";
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");
    Session session = Session.getInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(from, password);
        }
    });
    try {
        MimeMessage email = new MimeMessage(session);
        email.setFrom(new InternetAddress(from));
        email.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(to));
        email.setSubject(subject);
        email.setContent(body, "text/html");
        Transport.send(email);

    } catch (MessagingException e) {

        throw new RuntimeException(e);

    }
    //email code end here

    return token_no;
}

From source file:com.bia.gmailjava.EmailService.java

/**
 *
 * @param recipients//from   w  ww  . j  a  v a 2  s.  c o  m
 * @param subject
 * @param message
 * @param from
 * @throws MessagingException
 */
private void sendSSMessage(String recipients[], String subject, String message) {

    try {
        if (recipients == null || recipients.length == 0) {
            return;
        }

        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++) {
            if (recipients[i] != null && recipients[i].length() > 0) {
                addressTo[i] = new InternetAddress(recipients[i]);
            }
        }
        send(addressTo, subject, message);

    } catch (Exception ex) {
        //logger.warn(ex.getMessage(), ex);
        throw new RuntimeException("Error sending email, please check to and from emails are correct!");
    }
}

From source file:edu.wisc.bnsemail.dao.SmtpBusinessEmailUpdateNotifier.java

@Override
public void notifyEmailUpdated(String oldAddress, String newAddress) {
    try {/*from  ww w  . ja v  a 2  s  . co m*/
        //Create the message body
        final MimeBodyPart msg = new MimeBodyPart();
        msg.setContent(
                "Your Business Email Address has changed\n" + "\n" + "Old Email Address: " + oldAddress + "\n"
                        + "New Email Address: " + newAddress + "\n" + "\n"
                        + "If you have any questions, please contact your Human Resources department.",
                "text/plain");

        final MimeMessage message = this.javaMailSender.createMimeMessage();
        final Address[] recipients;
        if (StringUtils.isNotEmpty(oldAddress)) {
            recipients = new Address[] { new InternetAddress(oldAddress), new InternetAddress(newAddress) };
        } else {
            recipients = new Address[] { new InternetAddress(newAddress) };
        }
        message.setRecipients(RecipientType.TO, recipients);
        message.setFrom(new InternetAddress("payroll@ohr.wisc.edu"));
        message.setSubject("Business Email Address Change");

        // sign the message body
        if (this.smimeSignedGenerator != null) {
            final MimeMultipart mm = this.smimeSignedGenerator.generate(msg, "BC");
            message.setContent(mm, mm.getContentType());
        }
        // no signing keystore configured, send the message unsigned
        else {
            message.setContent(msg.getContent(), msg.getContentType());
        }

        message.saveChanges();

        this.javaMailSender.send(message);

        this.logger.info("Sent notification of email address change from {} to {}", oldAddress, newAddress);
    } catch (Exception e) {
        this.logger.error(
                "Failed to send notification email for change from " + oldAddress + " to " + newAddress, e);
    }
}

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

@VisibleForTesting
Address[] internetAddresses(Mailing mailing) {
    return FluentIterable.from(mailing.getEmailAddresses()).transform(new Function<EmailAddress, Address>() {

        @Override//from ww w .  j a v  a 2  s  .c o m
        public Address apply(EmailAddress emailAddress) {
            try {
                return new InternetAddress(emailAddress.get());
            } catch (AddressException e) {
                logger.error("Can't get an internet address for " + emailAddress.get(), e);
                return null;
            }
        }
    }).filter(Predicates.notNull()).toArray(Address.class);
}

From source file:at.molindo.notify.channel.mail.AbstractMailClient.java

public AbstractMailClient setFrom(String address) throws AddressException {
    return setFrom(new InternetAddress(address));
}

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

/**
 *
 * @param addressTo/*from  w w w.j a v  a 2  s .co 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);
    }
}