List of usage examples for javax.mail Transport send
public static void send(Message msg) throws MessagingException
From source file:com.unilever.audit.services2.ForgetPassword.java
@GET @Path("Email/{email}") @Produces("application/json") public String LoginIn(@PathParam("email") String email) throws IOException { boolean status = true; String error = null;//from www . j av a 2 s . c om JSONObject result = new JSONObject(); Map<String, Object> hm = new HashMap<String, Object>(); hm.put("email", email); Merchandisers merchidisers = (Merchandisers) merchandisersFacadeREST .findOneByQuery("Merchandisers.findByEmail", hm); if (merchidisers == null) { status = false; error = "invalid email"; } else { Properties props = new Properties(); final String from = ""; final String password = ""; props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.user", from); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.password", password); props.put("mail.smtp.port", "587"); props.put("mail.smtp.auth", "true"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(from, password); } }); session.setDebug(true); try { MimeMessage msg = new MimeMessage(session); msg.setFrom(); msg.setRecipients(Message.RecipientType.TO, email); msg.setSubject("Unilever Confirmation"); msg.setSentDate(new Date()); msg.setText(""); Transport.send(msg); } catch (MessagingException ex) { // status = false; // error="Error Sending Email"; ex.printStackTrace(); } } result.put("status", status); result.put("error", error); System.out.println("----------------" + result.toString()); return result.toString(); }
From source file:pmp.springmail.TestDrive.java
void sendEmailViaPlainMail(String text) { Authenticator authenticator = new Authenticator() { @Override/*from w w w. j a va2 s. c om*/ protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(USER, PASS); } }; Session session = Session.getInstance(props, authenticator); Message message = new MimeMessage(session); try { message.setFrom(new InternetAddress(FROM)); message.setRecipient(Message.RecipientType.TO, new InternetAddress(TO)); message.setSubject("Plain JavaMail Test"); message.setText(DATE_FORMAT.format(new Date()) + " " + text); Transport.send(message); } catch (Exception e) { System.err.println(e.getClass().getSimpleName() + " " + e.getMessage()); } }
From source file:org.webguitoolkit.messagebox.mail.MailChannel.java
@Override public void send(IMessage message) { try {//from w w w. ja v a2s . co m Message mail = new MimeMessage(getSession()); mail.addFrom(new InternetAddress[] { new InternetAddress(message.getSender()) }); Set<String> recipients = message.getRecipients(); if (recipients.isEmpty()) return; for (String recipient : recipients) { mail.addRecipient(RecipientType.TO, new InternetAddress(recipient)); } mail.setSubject(message.getSubject()); mail.setContent(message.getBody(), message.getContentType()); Transport.send(mail); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.wso2.carbon.apimgt.core.impl.NewApiVersionMailNotifier.java
@Override public void sendNotifications(NotificationDTO notificationDTO) throws APIManagementException { Properties props = notificationDTO.getProperties(); //get Notifier email List Set<String> emailList = getEmailNotifierList(notificationDTO); if (emailList.isEmpty()) { log.debug("Email Notifier Set is Empty"); return;/*from w w w. j av a 2 s .co m*/ } for (String mail : emailList) { try { Authenticator auth = new SMTPAuthenticator(); Session mailSession = Session.getDefaultInstance(props, auth); MimeMessage message = new MimeMessage(mailSession); notificationDTO.setTitle((String) notificationDTO.getProperty(NotifierConstants.TITLE_KEY)); notificationDTO.setMessage((String) notificationDTO.getProperty(NotifierConstants.TEMPLATE_KEY)); notificationDTO = loadMailTemplate(notificationDTO); message.setSubject(notificationDTO.getTitle()); message.setContent(notificationDTO.getMessage(), NotifierConstants.TEXT_TYPE); message.setFrom(new InternetAddress(mailConfigurations.getFromUser())); message.addRecipient(Message.RecipientType.TO, new InternetAddress(mail)); Transport.send(message); } catch (MessagingException e) { log.error("Exception Occurred during Email notification Sending", e); } } }
From source file:com.gazbert.bxbot.core.mail.EmailAlerter.java
public void sendMessage(String subject, String msgContent) { if (sendEmailAlertsEnabled) { final Session session = Session.getInstance(smtpProps, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(smtpConfig.getAccountUsername(), smtpConfig.getAccountPassword()); }//www . j ava 2s. c o m }); try { final Message message = new MimeMessage(session); message.setFrom(new InternetAddress(smtpConfig.getFromAddress())); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(smtpConfig.getToAddress())); message.setSubject(subject); message.setText(msgContent); LOG.info(() -> "About to send following Email Alert with message content: " + msgContent); Transport.send(message); } catch (MessagingException e) { // not much we can do here, especially if the alert was critical - the bot is shutting down; just log it. LOG.error("Failed to send Email Alert. Details: " + e.getMessage(), e); } } else { LOG.warn("Email Alerts are disabled. Not sending the following message: Subject: " + subject + " Content: " + msgContent); } }
From source file:uk.ac.cam.cl.dtg.util.Mailer.java
/** * SendMail Utility Method Sends e-mail to a given recipient using the hard-coded MAIL_ADDRESS and SMTP details. * /* w ww . j a v a 2 s.com*/ * @param recipient * - string array of recipients that the message should be sent to * @param from * - the e-mail address that should be used as the sending address * @param replyTo * - (nullable) the e-mail address that should be used as the reply-to address * @param replyToName * - (nullable) the name that should be used as the reply-to name * @param subject * - The message subject * @param contents * - The message body * @throws MessagingException * - if we cannot send the message for some reason. * @throws AddressException * - if the address is not valid. */ public void sendPlainTextMail(final String[] recipient, final String from, @Nullable final String replyTo, @Nullable final String replyToName, final String subject, final String contents) throws MessagingException, AddressException, UnsupportedEncodingException { Message msg = this.setupMessage(recipient, from, replyTo, replyToName, subject); msg.setText(contents); Transport.send(msg); }
From source file:org.apache.oodt.cas.crawl.action.EmailNotification.java
@Override public boolean performAction(File product, Metadata metadata) throws CrawlerActionException { try {//ww w. ja va2s . co m Properties props = new Properties(); props.put("mail.host", this.mailHost); props.put("mail.transport.protocol", "smtp"); props.put("mail.from", this.sender); Session session = Session.getDefaultInstance(props); Message msg = new MimeMessage(session); msg.setSubject(PathUtils.doDynamicReplacement(subject, metadata)); msg.setText(new String(PathUtils.doDynamicReplacement(message, metadata).getBytes())); for (String recipient : recipients) { try { msg.addRecipient(Message.RecipientType.TO, new InternetAddress( PathUtils.replaceEnvVariables(recipient.trim(), metadata), ignoreInvalidAddresses)); LOG.fine("Recipient: " + PathUtils.replaceEnvVariables(recipient.trim(), metadata)); } catch (AddressException ae) { LOG.fine("Recipient: " + PathUtils.replaceEnvVariables(recipient.trim(), metadata)); LOG.warning(ae.getMessage()); } } LOG.fine("Subject: " + msg.getSubject()); LOG.fine("Message: " + new String(PathUtils.doDynamicReplacement(message, metadata).getBytes())); Transport.send(msg); return true; } catch (Exception e) { LOG.severe(e.getMessage()); return false; } }
From source file:org.roda.core.util.EmailUtility.java
/** * @param from//w w w . java2 s . c o m * @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); }
From source file:org.quartz.jobs.ee.mail.SendMailJob.java
/** * @see org.quartz.Job#execute(org.quartz.JobExecutionContext) *//* w w w.ja v a2s. co m*/ public void execute(JobExecutionContext context) throws JobExecutionException { JobDataMap data = context.getMergedJobDataMap(); MailInfo mailInfo = populateMailInfo(data, createMailInfo()); getLog().info("Sending message " + mailInfo); try { MimeMessage mimeMessage = prepareMimeMessage(mailInfo); Transport.send(mimeMessage); } catch (MessagingException e) { throw new JobExecutionException("Unable to send mail: " + mailInfo, e, false); } }
From source file:org.cgiar.ccafs.marlo.action.TestSMTPAction.java
@Override public String execute() throws Exception { Properties properties = System.getProperties(); properties.put("mail.smtp.host", config.getEmailHost()); properties.put("mail.smtp.port", config.getEmailPort()); Session session = Session.getInstance(properties, new Authenticator() { @Override//from w w w . ja v a 2s .c om protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(config.getEmailUsername(), config.getEmailPassword()); } }); // Create a new message MimeMessage msg = new MimeMessage(session) { @Override protected void updateMessageID() throws MessagingException { if (this.getHeader("Message-ID") == null) { super.updateMessageID(); } } }; msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("h.jimenez@cgiar.org", false)); msg.setSubject("Test email"); msg.setSentDate(new Date()); MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeBodyPart.setContent("If you receive this email, it means that the server is working correctly.", "text; charset=utf-8"); Thread thread = new Thread() { @Override public void run() { sent = false; int i = 0; while (!sent) { try { Transport.send(sendMail); LOG.info("Message sent TRIED#: " + i + " \n" + "Test email"); sent = true; } catch (MessagingException e) { LOG.info("Message DON'T sent: \n" + "Test email"); i++; if (i == 10) { break; } try { Thread.sleep(1 * // minutes to sleep 60 * // seconds to a minute 1000); } catch (InterruptedException e1) { e1.printStackTrace(); } e.printStackTrace(); } } }; }; thread.run(); if (sent) { return SUCCESS; } else { return INPUT; } }