List of usage examples for javax.mail Transport connect
public void connect(String host, String user, String password) throws MessagingException
From source file:com.wso2telco.workflow.notification.EmailService.java
public void sendEmail(final String emailAddress, final String subject, final String content) { new Thread() { @Override/*w ww . j a v a 2s.com*/ public void run() { Map<String, String> workflowProperties = WorkflowProperties.loadWorkflowPropertiesFromXML(); String emailHost = workflowProperties.get(Constants.KEY_WORKFLOW_EMAIL_NOTIFICATION_HOST); String fromEmailAddress = workflowProperties .get(Constants.KEY_WORKFLOW_EMAIL_NOTIFICATION_FROM_ADDRESS); String fromEmailPassword = workflowProperties .get(Constants.KEY_WORKFLOW_EMAIL_NOTIFICATION_FROM_PASSWORD); Properties props = System.getProperties(); props.put("mail.smtp.host", emailHost); props.put("mail.smtp.user", fromEmailAddress); props.put("mail.smtp.password", fromEmailPassword); props.put("mail.smtp.port", "587"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.auth", "true"); try { Session session = Session.getDefaultInstance(props, null); InternetAddress toAddress = new InternetAddress(emailAddress); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(fromEmailAddress)); message.addRecipient(Message.RecipientType.TO, toAddress); message.setSubject(subject); message.setContent(content, "text/html; charset=UTF-8"); Transport transport = session.getTransport("smtp"); transport.connect(emailHost, fromEmailAddress, fromEmailPassword); transport.sendMessage(message, message.getAllRecipients()); transport.close(); } catch (Exception e) { log.error("Email sending failed. ", e); } } }.start(); }
From source file:com.warsaw.data.controller.LoginController.java
private boolean sendEmail(String to) { Properties properties = System.getProperties(); // Setup mail server properties.put("mail.smtp.starttls.enable", "true"); properties.put("mail.smtp.host", HOST); properties.put("mail.smtp.user", EMAIL); properties.put("mail.smtp.password", PASS); properties.put("mail.smtp.port", "587"); properties.put("mail.smtp.auth", "true"); properties.setProperty("mail.transport.protocol", "smtp"); // Get the default Session object. Session session = Session.getDefaultInstance(properties); try {//from w w w . j ava2 s . co m Message msg = this.buildEmail(session, to); /* Transport transport = session.getTransport(); transport.send(msg);*/ Transport transport = session.getTransport("smtp"); transport.connect(HOST, EMAIL, PASS); transport.sendMessage(msg, msg.getAllRecipients()); transport.close(); } catch (Exception e) { System.out.println("Ex :" + e); return false; } return true; }
From source file:com.fstx.stdlib.common.messages.MailSenderImpl.java
/** * @see com.ess.messages.MailSender#send() *///from www . j av a 2s. co m public boolean send() { try { // 2005-11-27 RSC always requires authentication. Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.host", host.getAddress()); /* * 2005-11-27 RSC * Since webmail.us is starting to make other ports available * as Comcast blocks port 25. */ props.put("mail.smtp.port", host.getPort()); Session s = Session.getInstance(props, null); MimeMessage messageOut = new MimeMessage(s); InternetAddress fromOut = new InternetAddress(from.getAddress()); //reid 2004-12-20 fromOut.setPersonal(from.getName()); messageOut.setFrom(fromOut); InternetAddress toOut = new InternetAddress(this.to.getAddress()); //reid 2004-12-20 toOut.setPersonal(to.getName()); messageOut.addRecipient(javax.mail.Message.RecipientType.TO, toOut); messageOut.setSubject(message.getSubject()); messageOut.setText(message.getMessage()); if (host.useAuthentication()) { Transport transport = s.getTransport("smtp"); transport.connect(host.getAddress(), host.getUsername(), host.getPassword()); transport.sendMessage(messageOut, messageOut.getAllRecipients()); transport.close(); } else { Transport.send(messageOut); } } catch (Exception e) { log.info("\n\nMailSenderIMPL3: " + host.getAddress()); e.printStackTrace(); throw new RuntimeException(e); } return true; }
From source file:org.opens.emailsender.EmailSender.java
/** * * @param emailFrom//from w w w. ja va 2s . co m * @param emailToSet * @param emailBccSet (can be null) * @param replyTo (can be null) * @param emailSubject * @param emailContent */ public void sendEmail(String emailFrom, Set<String> emailToSet, Set<String> emailBccSet, String replyTo, String emailSubject, String emailContent) { boolean debug = false; // Set the host smtp address Properties props = new Properties(); props.put("mail.smtp.host", smtpHost); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); // create some properties and get the default Session Session session = Session.getInstance(props); session.setDebug(debug); try { Transport t = session.getTransport("smtp"); t.connect(smtpHost, userName, password); // create a message MimeMessage msg = new MimeMessage(session); // set the from and to address InternetAddress addressFrom; try { // Used default from address is passed one is null or empty or // blank addressFrom = (StringUtils.isNotBlank(emailFrom)) ? new InternetAddress(emailFrom) : new InternetAddress(from); msg.setFrom(addressFrom); Address[] recipients = new InternetAddress[emailToSet.size()]; int i = 0; for (String emailTo : emailToSet) { recipients[i] = new InternetAddress(emailTo); i++; } msg.setRecipients(Message.RecipientType.TO, recipients); if (CollectionUtils.isNotEmpty(emailBccSet)) { Address[] bccRecipients = new InternetAddress[emailBccSet.size()]; i = 0; for (String emailBcc : emailBccSet) { bccRecipients[i] = new InternetAddress(emailBcc); i++; } msg.setRecipients(Message.RecipientType.BCC, bccRecipients); } if (StringUtils.isNotBlank(replyTo)) { Address[] replyToRecipients = { new InternetAddress(replyTo) }; msg.setReplyTo(replyToRecipients); } // Setting the Subject msg.setSubject(emailSubject, CHARSET_KEY); // Setting content and charset (warning: both declarations of // charset are needed) msg.setHeader(CONTENT_TYPE_KEY, FULL_CHARSET_KEY); LOGGER.error("emailContent " + emailContent); msg.setContent(emailContent, FULL_CHARSET_KEY); try { LOGGER.debug("emailContent from message object " + msg.getContent().toString()); } catch (IOException ex) { LOGGER.error(ex.getMessage()); } catch (MessagingException ex) { LOGGER.error(ex.getMessage()); } for (Address addr : msg.getAllRecipients()) { LOGGER.error("addr " + addr); } t.sendMessage(msg, msg.getAllRecipients()); } catch (AddressException ex) { LOGGER.warn(ex.getMessage()); } } catch (NoSuchProviderException e) { LOGGER.warn(e.getMessage()); } catch (MessagingException e) { LOGGER.warn(e.getMessage()); } }
From source file:com.fsrin.menumine.common.message.MailSenderImpl.java
/** * @see com.ess.messages.MailSender#send() *//* w w w . j a v a2 s . c o m*/ public boolean send() { try { Properties props = new Properties(); props.put("mail.smtp.host", host.getAddress()); if (host.useAuthentication()) { props.put("mail.smtp.auth", "true"); } Session s = Session.getInstance(props, null); s.setDebug(true); // PasswordAuthentication pa = new PasswordAuthentication(host // .getUsername(), host.getPassword()); // // URLName url = new URLName(host.getAddress()); // // s.setPasswordAuthentication(url, pa); MimeMessage messageOut = new MimeMessage(s); InternetAddress fromOut = new InternetAddress(from.getAddress()); //reid 2004-12-20 fromOut.setPersonal(from.getName()); messageOut.setFrom(fromOut); InternetAddress toOut = new InternetAddress(this.to.getAddress()); //reid 2004-12-20 toOut.setPersonal(to.getName()); messageOut.addRecipient(javax.mail.Message.RecipientType.TO, toOut); messageOut.setSubject(message.getSubject()); messageOut.setText(message.getMessage()); if (host.useAuthentication()) { Transport transport = s.getTransport("smtp"); transport.connect(host.getAddress(), host.getUsername(), host.getPassword()); transport.sendMessage(messageOut, messageOut.getAllRecipients()); transport.close(); } else { Transport.send(messageOut); } } catch (Exception e) { log.info("\n\nMailSenderIMPL3: " + host.getAddress()); e.printStackTrace(); throw new RuntimeException(e); } return true; }
From source file:org.asqatasun.emailsender.EmailSender.java
/** * * @param emailFrom//w w w .ja v a 2s . c o m * @param emailToSet * @param emailBccSet (can be null) * @param replyTo (can be null) * @param emailSubject * @param emailContent */ public void sendEmail(String emailFrom, Set<String> emailToSet, Set<String> emailBccSet, String replyTo, String emailSubject, String emailContent) { boolean debug = false; // Set the host smtp address Properties props = new Properties(); props.put("mail.smtp.host", smtpHost); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); // create some properties and get the default Session Session session = Session.getInstance(props); session.setDebug(debug); try { Transport t = session.getTransport("smtp"); t.connect(smtpHost, userName, password); // create a message MimeMessage msg = new MimeMessage(session); // set the from and to address InternetAddress addressFrom; try { // Used default from address is passed one is null or empty or // blank addressFrom = (StringUtils.isNotBlank(emailFrom)) ? new InternetAddress(emailFrom) : new InternetAddress(from); msg.setFrom(addressFrom); Address[] recipients = new InternetAddress[emailToSet.size()]; int i = 0; for (String emailTo : emailToSet) { recipients[i] = new InternetAddress(emailTo); i++; } msg.setRecipients(Message.RecipientType.TO, recipients); if (CollectionUtils.isNotEmpty(emailBccSet)) { Address[] bccRecipients = new InternetAddress[emailBccSet.size()]; i = 0; for (String emailBcc : emailBccSet) { bccRecipients[i] = new InternetAddress(emailBcc); i++; } msg.setRecipients(Message.RecipientType.BCC, bccRecipients); } if (StringUtils.isNotBlank(replyTo)) { Address[] replyToRecipients = { new InternetAddress(replyTo) }; msg.setReplyTo(replyToRecipients); } // Setting the Subject msg.setSubject(emailSubject, CHARSET_KEY); // Setting content and charset (warning: both declarations of // charset are needed) msg.setHeader(CONTENT_TYPE_KEY, FULL_CHARSET_KEY); LOGGER.debug("emailContent " + emailContent); msg.setContent(emailContent, FULL_CHARSET_KEY); try { LOGGER.debug("emailContent from message object " + msg.getContent().toString()); } catch (IOException ex) { LOGGER.error(ex.getMessage()); } catch (MessagingException ex) { LOGGER.error(ex.getMessage()); } for (Address addr : msg.getAllRecipients()) { LOGGER.debug("addr " + addr); } t.sendMessage(msg, msg.getAllRecipients()); } catch (AddressException ex) { LOGGER.warn("AddressException " + ex.getMessage()); LOGGER.warn("AddressException " + ex.getStackTrace()); } } catch (NoSuchProviderException e) { LOGGER.warn("NoSuchProviderException " + e.getMessage()); LOGGER.warn("NoSuchProviderException " + e.getStackTrace()); } catch (MessagingException e) { LOGGER.warn("MessagingException " + e.getMessage()); LOGGER.warn("MessagingException " + e.getStackTrace()); } }
From source file:org.openiam.idm.srvc.msg.service.MailSenderClient.java
public void send(Message msg) { Properties properties = System.getProperties(); properties.setProperty("mail.smtp.host", host); properties.setProperty("mail.transport.protocol", "smtp"); if (username != null && !username.isEmpty()) { properties.setProperty("mail.user", username); properties.setProperty("mail.password", password); }/* w w w . j ava2 s . c o m*/ if (port != null && !port.isEmpty()) { properties.setProperty("mail.smtp.port", port); } Session session = Session.getDefaultInstance(properties); MimeMessage message = new MimeMessage(session); try { message.setFrom(msg.getFrom()); if (msg.getTo().size() > 1) { List<InternetAddress> addresses = msg.getTo(); message.addRecipients(TO, addresses.toArray(new Address[addresses.size()])); } else { message.addRecipient(TO, msg.getTo().get(0)); } if (msg.getBcc() != null && msg.getBcc().size() != 0) { if (msg.getTo().size() > 1) { List<InternetAddress> addresses = msg.getBcc(); message.addRecipients(BCC, addresses.toArray(new Address[addresses.size()])); } else { message.addRecipient(TO, msg.getBcc().get(0)); } } if (msg.getCc() != null && msg.getCc().size() > 0) { if (msg.getCc().size() > 1) { List<InternetAddress> addresses = msg.getCc(); message.addRecipients(CC, addresses.toArray(new Address[addresses.size()])); } else { message.addRecipient(CC, msg.getCc().get(0)); } } message.setSubject(msg.getSubject(), "UTF-8"); MimeBodyPart mbp1 = new MimeBodyPart(); // create the Multipart and add its parts to it Multipart mp = new MimeMultipart(); if (msg.getBodyType() == Message.BodyType.HTML_TEXT) { mbp1.setContent(msg.getBody(), "text/html"); } else { mbp1.setText(msg.getBody(), "UTF-8"); } if (port != null && !port.isEmpty()) { properties.setProperty("mail.smtp.port", port); } mp.addBodyPart(mbp1); if (msg.getAttachments().size() > 0) { for (String fileName : msg.getAttachments()) { // create the second message part MimeBodyPart mbpFile = new MimeBodyPart(); // attach the file to the message FileDataSource fds = new FileDataSource(fileName); mbpFile.setDataHandler(new DataHandler(fds)); mbpFile.setFileName(fds.getName()); mp.addBodyPart(mbpFile); } } // add the Multipart to the message message.setContent(mp); if (username != null && !username.isEmpty()) { properties.setProperty("mail.user", username); properties.setProperty("mail.password", password); properties.put("mail.smtp.auth", auth); properties.put("mail.smtp.starttls.enable", starttls); Transport mailTransport = session.getTransport(); mailTransport.connect(host, username, password); mailTransport.sendMessage(message, message.getAllRecipients()); } else { Transport.send(message); log.debug("Message successfully sent."); } } catch (Throwable e) { log.error("Exception while sending mail", e); } }
From source file:org.wandora.piccolo.actions.SendEmail.java
public void doAction(User user, javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, Application application) {//www.j a v a2 s . c om try { Properties props = new Properties(); props.put("mail.smtp.host", smtpServer); Session session = Session.getDefaultInstance(props, null); MimeMessage message = new MimeMessage(session); if (subject != null) message.setSubject(subject); if (from != null) message.setFrom(new InternetAddress(from)); Vector<String> recipients = new Vector<String>(); if (recipient != null) { String[] rs = recipient.split(","); String r = null; for (int i = 0; i < rs.length; i++) { r = rs[i]; if (r != null) recipients.add(r); } } MimeMultipart multipart = new MimeMultipart(); Template template = application.getTemplate(emailTemplate, user); HashMap context = new HashMap(); context.put("request", request); context.put("message", message); context.put("recipients", recipients); context.put("emailhelper", new MailHelper()); context.put("multipart", multipart); context.putAll(application.getDefaultContext(user)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); template.process(context, baos); MimeBodyPart mimeBody = new MimeBodyPart(); mimeBody.setContent(new String(baos.toByteArray(), template.getEncoding()), template.getMimeType()); // mimeBody.setContent(baos.toByteArray(),template.getMimeType()); multipart.addBodyPart(mimeBody); message.setContent(multipart); Transport transport = session.getTransport("smtp"); transport.connect(smtpServer, smtpUser, smtpPass); Address[] recipientAddresses = new Address[recipients.size()]; for (int i = 0; i < recipientAddresses.length; i++) { recipientAddresses[i] = new InternetAddress(recipients.elementAt(i)); } transport.sendMessage(message, recipientAddresses); } catch (Exception e) { logger.writelog("WRN", e); } }
From source file:fsi_admin.JSmtpConn.java
private boolean sendMsg(String HOST, String USERNAME, String PASSWORD, StringBuffer msj, Session session, MimeMessage mmsg, MimeMultipart multipart) { try {//from ww w . jav a 2s.c o m mmsg.setContent(multipart); // Create a transport. Transport transport = session.getTransport(); // Send the message. System.out.println(HOST + " " + USERNAME + " " + PASSWORD); transport.connect(HOST, USERNAME, PASSWORD); // Send the email. transport.sendMessage(mmsg, mmsg.getAllRecipients()); transport.close(); return true; } catch (MessagingException e) { e.printStackTrace(); msj.append("Error de Mensajeria al enviar SMTP: " + e.getMessage()); return false; } catch (Exception ex) { ex.printStackTrace(); msj.append("Error general de mensaje al enviar SMTP: " + ex.getMessage()); return false; } }
From source file:org.exoplatform.chat.server.ChatServer.java
public void sendMailWithAuth(String senderFullname, List<String> toList, String htmlBody, String subject) throws Exception { String host = PropertyManager.getProperty(PropertyManager.PROPERTY_MAIL_HOST); String user = PropertyManager.getProperty(PropertyManager.PROPERTY_MAIL_USER); String password = PropertyManager.getProperty(PropertyManager.PROPERTY_MAIL_PASSWORD); String port = PropertyManager.getProperty(PropertyManager.PROPERTY_MAIL_PORT); Properties props = System.getProperties(); props.put("mail.smtp.user", user); props.put("mail.smtp.password", password); props.put("mail.smtp.host", host); props.put("mail.smtp.port", port); //props.put("mail.debug", "true"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.EnableSSL.enable", "true"); Session session = Session.getInstance(props, null); //session.setDebug(true); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(user, senderFullname)); // To get the array of addresses for (String to : toList) { message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); }//from w w w . ja va 2 s . c o m message.setSubject(subject); message.setContent(htmlBody, "text/html"); Transport transport = session.getTransport("smtp"); try { transport.connect(host, user, password); transport.sendMessage(message, message.getAllRecipients()); } finally { transport.close(); } }