List of usage examples for javax.mail.internet MimeMessage setSubject
@Override public void setSubject(String subject) throws MessagingException
From source file:com.github.sleroy.junit.mail.server.test.MailSender.java
/** * Send mail./*from ww w.j av a 2s . com*/ * * @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(); } } }
From source file:com.twinsoft.convertigo.engine.admin.services.projects.Deploy.java
@Override protected void doUpload(HttpServletRequest request, Document document, FileItem item) throws Exception { if (!item.getName().endsWith(".car")) { ServiceUtils/*from w w w. j a v a 2 s.c o m*/ .addMessage(document, document.getDocumentElement(), "The deployment of the project " + item.getName() + " has failed. The archive file is not valid (.car required).", "error", false); } super.doUpload(request, document, item); // Depending on client browsers, according to the documentation, // item.getName() can either return a full path file name, or // simply a file name. String projectArchive = item.getName(); // Bugfix #1425 int i = projectArchive.lastIndexOf('/'); if (i == -1) { i = projectArchive.lastIndexOf('\\'); if (i != -1) { projectArchive = projectArchive.substring(i + 1); } } else { projectArchive = projectArchive.substring(i + 1); } String projectName = projectArchive.substring(0, projectArchive.indexOf(".car")); Engine.theApp.databaseObjectsManager.deployProject(getRepository() + projectArchive, true, bAssembleXsl); if (Boolean.parseBoolean( EnginePropertiesManager.getProperty(PropertyName.NOTIFICATIONS_NOTIFY_PROJECT_DEPLOYMENT))) { final String fUser = (String) request.getSession().getAttribute(SessionKey.ADMIN_USER.toString()); final String fProjectName = projectName; new Thread(new Runnable() { public void run() { try { Properties props = new Properties(); props.put("mail.smtp.host", EnginePropertiesManager.getProperty(PropertyName.NOTIFICATIONS_SMTP_HOST)); props.put("mail.smtp.socketFactory.port", EnginePropertiesManager.getProperty(PropertyName.NOTIFICATIONS_SMTP_PORT)); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.socketFactory.fallback", "false"); // Initializing Session mailSession = Session.getInstance(props, new Authenticator() { @Override public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( EnginePropertiesManager.getProperty(PropertyName.NOTIFICATIONS_SMTP_USER), EnginePropertiesManager .getProperty(PropertyName.NOTIFICATIONS_SMTP_PASSWORD)); } }); MimeMessage message = new MimeMessage(mailSession); message.addRecipient(Message.RecipientType.TO, new InternetAddress( EnginePropertiesManager.getProperty(PropertyName.NOTIFICATIONS_TARGET_EMAIL))); message.setSubject("[trial] deployment of " + fProjectName + " by " + fUser); message.setText(message.getSubject() + "\n" + "http://trial.convertigo.net/cems/projects/" + fProjectName + "\n" + "https://trial.convertigo.net/cems/projects/" + fProjectName); Transport.send(message); } catch (MessagingException e1) { } } }).start(); } String message = "The project '" + projectName + "' has been successfully deployed."; Engine.logAdmin.info(message); ServiceUtils.addMessage(document, document.getDocumentElement(), message, "message", false); }
From source file:org.socraticgrid.hl7.ucs.nifi.processor.SendEmail.java
private String sendEmail(String emailSubject, String fromEmail, String toEmail, String emailBody, String mimeType, String charset, String smtpServerUrl, String smtpServerPort, String username, String password, ServiceStatusController serviceStatusControllerService) throws Exception { String statusMessage = "Email sent successfully to " + toEmail; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", smtpServerUrl); props.put("mail.smtp.port", smtpServerPort); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); }//from ww w .ja v a 2 s .co m }); try { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(fromEmail)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail)); message.setSubject(emailSubject); message.setContent(emailBody, mimeType + "; charset=" + charset); Transport.send(message); getLogger().info("Email sent successfully!"); serviceStatusControllerService.updateServiceStatus("EMAIL", Status.AVAILABLE); } catch (MessagingException e) { serviceStatusControllerService.updateServiceStatus("EMAIL", Status.UNAVAILABLE); getLogger().error("Unable to send Email! Reason : " + e.getMessage(), e); statusMessage = "Unable to send Email! Reason : " + e.getMessage(); throw new RuntimeException(e); } return statusMessage; }
From source file:com.srotya.tau.nucleus.qa.QAAlertRules.java
@Test public void testASMTPServerAvailability() throws UnknownHostException, IOException, MessagingException { MimeMessage msg = new MimeMessage(Session.getDefaultInstance(new Properties())); msg.setFrom(new InternetAddress("alert@srotya.com")); msg.setRecipient(RecipientType.TO, new InternetAddress("alert@srotya.com")); msg.setSubject("test mail"); msg.setContent("Hello", "text/html"); Transport.send(msg);/* w w w . java 2s.c o m*/ MailService ms = new MailService(); ms.init(new HashMap<>()); Alert alert = new Alert(); alert.setBody("test"); alert.setId((short) 0); alert.setMedia("test"); alert.setSubject("test"); alert.setTarget("alert@srotya.com"); ms.sendMail(alert); assertEquals(2, AllQATests.getSmtpServer().getReceivedEmailSize()); System.err.println("Mail sent"); }
From source file:com.predic8.membrane.core.interceptor.authentication.session.EmailTokenProvider.java
private void sendEmail(String sender, String recipient, String subject, String text) { try {/* w ww .j a v a2s.com*/ Properties props = System.getProperties(); props.put("mail.smtp.port", "" + smtpPort); props.put("mail.smtp.socketFactory.port", "" + smtpPort); if (ssl) { props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.starttls.required", "true"); } if (smtpUser != null) { props.put("mail.smtp.auth", "true"); } Session session = Session.getInstance(props, null); final MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(sender)); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient, false)); msg.setSubject(subject); msg.setText(text, Constants.UTF_8); msg.setSentDate(new Date()); SMTPTransport t = (SMTPTransport) session.getTransport(ssl ? "smtps" : "smtp"); t.connect(smtpHost, smtpUser, smtpPassword); t.sendMessage(msg, msg.getAllRecipients()); t.close(); } catch (MessagingException e) { throw new RuntimeException(e); } }
From source file:io.uengine.mail.MailAsyncService.java
@Async public void trialCreated(String subject, String fromUser, String fromName, String toUser, InternetAddress[] toCC) {//from ww w . j av a 2s.com Session session = setMailProperties(toUser); Map model = new HashMap(); model.put("link", "http://www.uengine.io/my/license"); String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "mail/trial-created.vm", "UTF-8", model); try { InternetAddress from = new InternetAddress(fromUser, fromName); MimeMessage message = new MimeMessage(session); message.setFrom(from); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toUser)); message.setSubject(subject); message.setContent(body, "text/html; charset=utf-8"); if (toCC != null && toCC.length > 0) message.setRecipients(Message.RecipientType.CC, toCC); Transport.send(message); logger.info("{} ? ?? .", toUser); } catch (Exception e) { throw new ServiceException("?? .", e); } }
From source file:io.uengine.mail.MailAsyncService.java
@Async public void passwd(Long userId, String token, String subject, String fromUser, String fromName, final String toUser, InternetAddress[] toCC) { Session session = setMailProperties(toUser); Map model = new HashMap(); model.put("link", MessageFormatter.arrayFormat("http://www.uengine.io/auth/passwdConfirm?userid={}&token={}", new Object[] { Long.toString(userId), token }).getMessage()); String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "mail/passwd.vm", "UTF-8", model); try {//from w w w. j a v a 2 s. c o m InternetAddress from = new InternetAddress(fromUser, fromName); MimeMessage message = new MimeMessage(session); message.setFrom(from); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toUser)); message.setSubject(subject); message.setContent(body, "text/html; charset=utf-8"); if (toCC != null && toCC.length > 0) message.setRecipients(Message.RecipientType.CC, toCC); Transport.send(message); logger.info("{} ? ?? .", toUser); } catch (Exception e) { throw new ServiceException("?? .", e); } }
From source file:io.uengine.mail.MailAsyncService.java
@Async public void registe(Long userId, String token, String subject, String fromUser, String fromName, final String toUser, InternetAddress[] toCC) { Session session = setMailProperties(toUser); Map model = new HashMap(); model.put("link", MessageFormatter.arrayFormat("http://www.uengine.io/registe/confirm?userid={}&token={}", new Object[] { Long.toString(userId), token }).getMessage()); String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "mail/registe.vm", "UTF-8", model);/*from w ww . j ava 2 s . co m*/ try { InternetAddress from = new InternetAddress(fromUser, fromName); MimeMessage message = new MimeMessage(session); message.setFrom(from); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toUser)); message.setSubject(subject); message.setContent(body, "text/html; charset=utf-8"); if (toCC != null && toCC.length > 0) message.setRecipients(Message.RecipientType.CC, toCC); Transport.send(message); logger.info("{} ? ?? .", toUser); } catch (Exception e) { throw new ServiceException("?? .", e); } }
From source file:com.hiperium.bo.manager.mail.EmailMessageManager.java
/** * Sends an email with the new user password. * /* w w w . j a v a 2 s. c o m*/ * @param user * @throws javax.mail.internet.AddressException * @throws javax.mail.MessagingException */ public void sendNewPassword(User user) { try { this.log.debug("sendNewPassword() - START"); // Create the corresponding user locale. Locale locale = null; if (StringUtils.isBlank(user.getLanguageId())) { locale = Locale.getDefault(); } else { locale = new Locale(user.getLanguageId()); } ResourceBundle resource = ResourceBundle.getBundle(EnumI18N.COMMON.value(), locale); // Get system properties Properties properties = System.getProperties(); // Setup mail server properties.setProperty("mail.smtp.host", CloudEmailUser.HOST); properties.setProperty("mail.user", CloudEmailUser.USERNAME); properties.setProperty("mail.password", CloudEmailUser.PASSWORD); // Get the default Session object. Session session = Session.getDefaultInstance(properties); // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(CloudEmailUser.ADDRESS)); // Set To: header field of the header. message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail())); // Set Subject: header field message.setSubject(resource.getString(GENERATED_USER_PASSWD_SUBJECT)); // Send the actual HTML message, as big as you like message.setContent(MessageFormat.format(resource.getString(GENERATED_USER_PASSWD_CONTENT), user.getFirstname(), user.getLastname(), user.getPassword()), "text/html"); // Send message Transport.send(message); this.log.debug("sendNewPassword() - END"); } catch (AddressException e) { this.log.error("AddressException to send email to " + user.getEmail(), e); } catch (MessagingException e) { this.log.error("MessagingException to send email to " + user.getEmail(), e); } }
From source file:net.sourceforge.vulcan.mailer.MessageAssembler.java
public MimeMessage constructMessage(String subscribers, ConfigDto config, ProjectStatusDto status, String html) throws MessagingException, AddressException { final MimeMessage message = new MimeMessage(mailSession); message.setSentDate(new Date()); message.setFrom(new InternetAddress(config.getSenderAddress())); if (isNotBlank(config.getReplyToAddress())) { message.setReplyTo(InternetAddress.parse(config.getReplyToAddress())); }/* ww w . j av a 2 s. c o m*/ message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(subscribers)); message.setSubject(status.getName() + ": " + status.getStatus()); final Multipart multipart = new MimeMultipart(); html = html.replaceAll("\\r", ""); addMultipartBody(multipart, "text/html; charset=UTF-8", html); message.setContent(multipart); return message; }