List of usage examples for javax.mail Transport send
public static void send(Message msg) throws MessagingException
From source file:org.opens.kbaccess.controller.utils.AMailerController.java
/** * Send a mail to the specified recipients. * //from w ww . ja va2 s .c o m * @param subject The mail's subject * @param message The mail's body * @param recipients The adressee * @return true if the send succeed, * false otherwise */ public boolean sendMail(String subject, String message, String[] recipients) { Session session; MimeMessage mimeMessage; Properties properties; // sanity check if (recipients.length == 0) { return true; } // set-up session properties = new Properties(); properties.put("mail.smtp.host", mailingServiceProperties.getSmtpHost()); session = Session.getDefaultInstance(properties); //session.setDebug(true); try { Address from; Address[] to = new InternetAddress[recipients.length]; // set sender from = new InternetAddress(mailingServiceProperties.getDefaultReturnAddress()); // initialize recipients list for (int i = 0; i < to.length; ++i) { to[i] = new InternetAddress(recipients[i]); } // create message mimeMessage = new MimeMessage(session); mimeMessage.setSender(from); mimeMessage.setFrom(from); mimeMessage.setReplyTo(new Address[] { from }); mimeMessage.setRecipients(Message.RecipientType.TO, to); mimeMessage.setSubject(subject); mimeMessage.setText(message, "utf-8"); // send it Transport.send(mimeMessage); } catch (MessagingException ex) { LogFactory.getLog(GuestController.class).error("Unable to send email", ex); return false; } return true; }
From source file:eu.scape_project.planning.application.Feedback.java
/** * Method responsible for sending feedback per mail. * /*from ww w . j a v a 2 s. co m*/ * @param userEmail * email address of the user. * @param userComments * comments from the user * @param location * the location of the application where the error occurred * @param applicationName * the name of the application * @throws MailException * if the feedback could not be sent */ public void sendFeedback(String userEmail, String userComments, String location, String applicationName) throws MailException { try { Properties props = System.getProperties(); props.put("mail.smtp.host", config.getString("mail.smtp.host")); Session session = Session.getDefaultInstance(props, null); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(config.getString("mail.from"))); message.setRecipient(RecipientType.TO, new InternetAddress(config.getString("mail.feedback"))); message.setSubject("[" + applicationName + "] from " + location); StringBuilder builder = new StringBuilder(); builder.append("Date: ").append(dateFormat.format(new Date())).append("\n\n"); // User info if (user == null) { builder.append("No user available.\n\n"); } else { builder.append("User: ").append(user.getUsername()).append("\n"); if (user.getUserGroup() != null) { builder.append("Group: ").append(user.getUserGroup().getName()).append("\n"); } } builder.append("UserMail: ").append(userEmail).append("\n\n"); // Comments builder.append("Comments:\n"); builder.append(SEPARATOR_LINE); builder.append(userComments).append("\n"); builder.append(SEPARATOR_LINE).append("\n"); message.setText(builder.toString()); message.saveChanges(); Transport.send(message); log.debug("Feedback mail sent successfully to {}", config.getString("mail.feedback")); } catch (MessagingException e) { log.error("Error sending feedback mail to {}", config.getString("mail.feedback"), e); throw new MailException("Error sending feedback mail to " + config.getString("mail.feedback"), e); } }
From source file:com.emc.plants.service.impl.MailerBean.java
/** * Create a mail message and send it./* ww w . j av a 2 s .co m*/ * * @param customerInfo Customer information. * @param orderKey * @throws MailerAppException */ public void createAndSendMail(CustomerInfo customerInfo, long orderKey) throws MailerAppException { try { EMailMessage eMessage = new EMailMessage(createSubjectLine(orderKey), createMessage(orderKey), customerInfo.getCustomerID()); Util.debug("Sending message" + "\nTo: " + eMessage.getEmailReceiver() + "\nSubject: " + eMessage.getSubject() + "\nContents: " + eMessage.getHtmlContents()); //Session mailSession = (Session) Util.getInitialContext().lookup(MAIL_SESSION); MimeMessage msg = new MimeMessage(mailSession); msg.setFrom(); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(eMessage.getEmailReceiver(), false)); msg.setSubject(eMessage.getSubject()); MimeBodyPart mbp = new MimeBodyPart(); mbp.setText(eMessage.getHtmlContents(), "us-ascii"); msg.setHeader("X-Mailer", "JavaMailer"); Multipart mp = new MimeMultipart(); mp.addBodyPart(mbp); msg.setContent(mp); msg.setSentDate(new Date()); Transport.send(msg); Util.debug("\nMail sent successfully."); } catch (Exception e) { Util.debug("Error sending mail. Have mail resources been configured correctly?"); Util.debug("createAndSendMail exception : " + e); e.printStackTrace(); throw new MailerAppException("Failure while sending mail"); } }
From source file:SendMime.java
/** Do the work: send the mail to the SMTP server. */ public void doSend() throws IOException, MessagingException { // Create the Session object session = Session.getDefaultInstance(null, null); session.setDebug(true); // Verbose! try {/*from ww w. jav a2 s .com*/ // create a message mesg = new MimeMessage(session); // From Address - this should come from a Properties... mesg.setFrom(new InternetAddress("nobody@host.domain")); // TO Address InternetAddress toAddress = new InternetAddress(message_recip); mesg.addRecipient(Message.RecipientType.TO, toAddress); // CC Address InternetAddress ccAddress = new InternetAddress(message_cc); mesg.addRecipient(Message.RecipientType.CC, ccAddress); // The Subject mesg.setSubject(message_subject); // Now the message body. Multipart mp = new MimeMultipart(); BodyPart textPart = new MimeBodyPart(); textPart.setText(message_body); // sets type to "text/plain" BodyPart pixPart = new MimeBodyPart(); pixPart.setContent(html_data, "text/html"); // Collect the Parts into the MultiPart mp.addBodyPart(textPart); mp.addBodyPart(pixPart); // Put the MultiPart into the Message mesg.setContent(mp); // Finally, send the message! Transport.send(mesg); } catch (MessagingException ex) { System.err.println(ex); ex.printStackTrace(System.err); } }
From source file:it.infn.ct.security.actions.ReactivateUser.java
private void sendMail(LDAPUser user) throws MailException { javax.mail.Session session = null;/*from w w w. ja v a 2 s.c om*/ 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, mailFrom)); InternetAddress mailTos[] = new InternetAddress[1]; mailTos[0] = new InternetAddress(mailTo); mailMsg.setRecipients(Message.RecipientType.TO, mailTos); mailMsg.setSubject(mailSubject); mailBody = mailBody.replaceAll("_USER_", user.getTitle() + " " + user.getGivenname() + " " + user.getSurname() + " (" + user.getUsername() + ")"); 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:org.eclipse.skalli.core.mail.MailComponent.java
private void sendMailInternal(Address[] rcptTo, Address[] rcptCC, Address[] rcptBCC, Address from, String subject, String body) { try {//from w w w . j av a 2 s . c o m String mailHost = "mail.sap.corp"; //$NON-NLS-1$ Properties props = System.getProperties(); props.put("mail.smtp.host", mailHost); //$NON-NLS-1$ Session session = Session.getInstance(props, null); Message message = new MimeMessage(session); message.setFrom(from); if (rcptTo != null && rcptTo.length > 0) { message.setRecipients(Message.RecipientType.TO, rcptTo); } if (rcptCC != null && rcptCC.length > 0) { message.setRecipients(Message.RecipientType.CC, rcptCC); } if (rcptBCC != null && rcptBCC.length > 0) { message.setRecipients(Message.RecipientType.BCC, rcptBCC); } message.setSubject(subject); message.setContent(body, "text/plain"); //$NON-NLS-1$ Transport.send(message); } catch (AddressException e) { throw new RuntimeException(e); } catch (MessagingException e) { throw new RuntimeException(e); } }
From source file:cc.kune.core.server.mail.MailServiceDefault.java
@Override public void send(final String from, final AbstractFormattedString subject, final AbstractFormattedString body, final boolean isHtml, final String... tos) { if (smtpSkip) { return;/*w w w.j a v a 2 s.c om*/ } // Get session final Session session = Session.getDefaultInstance(props, null); // Define message final MimeMessage message = new MimeMessage(session); for (final String to : tos) { try { message.setFrom(new InternetAddress(from)); // In case we should use utf8 also in address: // http://stackoverflow.com/questions/2656478/send-javax-mail-internet-mimemessage-to-a-recipient-with-non-ascii-name message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // If additional header should be added // message.addHeader(name, MimeUtility.encodeText(value, "utf-8", "B")); final String formatedSubject = subject.getString(); message.setSubject(formatedSubject, "utf-8"); final String formatedBody = body.getString(); if (isHtml) { // message.setContent(formatedBody, "text/html"); message.setText(formatedBody, "UTF-8", "html"); } else { message.setText(formatedBody, "UTF-8"); } // Send message Transport.send(message); } catch (final AddressException e) { } catch (final MessagingException e) { final String error = String.format("Error sendind an email to %s, with subject: %s, and body: %s", from, subject, to); log.error(error, e); // Better not to throw exceptions because users emails can be wrong... // throw new DefaultException(error, e); } } }
From source file:eagle.common.email.EagleMailClient.java
private boolean _send(String from, String to, String cc, String title, String content) { Message msg = new MimeMessage(session); try {//from ww w . j av a 2 s . co m msg.setFrom(new InternetAddress(from)); msg.setSubject(title); if (to != null) { msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); } if (cc != null) { msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc)); } //msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(DEFAULT_BCC_ADDRESS)); msg.setContent(content, "text/html;charset=utf-8"); LOG.info(String.format("Going to send mail: from[%s], to[%s], cc[%s], title[%s]", from, to, cc, title)); Transport.send(msg); return true; } catch (AddressException e) { LOG.info("Send mail failed, got an AddressException: " + e.getMessage(), e); return false; } catch (MessagingException e) { LOG.info("Send mail failed, got an AddressException: " + e.getMessage(), e); return false; } }
From source file:info.raack.appliancedetection.common.email.SMTPEmailSender.java
public SMTPEmailSender() { emailProperties = new Properties(); emailProperties.put("mail.smtp.host", "smtp.gmail.com"); emailProperties.put("mail.smtp.socketFactory.port", "465"); emailProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); emailProperties.put("mail.smtp.auth", "true"); emailProperties.put("mail.smtp.port", "465"); emailQueue = new LinkedBlockingQueue<Message>(); emailThread = new StoppableThread(new StoppableThreadAction() { public void doAction() throws InterruptedException { sendGeneralEmail(emailQueue.take()); }//from w w w. j a v a2 s . c om private void sendGeneralEmail(Message message) { try { logger.debug("Sending email to " + message.getRecipients(Message.RecipientType.TO)[0]); Transport.send(message); } catch (Exception e) { throw new RuntimeException("Could not send email", e); } } public String getName() { return "email sender"; } }); emailThread.start(); }
From source file:ua.aits.crc.controller.MainController.java
@RequestMapping(value = { "/sendmail/", "/sendmail" }, method = RequestMethod.GET) public @ResponseBody String sendMail(HttpServletRequest request, HttpServletResponse response) throws Exception { request.setCharacterEncoding("UTF-8"); String name = request.getParameter("name"); String email = request.getParameter("email"); String text = request.getParameter("text"); final String username = "office@crc.org.ua"; final String password = "crossroad2000"; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected javax.mail.PasswordAuthentication getPasswordAuthentication() { return new javax.mail.PasswordAuthentication(username, password); }// ww w . j a va2 s . c o m }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(email)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("office@crc.org.ua")); message.setSubject("Mail from site"); message.setText("Name: " + name + "\nEmail: " + email + "\n\n" + text); Transport.send(message); return "done"; } catch (MessagingException e) { throw new RuntimeException(e); } }