List of usage examples for javax.mail.internet MimeMessage addRecipient
public void addRecipient(RecipientType type, Address address) throws MessagingException
From source file:com.norconex.jef4.mail.SimpleMailer.java
/** * Sends an email.//w w w .ja v a 2 s .co m * @param recipients email recipients ("To" field) * @param subject email subject * @param body email body (content) * @throws MessagingException problem sending email */ public final void send(final String[] recipients, final String subject, final String body) throws MessagingException { if (recipients == null || recipients.length == 0) { throw new IllegalArgumentException("No mail recipient provided."); } Session session = Session.getDefaultInstance(props, null); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(sender)); for (int i = 0; i < recipients.length; i++) { message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipients[i])); } message.setSubject(subject); message.setContent(body, contentType); Transport.send(message); }
From source file:gov.nih.nci.caintegrator.application.mail.SendMail.java
public synchronized void sendMail(String mailTo, String mailCC, String mailBody, String subject) throws ValidationException { try {/*from w w w . j a va 2 s .c om*/ if (mailTo != null && EmailValidator.getInstance().isValid(mailTo)) { //get system properties Properties props = System.getProperties(); String to = mailTo; // Set up mail server props.put("mail.smtp.host", MailConfig.getInstance(mailProperties).getHost()); //Get session Session session = Session.getDefaultInstance(props, null); //Define Message MimeMessage message = new MimeMessage(session); MailManager mailManager = new MailManager(mailProperties); message.setFrom(new InternetAddress(mailManager.formatFromAddress())); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); if ((mailCC != null) && EmailValidator.getInstance().isValid(mailCC)) message.addRecipient(Message.RecipientType.CC, new InternetAddress(mailCC)); message.setSubject(subject); message.setText(mailBody); //Send Message Transport.send(message); } else { throw new ValidationException("Invalid Email Address"); } } catch (Exception e) { logger.error("Send Mail error", e); } //catch }
From source file:org.opencastproject.kernel.mail.SmtpService.java
/** * Method to send a test message./*from w ww . j a va2s. 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 Matterhorn"); message.setText("Hello world"); message.saveChanges(); send(message); }
From source file:com.tdclighthouse.commons.mail.util.MailClient.java
public void sendMail(String from, String[] to, Mail mail) throws MessagingException, AddressException { // a brief validation if ((from == null) || "".equals(from) || (to.length == 0) || (mail == null)) { throw new IllegalArgumentException(); }/*from w w w . j av a 2s . co m*/ Session session = getSession(); // Define a new mail message MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); for (int i = 0; i < to.length; i++) { message.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i])); } message.setSubject(mail.getSubject(), "utf-8"); // use a MimeMultipart as we need to handle the file attachments Multipart multipart = new MimeMultipart("alternative"); if ((mail.getMessageBody() != null) && !"".equals(mail.getMessageBody())) { // add the message body to the mime message BodyPart textPart = new MimeBodyPart(); textPart.setContent(mail.getMessageBody(), "text/plain; charset=utf-8"); // sets type to "text/plain" multipart.addBodyPart(textPart); } if (mail.getHtmlBody() != null) { BodyPart pixPart = new MimeBodyPart(); pixPart.setContent(mail.getHtmlBody(), "text/html; charset=utf-8"); multipart.addBodyPart(pixPart); } // add any file attachments to the message addAtachments(mail.getAttachments(), multipart); // Put all message parts in the message message.setContent(multipart); // Send the message Transport.send(message); }
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);/*ww w. ja v a 2s . c om*/ // Now set the actual message mimeMessage.setText(message); // Send message Transport.send(mimeMessage); }
From source file:mx.unam.pixel.controller.MailController.java
public void enviaMensaje(String mensaje) { try {/* w w w . j av a2s . com*/ 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:mailhost.StartApp.java
public void sendEmail(String to, String subject, String body) throws UnsupportedEncodingException, MessagingException { System.out.println(String.format("Sending notification email recipients " + "to " + to + " subject " + subject + "host " + mailhost)); if (StringUtils.isBlank(to)) throw new IllegalArgumentException("The email request should have at least one recipient"); Properties properties = new Properties(); properties.setProperty("mail.smtp.host", mailhost); Session session = Session.getDefaultInstance(properties); Address[] a = new InternetAddress[1]; a[0] = new InternetAddress("test@matson.com"); MimeMessage message = new MimeMessage(session); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.addFrom(a);//from ww w .j a v a 2 s .c o m message.setSubject(subject); message.setContent(body, "text/html; charset=utf-8"); //message.setText(body); // Send message Transport.send(message); System.out.println("Email sent."); }
From source file:com.synyx.greetingcard.mail.OpenCmsMailService.java
public void sendMultipartMail(MessageConfig config, DataSource ds, String filename) throws MessagingException { log.debug("Sending multipart message " + config); Session session = getSession();/*from ww w. ja v a 2 s . c o m*/ MimeMultipart multipart = new MimeMultipart(); MimeBodyPart html = new MimeBodyPart(); html.setContent(config.getContent(), config.getContentType()); html.setHeader("MIME-Version", "1.0"); html.setHeader("Content-Type", html.getContentType()); multipart.addBodyPart(html); BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setDataHandler(new DataHandler(ds)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart); final MimeMessage message = new MimeMessage(session); message.setContent(multipart); try { message.setFrom(new InternetAddress(config.getFrom(), config.getFromName())); message.addRecipient(Message.RecipientType.TO, new InternetAddress(config.getTo(), config.getToName())); } catch (UnsupportedEncodingException ex) { throw new MessagingException("Setting from or to failed", ex); } message.setSubject(config.getSubject()); // we don't send in a new Thread so that we get the Exception Transport.send(message); }
From source file:ch.entwine.weblounge.kernel.mail.SmtpService.java
/** * Method to send a test message./*from w ww . j a v a 2 s . co 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:com.github.sleroy.junit.mail.server.test.MailSender.java
/** * Send mail.//from ww w . ja v a2 s . c om * * @param from * Sender's email ID needs to be mentioned * @param to * Recipient's email ID needs to be mentioned. * @param subject * the subject * @throws MessagingException */ public void sendMail(String from, String to, String subject, String body) throws MessagingException { // Get system properties Properties properties = System.getProperties(); // Setup mail server properties.setProperty("mail.smtp.host", host); properties.setProperty("mail.smtp.port", Integer.toString(port)); // Get the default Session object. Session session = Session.getDefaultInstance(properties); Transport transport = null; try { transport = session.getTransport(); // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(from)); // Set To: header field of the header. message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: header field message.setSubject(subject); // Now set the actual message message.setText(body); // Send message transport.send(message); System.out.println("Sent message successfully...."); } finally { if (transport != null) { transport.close(); } } }