List of usage examples for javax.mail Session getDefaultInstance
public static synchronized Session getDefaultInstance(Properties props, Authenticator authenticator)
From source file:org.roda.core.util.EmailUtility.java
/** * @param from//from ww w . ja v a 2s. 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:ro.agrade.jira.qanda.listeners.DirectEmailMessageHandler.java
@Override protected void sendMail(String[] recipients, String subject, String message, String from) throws MessagingException { SMTPMailServer server = mailServerManager.getDefaultSMTPMailServer(); if (server == null) { LOG.debug("Email server is not configured. QandA is unable to send mails ..."); return;/* w w w . j a v a2 s . com*/ } LOG.debug("Email message: initializing."); //Set the host smtp address Properties props = new Properties(); String proto = server.getMailProtocol().getProtocol(); props.put("mail.transport.protocol", proto); props.put("mail." + proto + ".host", server.getHostname()); props.put("mail." + proto + ".port", server.getPort()); String username = server.getUsername(); String password = server.getPassword(); Authenticator auth = null; if (username != null && password != null) { auth = new SMTPAuthenticator(username, password); props.put("mail." + proto + ".auth", "true"); } Session session; try { session = auth != null ? Session.getDefaultInstance(props, auth) : Session.getDefaultInstance(props); } catch (SecurityException ex) { LOG.warn("Could not get default session. Attempting to create a new one."); session = auth != null ? Session.getInstance(props, auth) : Session.getInstance(props); } // create a message MimeMessage msg = new MimeMessage(session); Multipart multipart = new MimeMultipart(); if (from == null) { from = server.getDefaultFrom(); } // set the from address if (from != null) { InternetAddress addressFrom = new InternetAddress(from); msg.setFrom(addressFrom); } if (recipients != null && recipients.length > 0) { // set TO address(es) 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); } // Setting the Subject msg.setSubject(subject); // Setting text content MimeBodyPart contentPart = new MimeBodyPart(); contentPart.setContent(message, "text/html; charset=utf-8"); multipart.addBodyPart(contentPart); msg.setContent(multipart); Transport.send(msg); LOG.debug("Email message sent successfully."); }
From source file:de.fzi.ALERT.actor.ActionActuator.MailService.java
public void sendEmail(Authenticator auth, String address, String subject, String content) { try {//from www . j av a 2 s.c o m Properties props = new Properties(); props.put("mail.smtp.host", host); props.put("mail.smtp.port", port); props.put("mail.smtp.auth", true); props.put("mail.smtp.starttls.enable", "true"); Session session = Session.getDefaultInstance(props, auth); // -- Create a new message -- Message msg = new MimeMessage(session); // -- Set the FROM and TO fields -- msg.setFrom(new InternetAddress(username)); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(address, false)); // -- Set the subject and body text -- msg.setSubject(subject); msg.setText(content); // -- Set some other header information -- msg.setHeader("X-Mailer", "LOTONtechEmail"); msg.setSentDate(new Date()); // -- Send the message -- Transport.send(msg); System.out.println("An announce Mail has been send to " + address); } catch (AddressException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:com.bizintelapps.cars.service.impl.EmailServiceImpl.java
/** * //from w w w . j a va2s .co m * @param recipients * @param subject * @param message * @param from * @throws MessagingException */ private void sendSSMessage(String recipients[], String subject, Car car, String comment) { try { InternetAddress[] addressTo = new InternetAddress[recipients.length]; for (int i = 0; i < recipients.length; i++) { if (recipients[i] != null && recipients[i].length() > 0) { addressTo[i] = new InternetAddress(recipients[i]); } } if (addressTo == null || addressTo.length == 0) { return; } boolean debug = true; Properties props = new Properties(); props.put("mail.smtp.host", SMTP_HOST_NAME); props.put("mail.smtp.auth", "true"); props.put("mail.debug", "true"); props.put("mail.smtp.port", SMTP_PORT); props.put("mail.smtp.socketFactory.port", SMTP_PORT); props.put("mail.smtp.socketFactory.class", SSL_FACTORY); props.put("mail.smtp.socketFactory.fallback", "false"); Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(SEND_FROM_USERNAME, SEND_FROM_PASSWORD); } }); session.setDebug(debug); Message msg = new MimeMessage(session); InternetAddress addressFrom = new InternetAddress(EMAIL_FROM_ADDRESS); msg.setFrom(addressFrom); msg.setRecipients(Message.RecipientType.TO, addressTo); // Setting the Subject and Content Type msg.setSubject(subject); String message = buildMessage(car, comment); msg.setContent(message, EMAIL_CONTENT_TYPE); Transport.send(msg); } catch (Exception ex) { logger.warn(ex.getMessage(), ex); throw new RuntimeException("Error sending email, please check to and from emails are correct!"); } }
From source file:net.sourceforge.subsonic.backend.service.EmailSession.java
public EmailSession() throws Exception { Properties props = new Properties(); // props.setProperty("mail.debug", "true"); props.setProperty("mail.store.protocol", "pop3s"); props.setProperty("mail.smtps.host", SMTP_MAIL_SERVER); props.setProperty("mail.smtps.auth", "true"); props.setProperty("mail.smtps.timeout", "10000"); props.setProperty("mail.smtps.connectiontimeout", "10000"); props.setProperty("mail.pop3s.timeout", "10000"); props.setProperty("mail.pop3s.connectiontimeout", "10000"); session = Session.getDefaultInstance(props, null); password = Util.getPassword("gmailpwd.txt"); }
From source file:org.nekorp.workflow.desktop.servicio.imp.ServicioAlertaEmailTemplate.java
public Session buildSession() { Properties props = new Properties(); props.put("mail.smtp.host", smtpHost); props.put("mail.smtp.socketFactory.port", smtpPort); props.put("mail.smtp.socketFactory.class", smtpFactoryClass); props.put("mail.smtp.socketFactory.fallback", fallBack); props.put("mail.smtp.starttls.enable", starttls); props.put("mail.smtp.auth", smtpAuth); props.put("mail.smtp.port", smtpPort); props.put("mail.smtp.debug", debug); Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { @Override/*from ww w . j a va 2 s. co m*/ protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password); } }); return session; }
From source file:org.apache.camel.component.google.mail.GmailUsersThreadsIntegrationTest.java
private Message createThreadedTestEmail(String previousThreadId) throws MessagingException, IOException { com.google.api.services.gmail.model.Profile profile = requestBody( "google-mail://users/getProfile?inBody=userId", CURRENT_USERID); Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); MimeMessage mm = new MimeMessage(session); mm.addRecipients(javax.mail.Message.RecipientType.TO, profile.getEmailAddress()); mm.setSubject("Hello from camel-google-mail"); mm.setContent("Camel rocks!", "text/plain"); Message createMessageWithEmail = createMessageWithEmail(mm); if (previousThreadId != null) { createMessageWithEmail.setThreadId(previousThreadId); }/* w w w . ja v a2 s. com*/ Map<String, Object> headers = new HashMap<String, Object>(); // parameter type is String headers.put("CamelGoogleMail.userId", CURRENT_USERID); // parameter type is com.google.api.services.gmail.model.Message headers.put("CamelGoogleMail.content", createMessageWithEmail); return requestBodyAndHeaders("google-mail://messages/send", null, headers); }
From source file:com.wso2telco.workflow.notification.EmailService.java
public void sendEmail(final String emailAddress, final String subject, final String content) { new Thread() { @Override//from w ww .j ava 2s . co m 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:org.bml.util.mail.MailUtils.java
/** * Simple mail utility// ww w . ja v a 2 s .c o m * @param sendToAdresses email addresses to send the mail to * @param emailSubjectLine the subject of the email. * @param emailBody The body of the mail * @param smtpHost the smtp host * @param sender the mail address that is the sender * @param smtpPassword the password for the sender * @param smtpPort the port to contact the smtp server on * @return boolean true on success and false on error */ public static boolean sendMail(final String[] sendToAdresses, final String emailSubjectLine, final String emailBody, final String smtpHost, String sender, String smtpPassword, final int smtpPort) { if ((sendToAdresses == null) || (sendToAdresses.length == 0)) { return false; } if ((emailSubjectLine == null) || (emailBody == null)) { return false; } try { Address[] addresses = new Address[sendToAdresses.length]; for (int i = 0; i < sendToAdresses.length; i++) { addresses[i] = new InternetAddress(sendToAdresses[i]); } Properties props = System.getProperties(); props.setProperty("mail.smtp.host", smtpHost); props.setProperty("mail.smtp.localhost", smtpHost); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.smtp.port", String.valueOf(smtpPort)); props.put("mail.smtp.starttls.enable", "true"); Session session = Session.getDefaultInstance(props, null); Message message = new MimeMessage(session); message.setFrom(new InternetAddress(sender)); message.setRecipients(Message.RecipientType.TO, addresses); message.setSubject(emailSubjectLine); message.setContent(emailBody, "text/plain"); message.saveChanges(); Transport transport = session.getTransport("smtp"); transport.connect(smtpHost, sender, smtpPassword); transport.sendMessage(message, message.getAllRecipients()); transport.close(); } catch (Throwable t) { if (LOG.isErrorEnabled()) { LOG.error("Error occured while sending mail.", t); } return false; } return true; }
From source file:com.basicservice.service.MailService.java
public void sendEmail(String from, String to, String subject, String messageHtml) throws Exception { try {//from w w w . j a va 2 s .co m Properties props = new Properties(); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.host", smtpHost); props.put("mail.smtp.port", 587); props.put("mail.smtp.auth", "true"); Authenticator auth = new SMTPAuthenticator(); Session mailSession = Session.getDefaultInstance(props, auth); // mailSession.setDebug(true); Transport transport = mailSession.getTransport(); MimeMessage message = new MimeMessage(mailSession); Multipart multipart = new MimeMultipart("alternative"); BodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(new String(messageHtml.getBytes("UTF8"), "ISO-8859-1"), "text/html"); multipart.addBodyPart(htmlPart); message.setContent(multipart); message.setFrom(new InternetAddress(from)); message.setSubject(subject, "UTF-8"); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); transport.connect(); transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); transport.close(); } catch (Exception e) { LOG.debug("Exception while sending email: ", e); throw e; } }