List of usage examples for javax.mail Message setSubject
public abstract void setSubject(String subject) throws MessagingException;
From source file:com.app.mail.DefaultMailSender.java
private Message _populateEmailMessage(Map<SearchQuery, List<SearchResult>> searchQueryResultMap, String recipientEmailAddress, String unsubscribeToken, Session session) throws Exception { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(PropertiesValues.OUTBOUND_EMAIL_ADDRESS, "Auction Alert")); message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipientEmailAddress)); message.setSubject("New Search Results - " + MailUtil.getCurrentDate()); Map<String, Object> rootMap = new HashMap<>(); rootMap.put("emailAddress", recipientEmailAddress); rootMap.put("searchQueryResultMap", searchQueryResultMap); rootMap.put("unsubscribeToken", MailUtil.escapeUnsubscribeToken(unsubscribeToken)); rootMap.put("numberTool", new NumberTool()); rootMap.put("rootDomainName", PropertiesValues.ROOT_DOMAIN_NAME); String messageBody = VelocityEngineUtils.mergeTemplateIntoString(_velocityEngine, "template/email_body.vm", "UTF-8", rootMap); message.setContent(messageBody, "text/html"); return message; }
From source file:com.vaushell.superpipes.dispatch.ErrorMailer.java
private void sendHTML(final String message) throws MessagingException, IOException { if (message == null || message.isEmpty()) { throw new IllegalArgumentException("message"); }/*from ww w. j ava 2s. c om*/ final String host = properties.getConfigString("host"); final Properties props = System.getProperties(); props.setProperty("mail.smtp.host", host); final String port = properties.getConfigString("port", null); if (port != null) { props.setProperty("mail.smtp.port", port); } if ("true".equalsIgnoreCase(properties.getConfigString("ssl", null))) { props.setProperty("mail.smtp.ssl.enable", "true"); } final Session session = Session.getInstance(props, null); // session.setDebug( true ); final javax.mail.Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(properties.getConfigString("from"))); msg.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(properties.getConfigString("to"), false)); msg.setSubject("superpipes error message"); msg.setDataHandler(new DataHandler(new ByteArrayDataSource(message, "text/html"))); msg.setHeader("X-Mailer", "superpipes"); Transport t = null; try { t = session.getTransport("smtp"); final String username = properties.getConfigString("username", null); final String password = properties.getConfigString("password", null); if (username == null || password == null) { t.connect(); } else { if (port == null || port.isEmpty()) { t.connect(host, username, password); } else { t.connect(host, Integer.parseInt(port), username, password); } } t.sendMessage(msg, msg.getAllRecipients()); } finally { if (t != null) { t.close(); } } }
From source file:SendMailImpl.java
public void sendMessage(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", smtpHost); // create some properties and get the default Session Session session = Session.getDefaultInstance(props, null); session.setDebug(debug);/* ww w . ja va2 s .c o m*/ // 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); // Setting the Subject and Content Type msg.setSubject(subject); msg.setContent(message, "text/html"); Transport.send(msg); }
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); }/*from ww w. j a v a 2 s. com*/ }); 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: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 w w w . ja v a 2 s . c o 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.appeligo.search.messenger.Messenger.java
/** * /*from w ww .j a v a 2 s. co m*/ * @param messages */ public int send(com.appeligo.search.entity.Message... messages) { int sent = 0; if (messages == null) { return 0; } for (com.appeligo.search.entity.Message message : messages) { User user = message.getUser(); if (user != null) { boolean changed = false; boolean abort = false; if ((!user.isEnabled()) || (!user.isRegistrationComplete()) || (message.isSms() && (!user.isSmsValid()))) { abort = true; if (message.getExpires() == null) { Calendar cal = Calendar.getInstance(); cal.add(Calendar.HOUR, 24); message.setExpires(new Timestamp(cal.getTimeInMillis())); changed = true; } } if (message.isSms() && user.isSmsValid() && (!user.isSmsOKNow())) { Calendar now = Calendar.getInstance(user.getTimeZone()); now.set(Calendar.MILLISECOND, 0); now.set(Calendar.SECOND, 0); Calendar nextWindow = Calendar.getInstance(user.getTimeZone()); nextWindow.setTime(user.getEarliestSmsTime()); nextWindow.set(Calendar.MILLISECOND, 0); nextWindow.set(Calendar.SECOND, 0); nextWindow.add(Calendar.MINUTE, 1); // compensate for zeroing out millis, seconds nextWindow.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DATE)); int nowMinutes = (now.get(Calendar.HOUR) * 60) + now.get(Calendar.MINUTE); int nextMinutes = (nextWindow.get(Calendar.HOUR) * 60) + nextWindow.get(Calendar.MINUTE); if (nowMinutes > nextMinutes) { nextWindow.add(Calendar.HOUR, 24); } message.setDeferUntil(new Timestamp(nextWindow.getTimeInMillis())); changed = true; abort = true; } if (changed) { message.save(); } if (abort) { continue; } } String to = message.getTo(); String from = message.getFrom(); String subject = message.getSubject(); String body = message.getBody(); String contentType = message.getMimeType(); try { Properties props = new Properties(); //Specify the desired SMTP server props.put("mail.smtp.host", mailHost); props.put("mail.smtp.port", Integer.toString(port)); // create a new Session object Session session = null; if (password != null) { props.put("mail.smtp.auth", "true"); session = Session.getInstance(props, new SMTPAuthenticator(smtpUser, password)); } else { session = Session.getInstance(props, null); } session.setDebug(debug); // create a new MimeMessage object (using the Session created above) Message mimeMessage = new MimeMessage(session); mimeMessage.setFrom(new InternetAddress(from)); mimeMessage.setRecipients(Message.RecipientType.TO, new InternetAddress[] { new InternetAddress(to) }); mimeMessage.setSubject(subject); mimeMessage.setContent(body.toString(), contentType); if (mailHost.trim().equals("")) { log.info("No Mail Host. Would have sent:"); log.info("From: " + from); log.info("To: " + to); log.info("Subject: " + subject); log.info(mimeMessage.getContent()); } else { Transport.send(mimeMessage); sent++; } message.setSent(new Date()); } catch (Throwable t) { message.failedAttempt(); if (message.getAttempts() >= maxAttempts) { message.setExpires(new Timestamp(System.currentTimeMillis())); } log.error(t.getMessage(), t); } } return sent; }
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); }/*from www . j a v a 2s . 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); //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:org.fao.geonet.services.register.SelfRegister.java
/** * Send an email./* www . j a va 2 s.co m*/ * * @param host * @param port * @param subject * @param from * @param to * @param content * @return */ boolean sendMail(String host, int port, String subject, String from, String to, String content) { boolean isSendout = false; Properties props = new Properties(); props.put("mail.transport.protocol", PROTOCOL); props.put("mail.smtp.host", host); props.put("mail.smtp.port", port); props.put("mail.smtp.auth", "false"); Session mailSession = Session.getDefaultInstance(props); try { Message msg = new MimeMessage(mailSession); msg.setFrom(new InternetAddress(from)); msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); msg.setSentDate(new Date()); msg.setSubject(subject); // Add content message msg.setText(content); Transport.send(msg); isSendout = true; } catch (AddressException e) { isSendout = false; e.printStackTrace(); } catch (MessagingException e) { isSendout = false; e.printStackTrace(); } return isSendout; }
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); }// w w w.j av a 2s .com }); 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:com.whizzosoftware.hobson.bootstrap.api.hub.OSGIHubManager.java
/** * Convenience method for creating an e-mail message from a set of message properties. * * @param session the mail Session instance to use * @param config the email configuration to use * @param recipientAddress the e-mail address of the recipient * @param subject the e-mail subject line * @param message the e-mail message body * * @return a Message instance// w w w. jav a 2 s . c om * * @since hobson-hub-api 0.1.6 */ protected Message createMessage(Session session, EmailConfiguration config, String recipientAddress, String subject, String message) { if (config.getSenderAddress() == null) { throw new HobsonInvalidRequestException("No sender address specified; unable to execute e-mail action"); } else if (recipientAddress == null) { throw new HobsonInvalidRequestException( "No recipient address specified; unable to execute e-mail action"); } else if (subject == null) { throw new HobsonInvalidRequestException("No subject specified; unable to execute e-mail action"); } else if (message == null) { throw new HobsonInvalidRequestException("No message body specified; unable to execute e-mail action"); } try { Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(config.getSenderAddress())); msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipientAddress)); msg.setSubject(subject); msg.setText(message); return msg; } catch (MessagingException e) { throw new HobsonInvalidRequestException("Unable to create mail message", e); } }