List of usage examples for javax.mail.internet MimeMessage setSubject
@Override public void setSubject(String subject) throws MessagingException
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); // Now set the actual message mimeMessage.setText(message);/*from w w w . ja v a2 s .c om*/ // Send message Transport.send(mimeMessage); }
From source file:org.codice.alliance.core.email.impl.EmailSenderImpl.java
/** sendEmail method sends email after receiving input parameters */ @Override/*from w ww . j av a2 s . c om*/ public void sendEmail(String fromEmail, String toEmail, String subject, String body, List<Pair<String, InputStream>> attachments) throws IOException { notNull(fromEmail, "fromEmail must be non-null"); notNull(toEmail, "toEmail must be non-null"); notNull(subject, "subject must be non-null"); notNull(body, "body must be non-null"); notNull(attachments, "attachments must be non-null"); if (StringUtils.isBlank(mailHost)) { throw new IOException("the mail server hostname has not been configured"); } List<File> tempFiles = new LinkedList<>(); try { InternetAddress emailAddr = new InternetAddress(toEmail); emailAddr.validate(); Properties properties = createSessionProperties(); Session session = Session.getDefaultInstance(properties); MimeMessage mimeMessage = new MimeMessage(session); mimeMessage.setFrom(new InternetAddress(fromEmail)); mimeMessage.addRecipient(Message.RecipientType.TO, emailAddr); mimeMessage.setSubject(subject); BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText(body); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); Holder<Long> bytesWritten = new Holder<>(0L); for (Pair<String, InputStream> attachment : attachments) { messageBodyPart = new MimeBodyPart(); File file = File.createTempFile("email-sender-", ".dat"); tempFiles.add(file); copyDataToTempFile(file, attachment.getValue(), bytesWritten); messageBodyPart.setDataHandler(new DataHandler(new FileDataSource(file))); messageBodyPart.setFileName(attachment.getKey()); multipart.addBodyPart(messageBodyPart); } mimeMessage.setContent(multipart); send(mimeMessage); LOGGER.debug("Email sent to " + toEmail); } catch (AddressException e) { throw new IOException("invalid email address: email=" + toEmail, e); } catch (MessagingException e) { throw new IOException("message error occurred on send", e); } finally { tempFiles.forEach(file -> { if (!file.delete()) { LOGGER.debug("unable to delete tmp file: path={}", file); } }); } }
From source file:de.elomagic.mag.AbstractTest.java
protected MimeMessage createMimeMessage(String filename) throws Exception { MimeBodyPart textPart = new MimeBodyPart(); textPart.setContent("This is some text to be displayed inline", "text/plain"); textPart.setDisposition(Part.INLINE); MimeBodyPart binaryPart = new MimeBodyPart(); InputStream in = getClass().getResourceAsStream("/TestFile.pdf"); binaryPart.setContent(getOriginalMailAttachment(), "application/pdf"); binaryPart.setDisposition(Part.ATTACHMENT); binaryPart.setFileName(filename);//from w w w . ja va2 s . co m Multipart mp = new MimeMultipart(); mp.addBodyPart(textPart); mp.addBodyPart(binaryPart); MimeMessage message = new MimeMessage(greenMail.getImap().createSession()); message.setRecipients(Message.RecipientType.TO, new InternetAddress[] { new InternetAddress("mailuser@localhost.com") }); message.setSubject("Another test mail subject"); message.setContent(mp); // return message; // GreenMailUtil.createTextEmail("to@localhost.com", "from@localhost.com", "subject", "body", greenMail.getImap().getServerSetup()); }
From source file:com.autentia.tnt.mail.DefaultMailService.java
public void sendFiles(String to, String subject, String text, Collection<File> attachments) throws MessagingException { MimeMessage message = new MimeMessage(session); Transport t = session.getTransport("smtp"); message.setFrom(new InternetAddress(configurationUtil.getMailUsername())); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject(subject); message.setSentDate(new Date()); if (attachments == null || attachments.size() < 1) { message.setText(text);/*w w w .j av a2s .co m*/ } else { // create the message part MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText(text); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); for (File attachment : attachments) { messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(attachment); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(attachment.getName()); multipart.addBodyPart(messageBodyPart); } message.setContent(multipart); } t.connect(configurationUtil.getMailUsername(), configurationUtil.getMailPassword()); t.sendMessage(message, message.getAllRecipients()); t.close(); }
From source file:davmail.smtp.TestSmtp.java
public void testInvalidFrom() throws IOException, MessagingException, InterruptedException { String body = "Test message"; MimeMessage mimeMessage = new MimeMessage((Session) null); mimeMessage.addHeader("From", "guessant@loca.net"); mimeMessage.addHeader("To", Settings.getProperty("davmail.to")); mimeMessage.setSubject("Test subject"); mimeMessage.setText(body);//from ww w . jav a 2s.c o m sendAndCheckMessage(mimeMessage, "guessant@loca.net", null); }
From source file:net.duckling.ddl.service.mail.impl.MailServiceImpl.java
public void sendMail(Mail mail) { LOG.debug("sendEmail() to: " + mail.getRecipient()); try {//from ww w. j av a 2s. com Session session = Session.getInstance(m_bag.m_mailProperties, m_bag.m_authenticator); session.setDebug(false); MimeMessage msg = new MimeMessage(session); msg.setFrom(m_bag.m_fromAddress); msg.setRecipient(Message.RecipientType.TO, new InternetAddress(mail.getRecipient())); msg.setSubject(mail.getSubject()); msg.setSentDate(new Date()); Multipart mp = new MimeMultipart(); MimeBodyPart txtmbp = new MimeBodyPart(); txtmbp.setContent(mail.getMessage(), EMAIL_CONTENT_TYPE); mp.addBodyPart(txtmbp); List<String> attachments = mail.getAttachments(); for (Iterator<String> it = attachments.iterator(); it.hasNext();) { MimeBodyPart mbp = new MimeBodyPart(); String filename = it.next(); FileDataSource fds = new FileDataSource(filename); mbp.setDataHandler(new DataHandler(fds)); mbp.setFileName(MimeUtility.encodeText(fds.getName())); mp.addBodyPart(mbp); } msg.setContent(mp); if ((m_bag.m_fromAddress != null) && (m_bag.m_fromAddress.getAddress() != null) && (m_bag.m_fromAddress.getAddress().indexOf("@") != -1)) { cheat(msg, m_bag.m_fromAddress.getAddress().substring(m_bag.m_fromAddress.getAddress().indexOf("@"))); } Transport.send(msg); LOG.info("Successfully send the mail to " + mail.getRecipient()); } catch (Throwable e) { LOG.error("Exception occured while trying to send notification to: " + mail.getRecipient(), e); LOG.debug("Details:", e); } }
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 w ww. j a v a 2s . co 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:org.apache.james.postage.mail.AbstractMailFactory.java
/** * generates a mail containing data common to all test mails: postage headers, *//*from w w w.ja va 2s . c o m*/ public Message createMail(Session mailSession, MailSender mailSender, MailProcessingRecord mailProcessingRecord) { MimeMessage message = new MimeMessage(mailSession); try { message.addHeader(HeaderConstants.JAMES_POSTAGE_HEADER, "This is a test mail sent by James Postage"); message.addHeader(HeaderConstants.JAMES_POSTAGE_VALIDATORCLASSNAME_HEADER, getValidatorClass().getName()); message.setSubject(mailSender.getSubject()); message.addHeader("Message-ID", "Postage-" + System.currentTimeMillis()); mailProcessingRecord.setSubject(mailSender.getSubject()); if (mailProcessingRecord.getMailId() != null) { message.addHeader(HeaderConstants.MAIL_ID_HEADER, mailProcessingRecord.getMailId()); } else { log.warn("ID header is NULL!"); throw new RuntimeException("could not create mail with ID = NULL"); } populateMessage(message, mailSender, mailProcessingRecord); } catch (MessagingException e) { mailProcessingRecord.setErrorTextSending(e.toString()); log.error("mail could not be created", e); return null; } return message; }
From source file:edu.harvard.med.screensaver.service.SmtpEmailService.java
private void sendMessage(String subject, String message, InternetAddress from, InternetAddress[] recipients, InternetAddress[] cclist, File attachedFile) throws MessagingException, IOException { log.info("try to send: " + printEmail(subject, message, from, this.replyTos, recipients, cclist, (attachedFile == null ? "" : "" + attachedFile.getCanonicalFile()))); log.info("host: " + host + ", useSMTPS: " + useSmtps); Properties props = new Properties(); String protocol = "smtp"; if (useSmtps) // need smtps to test with gmail {/*w w w . jav a 2s .co m*/ props.put("mail.smtps.auth", "true"); protocol = "smtps"; } Session session = Session.getDefaultInstance(props, null); Transport t = session.getTransport(protocol); try { MimeMessage msg = new MimeMessage(session); if (this.replyTos != null) msg.setReplyTo(replyTos); msg.setFrom(from); msg.setSubject(subject); if (attachedFile != null) { setFileAsAttachment(msg, message, attachedFile); } else { msg.setContent(message, "text/plain"); } msg.addRecipients(Message.RecipientType.TO, recipients); if (cclist != null) msg.addRecipients(Message.RecipientType.CC, cclist); t.connect(host, username, password); t.sendMessage(msg, msg.getAllRecipients()); } finally { t.close(); } log.info("sent: " + printEmailHeader(subject, from, this.replyTos, recipients, cclist, (attachedFile == null ? "" : "" + attachedFile.getCanonicalFile()))); }
From source file:org.modelibra.util.Emailer.java
/** * Sends an email.//from w w w. ja va2s . c o m * * @throws dmLite * exception if there is a problem */ public void send() throws ModelibraException { try { MimeMessage message = new MimeMessage(emailSession); InternetAddress fromIA = new InternetAddress(from); message.setFrom(fromIA); InternetAddress toIA = new InternetAddress(to); message.addRecipient(Message.RecipientType.TO, toIA); message.setSubject(subject); message.setText(content); emailStore.connect(outServer, code, password); Transport.send(message); emailStore.close(); } catch (MessagingException e) { throw new ModelibraException("Could not send an email: " + e.getMessage()); } catch (IllegalStateException e) { throw new ModelibraException("Could not send an email: " + e.getMessage()); } }