List of usage examples for javax.mail Message setRecipients
public abstract void setRecipients(RecipientType type, Address[] addresses) throws MessagingException;
From source file:sendhtml.java
public sendhtml(String[] argv) { String to, subject = null, from = null, cc = null, bcc = null, url = null; String mailhost = null;//from w ww . j a v a 2 s . c om String mailer = "sendhtml"; String protocol = null, host = null, user = null, password = null; String record = null; // name of folder in which to record mail boolean debug = false; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int optind; for (optind = 0; optind < argv.length; optind++) { if (argv[optind].equals("-T")) { protocol = argv[++optind]; } else if (argv[optind].equals("-H")) { host = argv[++optind]; } else if (argv[optind].equals("-U")) { user = argv[++optind]; } else if (argv[optind].equals("-P")) { password = argv[++optind]; } else if (argv[optind].equals("-M")) { mailhost = argv[++optind]; } else if (argv[optind].equals("-f")) { record = argv[++optind]; } else if (argv[optind].equals("-s")) { subject = argv[++optind]; } else if (argv[optind].equals("-o")) { // originator from = argv[++optind]; } else if (argv[optind].equals("-c")) { cc = argv[++optind]; } else if (argv[optind].equals("-b")) { bcc = argv[++optind]; } else if (argv[optind].equals("-L")) { url = argv[++optind]; } else if (argv[optind].equals("-d")) { debug = true; } else if (argv[optind].equals("--")) { optind++; break; } else if (argv[optind].startsWith("-")) { System.out.println("Usage: sendhtml [[-L store-url] | [-T prot] [-H host] [-U user] [-P passwd]]"); System.out.println("\t[-s subject] [-o from-address] [-c cc-addresses] [-b bcc-addresses]"); System.out.println("\t[-f record-mailbox] [-M transport-host] [-d] [address]"); System.exit(1); } else { break; } } try { if (optind < argv.length) { // XXX - concatenate all remaining arguments to = argv[optind]; System.out.println("To: " + to); } else { System.out.print("To: "); System.out.flush(); to = in.readLine(); } if (subject == null) { System.out.print("Subject: "); System.out.flush(); subject = in.readLine(); } else { System.out.println("Subject: " + subject); } Properties props = System.getProperties(); // XXX - could use Session.getTransport() and Transport.connect() // XXX - assume we're using SMTP if (mailhost != null) props.put("mail.smtp.host", mailhost); // Get a Session object Session session = Session.getInstance(props, null); if (debug) session.setDebug(true); // construct the message Message msg = new MimeMessage(session); if (from != null) msg.setFrom(new InternetAddress(from)); else msg.setFrom(); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false)); if (cc != null) msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false)); if (bcc != null) msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false)); msg.setSubject(subject); collect(in, msg); msg.setHeader("X-Mailer", mailer); msg.setSentDate(new Date()); // send the thing off Transport.send(msg); System.out.println("\nMail was sent successfully."); // Keep a copy, if requested. if (record != null) { // Get a Store object Store store = null; if (url != null) { URLName urln = new URLName(url); store = session.getStore(urln); store.connect(); } else { if (protocol != null) store = session.getStore(protocol); else store = session.getStore(); // Connect if (host != null || user != null || password != null) store.connect(host, user, password); else store.connect(); } // Get record Folder. Create if it does not exist. Folder folder = store.getFolder(record); if (folder == null) { System.err.println("Can't get record folder."); System.exit(1); } if (!folder.exists()) folder.create(Folder.HOLDS_MESSAGES); Message[] msgs = new Message[1]; msgs[0] = msg; folder.appendMessages(msgs); System.out.println("Mail was recorded successfully."); } } catch (Exception e) { e.printStackTrace(); } }
From source file:com.waveerp.sendMail.java
public void sendMsgSSL(String strSource, String strSourceDesc, String strSubject, String strMsg, String strDestination, String strDestDesc) throws Exception { // Call the registry management system registrySystem rs = new registrySystem(); // Call the encryption management system desEncryption de = new desEncryption(); de.Encrypter("", ""); String strHost = rs.readRegistry("NA", "NA", "NA", "EMAILHOST"); String strPort = rs.readRegistry("NA", "NA", "NA", "EMAILPORT"); final String strUser = rs.readRegistry("NA", "NA", "NA", "EMAILUSER"); String strPass = rs.readRegistry("NA", "NA", "NA", "EMAILPASSWORD"); //Decrypt the encrypted password. final String strPass01 = de.decrypt(strPass); Properties props = new Properties(); props.put("mail.smtp.host", strHost); props.put("mail.smtp.socketFactory.port", strPort); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", strPort); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(strUser, strPass01); }/* w w w. j av a2 s.c om*/ }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(strSource)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(strDestination)); message.setSubject(strSubject); message.setText(strMsg); Transport.send(message); //System.out.println("Done"); } catch (MessagingException e) { throw new RuntimeException(e); } //log(DEBUG, strHost + "|" + strPort + "|" + strUser + "|" + strPass ); }
From source file:sendhtml.java
public sendhtml(String[] argv) { String to, subject = null, from = null, cc = null, bcc = null, url = null; String mailhost = null;//from www .j ava 2 s.c o m String mailer = "sendhtml"; String protocol = null, host = null, user = null, password = null; String record = null; // name of folder in which to record mail boolean debug = false; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int optind; for (optind = 0; optind < argv.length; optind++) { if (argv[optind].equals("-T")) { protocol = argv[++optind]; } else if (argv[optind].equals("-H")) { host = argv[++optind]; } else if (argv[optind].equals("-U")) { user = argv[++optind]; } else if (argv[optind].equals("-P")) { password = argv[++optind]; } else if (argv[optind].equals("-M")) { mailhost = argv[++optind]; } else if (argv[optind].equals("-f")) { record = argv[++optind]; } else if (argv[optind].equals("-s")) { subject = argv[++optind]; } else if (argv[optind].equals("-o")) { // originator from = argv[++optind]; } else if (argv[optind].equals("-c")) { cc = argv[++optind]; } else if (argv[optind].equals("-b")) { bcc = argv[++optind]; } else if (argv[optind].equals("-L")) { url = argv[++optind]; } else if (argv[optind].equals("-d")) { debug = true; } else if (argv[optind].equals("--")) { optind++; break; } else if (argv[optind].startsWith("-")) { System.out.println("Usage: sendhtml [[-L store-url] | [-T prot] [-H host] [-U user] [-P passwd]]"); System.out.println("\t[-s subject] [-o from-address] [-c cc-addresses] [-b bcc-addresses]"); System.out.println("\t[-f record-mailbox] [-M transport-host] [-d] [address]"); System.exit(1); } else { break; } } try { if (optind < argv.length) { // XXX - concatenate all remaining arguments to = argv[optind]; System.out.println("To: " + to); } else { System.out.print("To: "); System.out.flush(); to = in.readLine(); } if (subject == null) { System.out.print("Subject: "); System.out.flush(); subject = in.readLine(); } else { System.out.println("Subject: " + subject); } Properties props = System.getProperties(); // XXX - could use Session.getTransport() and Transport.connect() // XXX - assume we're using SMTP if (mailhost != null) props.put("mail.smtp.host", mailhost); // Get a Session object Session session = Session.getInstance(props, null); if (debug) session.setDebug(true); // construct the message Message msg = new MimeMessage(session); if (from != null) msg.setFrom(new InternetAddress(from)); else msg.setFrom(); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false)); if (cc != null) msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false)); if (bcc != null) msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false)); msg.setSubject(subject); collect(in, msg); msg.setHeader("X-Mailer", mailer); msg.setSentDate(new Date()); // send the thing off Transport.send(msg); System.out.println("\nMail was sent successfully."); // Keep a copy, if requested. if (record != null) { // Get a Store object Store store = null; if (url != null) { URLName urln = new URLName(url); store = session.getStore(urln); store.connect(); } else { if (protocol != null) store = session.getStore(protocol); else store = session.getStore(); // Connect if (host != null || user != null || password != null) store.connect(host, user, password); else store.connect(); } // Get record Folder. Create if it does not exist. Folder folder = store.getFolder(record); if (folder == null) { System.err.println("Can't get record folder."); System.exit(1); } if (!folder.exists()) folder.create(Folder.HOLDS_MESSAGES); Message[] msgs = new Message[1]; msgs[0] = msg; folder.appendMessages(msgs); System.out.println("Mail was recorded successfully."); } } catch (Exception e) { e.printStackTrace(); } }
From source file:gov.nih.nci.cacisweb.util.CaCISUtil.java
/** * Sends email by setting some of the email properties that are common to Secure Email and XDS/NAV * //from ww w . ja v a2 s . co m * @param host * @param port * @param protocol * @param senderEmail * @param senderUser * @param senderPassword * @param subject * @param message * @param recepientEmail * @throws EmailException */ public void sendEmail(String host, String port, String senderEmail, String senderUser, String senderPassword, String subject, String body, String recepientEmail) throws MessagingException { log.debug("sendEmail(String host, String port, String protocol, String senderEmail, String senderUser," + "String senderPassword, String subject, String message, String recepientEmail) - start"); final String username = senderUser; final String password = senderPassword; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", host); props.put("mail.smtp.port", port); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); Message message = new MimeMessage(session); message.setFrom(new InternetAddress(senderEmail)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recepientEmail)); message.setSubject(subject); message.setText(body); Transport.send(message); System.out.println("Done"); // Email email = new SimpleEmail(); // email.setHostName(host); // email.setSmtpPort(port); // // email.setAuthentication(senderUser, senderPassword); // email.setAuthenticator(new DefaultAuthenticator(senderUser, senderPassword)); // email.addTo(recepientEmail); // email.setFrom(senderEmail); // email.setSubject(subject); // email.setMsg(message); // //email.setSSL(true); // log.info(String.format("Sending Email to %s, to report successful setup.", recepientEmail)); // email.send(); log.debug("sendEmail(String host, String port, String protocol, String senderEmail, String senderUser," + "String senderPassword, String subject, String message, String recepientEmail) - end"); }
From source file:com.waveerp.sendMail.java
public void sendMsgTLS(String strSource, String strSourceDesc, String strSubject, String strMsg, String strDestination, String strDestDesc) throws Exception { // Call the registry management system registrySystem rs = new registrySystem(); // Call the encryption management system desEncryption de = new desEncryption(); de.Encrypter("", ""); String strHost = rs.readRegistry("NA", "NA", "NA", "EMAILHOST"); String strPort = rs.readRegistry("NA", "NA", "NA", "EMAILPORT"); final String strUser = rs.readRegistry("NA", "NA", "NA", "EMAILUSER"); String strPass = rs.readRegistry("NA", "NA", "NA", "EMAILPASSWORD"); //Decrypt the encrypted password. final String strPass01 = de.decrypt(strPass); Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", strHost); props.put("mail.smtp.port", strPort); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(strUser, strPass01); }/* w w w . j a v a 2 s.c o m*/ }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(strSource)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(strDestination)); message.setSubject(strSubject); message.setText(strMsg); Transport.send(message); //System.out.println("Done"); } catch (MessagingException e) { throw new RuntimeException(e); } log(DEBUG, strHost + "|" + strPort + "|" + strUser + "|" + strPass); //Email email = new SimpleEmail(); //email.setHostName(strHost); //email.setSmtpPort( Integer.parseInt(strPort) ); //email.setAuthenticator(new DefaultAuthenticator(strUser, strPass01)); //email.setTLS(true); //email.setFrom(strSource, strSourceDesc); //email.setSubject(strSubject); //email.setMsg(strMsg); //email.addTo(strDestination, strDestDesc); //email.send(); }
From source file:it.infn.ct.security.actions.ActivateAccount.java
private void sendMail(LDAPUser user, boolean enabled) throws MailException { javax.mail.Session session = null;//from w ww. j av a 2 s.c o m try { Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); session = (javax.mail.Session) envCtx.lookup("mail/Users"); } catch (Exception ex) { _log.error("Mail resource lookup error"); _log.error(ex.getMessage()); throw new MailException("Mail Resource not available"); } Message mailMsg = new MimeMessage(session); try { mailMsg.setFrom(new InternetAddress(mailFrom, idPAdmin)); InternetAddress mailTos[] = new InternetAddress[1]; mailTos[0] = new InternetAddress(user.getPreferredMail()); mailMsg.setRecipients(Message.RecipientType.TO, mailTos); _log.error("mail bcc: " + mailBCC); String ccMail[] = mailBCC.split(";"); InternetAddress mailCCopy[] = new InternetAddress[ccMail.length]; for (int i = 0; i < ccMail.length; i++) { mailCCopy[i] = new InternetAddress(ccMail[i]); } mailMsg.setRecipients(Message.RecipientType.BCC, mailCCopy); mailMsg.setSubject(mailSubject); mailBody = mailBody.replaceAll("_USER_", user.getTitle() + " " + user.getGivenname() + " " + user.getSurname() + " (" + user.getUsername() + ")"); if (enabled) { mailBody = mailBody.replace("_RESULT_", "accepted"); } else { mailBody = mailBody.replace("_RESULT_", "denied"); } mailMsg.setText(mailBody); Transport.send(mailMsg); } catch (UnsupportedEncodingException ex) { _log.error(ex); throw new MailException("Mail address format not valid"); } catch (MessagingException ex) { _log.error(ex); throw new MailException("Mail message has problems"); } }
From source file:br.com.cgcop.administrativo.modelo.ContaEmail.java
public void enviarEmailHtml(List<String> destinos, String msg, String titulo) { // Recipient's email ID needs to be mentioned. String to = ""; String rodape = "<br/><br/><br/><br/> <div style=\"border-top: 1px dashed #c8cdbe;border-top: 1px dashed #c8cdbe \">" + "Esta mensagem uma notificao enviada automaticamente por tanto no deve ser respondida. <br/> " + "<span style=\"font-style: italic; font-family: Narrow; font-size: large; color: rgb(0, 153, 0);\">Sistema de Gesto de Projetos e Obras - SGPO</span><br/>" + "<span style=\"font-size: large; font-style: italic; color: rgb(0, 153, 0); font-family: Narrow;\">Oiti Engenharia e Gesto Ambiental</span>" + "</div>"; for (String d : destinos) { if (d.equals(destinos.get(destinos.size() - 1))) { to = to.concat(d);//from w w w . ja va 2s. c om } else { to = to.concat(d.concat(",")); } } // Sender's email ID needs to be mentioned String from = this.email; final String username = this.email;//change accordingly final String password = this.senha;//change accordingly Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", this.host); props.put("mail.smtp.port", "25"); // Get the Session object. Session session = Session.getInstance(props, new javax.mail.Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { // Create a default MimeMessage object. Message message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(from)); // Set To: header field of the header. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); // Set Subject: header field message.setSubject(titulo); // Send the actual HTML message, as big as you like message.setContent(msg + rodape, "text/html"); // Send message Transport.send(message); } catch (MessagingException e) { e.printStackTrace(); throw new RuntimeException(e); } }
From source file:org.apache.roller.weblogger.util.MailUtil.java
/** * This method is used to send a Message with a pre-defined * mime-type.// w w w . ja v a 2s . co m * * @param from e-mail address of sender * @param to e-mail address(es) of recipients * @param subject subject of e-mail * @param content the body of the e-mail * @param mimeType type of message, i.e. text/plain or text/html * @throws MessagingException the exception to indicate failure */ public static void sendMessage(String from, String[] to, String[] cc, String[] bcc, String subject, String content, String mimeType) throws MessagingException { MailProvider mailProvider = WebloggerStartup.getMailProvider(); if (mailProvider == null) { return; } Session session = mailProvider.getSession(); Message message = new MimeMessage(session); // n.b. any default from address is expected to be determined by caller. if (!StringUtils.isEmpty(from)) { InternetAddress sentFrom = new InternetAddress(from); message.setFrom(sentFrom); if (log.isDebugEnabled()) log.debug("e-mail from: " + sentFrom); } if (to != null) { InternetAddress[] sendTo = new InternetAddress[to.length]; for (int i = 0; i < to.length; i++) { sendTo[i] = new InternetAddress(to[i]); if (log.isDebugEnabled()) log.debug("sending e-mail to: " + to[i]); } message.setRecipients(Message.RecipientType.TO, sendTo); } if (cc != null) { InternetAddress[] copyTo = new InternetAddress[cc.length]; for (int i = 0; i < cc.length; i++) { copyTo[i] = new InternetAddress(cc[i]); if (log.isDebugEnabled()) log.debug("copying e-mail to: " + cc[i]); } message.setRecipients(Message.RecipientType.CC, copyTo); } if (bcc != null) { InternetAddress[] copyTo = new InternetAddress[bcc.length]; for (int i = 0; i < bcc.length; i++) { copyTo[i] = new InternetAddress(bcc[i]); if (log.isDebugEnabled()) log.debug("blind copying e-mail to: " + bcc[i]); } message.setRecipients(Message.RecipientType.BCC, copyTo); } message.setSubject((subject == null) ? "(no subject)" : subject); message.setContent(content, mimeType); message.setSentDate(new java.util.Date()); // First collect all the addresses together. Address[] remainingAddresses = message.getAllRecipients(); int nAddresses = remainingAddresses.length; boolean bFailedToSome = false; SendFailedException sendex = new SendFailedException("Unable to send message to some recipients"); Transport transport = mailProvider.getTransport(); // Try to send while there remain some potentially good addresses try { do { // Avoid a loop if we are stuck nAddresses = remainingAddresses.length; try { // Send to the list of remaining addresses, ignoring the addresses attached to the message transport.sendMessage(message, remainingAddresses); } catch (SendFailedException ex) { bFailedToSome = true; sendex.setNextException(ex); // Extract the remaining potentially good addresses remainingAddresses = ex.getValidUnsentAddresses(); } } while (remainingAddresses != null && remainingAddresses.length > 0 && remainingAddresses.length != nAddresses); } finally { transport.close(); } if (bFailedToSome) throw sendex; }
From source file:Implement.DAO.CommonDAOImpl.java
@Override public boolean sendMail(String title, String receiver, String messageContent) throws MessagingException { final String username = "registration@youtripper.com"; final String password = "Tripregister190515"; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "mail.youtripper.com"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); }//from www . j a v a 2 s .co m }); Message message = new MimeMessage(session); message.setFrom(new InternetAddress("registration@youtripper.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(receiver)); message.setSubject(title); message.setContent(messageContent, "text/html; charset=utf-8"); Transport.send(message); return true; }
From source file:org.roda.core.util.EmailUtility.java
/** * @param from//from w w w .j a v a 2 s . c om * @param recipients * @param subject * @param message * @throws MessagingException */ public void sendMail(String from, String recipients[], String subject, String message) throws MessagingException { boolean debug = false; // Set the host smtp address Properties props = new Properties(); props.put("mail.smtp.host", this.smtpHost); // create some properties and get the default Session Session session = Session.getDefaultInstance(props, null); session.setDebug(debug); // create a message Message msg = new MimeMessage(session); // set the from and to address InternetAddress addressFrom = new InternetAddress(from); msg.setFrom(addressFrom); InternetAddress[] addressTo = new InternetAddress[recipients.length]; for (int i = 0; i < recipients.length; i++) { addressTo[i] = new InternetAddress(recipients[i]); } msg.setRecipients(Message.RecipientType.TO, addressTo); // Optional : You can also set your custom headers in the Email if you // want // msg.addHeader("MyHeaderName", "myHeaderValue"); String htmlMessage = String.format( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<html><body><pre>%s</pre></body></html>", StringEscapeUtils.escapeHtml4(message)); MimeMultipart mimeMultipart = new MimeMultipart(); MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeBodyPart.setContent(htmlMessage, "text/html;charset=UTF-8"); mimeMultipart.addBodyPart(mimeBodyPart); // Setting the Subject and Content Type msg.setSubject(subject); msg.setContent(mimeMultipart); // msg.setContent(message, "text/plain;charset=UTF-8"); Transport.send(msg); }