List of usage examples for javax.mail Session getInstance
public static Session getInstance(Properties props, Authenticator authenticator)
From source file:com.email.SendEmailNotification.java
/** * This sends a basic notification email that is just pure TEXT. Message is * sent from the section gathered/* w w w. j a v a2s . c om*/ * * @param eml DocketNotificationModel */ public static void sendNotificationEmail(DocketNotificationModel eml) { //Get Account SystemEmailModel account = null; for (SystemEmailModel acc : Global.getSystemEmailParams()) { if (acc.getSection().equals(eml.getSection())) { account = acc; break; } } if (account != null) { String FROMaddress = account.getEmailAddress(); String[] TOAddressess = ((eml.getSendTo() == null) ? "".split(";") : eml.getSendTo().split(";")); String subject = eml.getMessageSubject(); String body = eml.getMessageBody(); Authenticator auth = EmailAuthenticator.setEmailAuthenticator(account); Properties properties = EmailProperties.setEmailOutProperties(account); Session session = Session.getInstance(properties, auth); MimeMessage email = new MimeMessage(session); try { for (String To : TOAddressess) { if (EmailValidator.getInstance().isValid(To)) { email.addRecipient(Message.RecipientType.TO, new InternetAddress(To)); } } email.setFrom(new InternetAddress(FROMaddress)); email.setSubject(subject); email.setText(body); if (Global.isOkToSendEmail()) { Transport.send(email); } else { Audit.addAuditEntry("Notification Not Actually Sent: " + eml.getId() + " - " + subject); } DocketNotification.deleteEmailEntry(eml.getId()); } catch (AddressException ex) { ExceptionHandler.Handle(ex); } catch (MessagingException ex) { ExceptionHandler.Handle(ex); } } }
From source file:com.mimp.hibernate.HiberMail.java
public static void generateAndSendEmail(String correo, String pass_plano, String user) { final String username = "formacionadopcion@gmail.com"; final String password = "cairani."; 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 PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); }/* w w w . ja v a 2 s . c o m*/ }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress("formacionadopcion@gmail.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(correo)); message.setSubject("Sistema de adopciones"); message.setText("Estimado solicitante," + "\n\n Su solicitud de recuperacin de contrasea ha sido procesada. Su usuario y contrasea para acceder a la plataforma de adopciones son los siguientes:" + "\n\n Usuario: " + user + "\n\n Contrasea: " + pass_plano + "\n\n Saludos cordiales, "); Transport.send(message); } catch (Exception ex) { } /*catch (MessagingException e) { throw new RuntimeException(e); }*/ }
From source file:com.email.SendEmailCrashReport.java
/** * Sends crash email to predetermined list from the database. * * Also BCCs members of XLN team for notification of errors *///from ww w . j av a2s . c om public static void sendCrashEmail() { //Get Account SystemEmailModel account = null; for (SystemEmailModel acc : Global.getSystemEmailParams()) { if (acc.getSection().equals("ERROR")) { account = acc; break; } } if (account != null) { String FROMaddress = account.getEmailAddress(); List<String> TOAddresses = SystemErrorEmailList.getActiveEmailAddresses(); String[] BCCAddressess = ("Anthony.Perk@XLNSystems.com".split(";")); String subject = "SERB 3.0 Application Daily Error Report for " + Global.getMmddyyyy().format(Calendar.getInstance().getTime()); String body = buildBody(); Authenticator auth = EmailAuthenticator.setEmailAuthenticator(account); Properties properties = EmailProperties.setEmailOutProperties(account); Session session = Session.getInstance(properties, auth); MimeMessage email = new MimeMessage(session); try { for (String TO : TOAddresses) { if (EmailValidator.getInstance().isValid(TO)) { email.addRecipient(Message.RecipientType.TO, new InternetAddress(TO)); } } for (String BCC : BCCAddressess) { if (EmailValidator.getInstance().isValid(BCC)) { email.addRecipient(Message.RecipientType.BCC, new InternetAddress(BCC)); } } email.setFrom(new InternetAddress(FROMaddress)); email.setSubject(subject); email.setText(body); Transport.send(email); } catch (AddressException ex) { ExceptionHandler.Handle(ex); } catch (MessagingException ex) { ExceptionHandler.Handle(ex); } } else { System.out.println("No account found to send Error Email"); } }
From source file:org.capelin.mvc.mail.SMTPMailSender.java
protected boolean send(String recipient, String body, boolean html) { Properties props = System.getProperties(); props.put("mail.smtp.host", serverName); Session session = Session.getInstance(props, null); props.put("mail.from", sender); Message msg = new MimeMessage(session); try {/*from w w w . j a v a 2 s. c om*/ msg.setSubject(subject); msg.setFrom(); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient, false)); if (html) { msg.setContent(new String(body.getBytes(), "iso-8859-1"), "text/html; charset=iso-8859-1"); } else { msg.setText(body); } // Send the message: Transport.send(msg); log.debug("Email send to: " + sender); return true; } catch (MessagingException e) { log.info("Email Sending failed due to " + e); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; }
From source file:easyproject.service.Mail.java
public void sendMail() { Properties props = new Properties(); props.put("mail.debug", "true"); props.put("mail.smtp.auth", true); props.put("mail.smtp.starttls.enable", true); props.put("mail.smtp.host", servidorSMTP); props.put("mail.smtp.port", puerto); Session session = Session.getInstance(props, null); try {/*www . ja va 2s .c o m*/ MimeMessage message = new MimeMessage(session); message.addRecipient(Message.RecipientType.TO, new InternetAddress(destino)); message.setSubject(asunto); message.setSentDate(new Date()); message.setContent(mensaje, "text/html; charset=utf-8"); Transport tr = session.getTransport("smtp"); tr.connect(servidorSMTP, usuario, password); message.saveChanges(); tr.sendMessage(message, message.getAllRecipients()); tr.close(); } catch (MessagingException e) { e.printStackTrace(); } }
From source file:net.kamhon.ieagle.function.email.SendMail.java
public void send(String to, String cc, String bcc, String from, String subject, String text, String emailType) { // Session session = Session.getInstance(props, null); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(emailServerSetting.getUsername(), emailServerSetting.getPassword()); }//w w w. ja v a2s . c o m }); session.setDebug(emailServerSetting.isDebug()); try { Message msg = new MimeMessage(session); if (StringUtils.isNotBlank(from)) { msg.setFrom(new InternetAddress(from)); } else { msg.setFrom(); } msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false)); if (StringUtils.isNotBlank(cc)) { msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false)); } if (StringUtils.isNotBlank(bcc)) { msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false)); } msg.setSubject(subject); if (Emailq.TYPE_HTML.equalsIgnoreCase(emailType)) { msg.setContent(text, "text/html"); } else { msg.setText(text); } msg.setSentDate(new java.util.Date()); Transport.send(msg); } catch (MessagingException mex) { mex.printStackTrace(); Exception ex = null; if ((ex = mex.getNextException()) != null) { ex.printStackTrace(); } throw new SystemErrorException(mex); } }
From source file:org.xmlactions.email.EMailSend.java
public static void sendEMail(String fromAddress, String toAddress, String host, String userName, String password, String subject, String msg) throws AddressException, MessagingException { log.debug(String.format(/*ww w.j av a2 s . c o m*/ "sendEMail(from:%s, to:%s, host:%s, userName:%s, password:%s)\nsubject:{" + subject + "\n}\nmessage:{" + msg + "\n}", fromAddress, toAddress, host, userName, toPassword(password), subject, msg)); Properties props = System.getProperties(); props.put("mail.smtp.host", host); Session session; if (!StringUtils.isEmpty(password)) { props.put("mail.smtp.auth", "true"); //EMailAuthenticator auth = new EMailAuthenticator(userName + "+" + host, password); EMailAuthenticator auth = new EMailAuthenticator(userName, password); session = Session.getInstance(props, auth); } else { session = Session.getInstance(props); } // Define message MimeMessage message = new MimeMessage(session); // message.setFrom(new InternetAddress("email_addresses@riostl.com")); message.setFrom(new InternetAddress(fromAddress)); message.addRecipient(RecipientType.TO, new InternetAddress(toAddress)); message.setSubject(subject); message.setText(msg); // Send message if (StringUtils.isEmpty(password)) { Transport.send(message); } else { Provider provider = session.getProvider("smtp"); Transport transport = session.getTransport(provider); // Send message transport.connect(); transport.sendMessage(message, new Address[] { new InternetAddress(toAddress) }); transport.close(); } }
From source file:Utilities.SendEmailUsingGMailSMTP.java
public void enviarCorreo(String correo, String clave) { String to = correo;/*from ww w. ja v a 2 s .co m*/ final String username = "angelicabarrientosvera@gmail.com";//change accordingly final String password = "90445359D10s";//change accordingly // Assuming you are sending email through relay.jangosmtp.net String host = "smtp.gmail.com"; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", host); props.put("mail.smtp.port", "587"); props.put("mail.smtp.ssl.trust", "smtp.gmail.com"); // Get the Session object. Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { // Create a default MimeMessage object. Message message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(to)); // Set To: header field of the header. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); // Set Subject: header field message.setSubject("Testing Subject"); // Now set the actual message message.setText("\n" + "Use esta clave para poder restablecer la contrasea" + "\n" + "Tu cdigo para restaurar su cuenta es:" + clave); // Send message Transport.send(message); System.out.println("Sent message successfully...."); //return true; } catch (MessagingException e) { throw new RuntimeException(e); } }
From source file:easyproject.utils.SendMail.java
@Override public void run() { Properties props = new Properties(); props.put("mail.debug", "true"); props.put("mail.smtp.auth", true); props.put("mail.smtp.starttls.enable", true); props.put("mail.smtp.host", servidorSMTP); props.put("mail.smtp.port", puerto); Session session = Session.getInstance(props, null); try {//from w w w . j a v a2 s .c o m MimeMessage message1 = new MimeMessage(session); message1.addRecipient(Message.RecipientType.TO, new InternetAddress(destino)); message1.setSubject(asunto); message1.setSentDate(new Date()); message1.setContent(mensaje, "text/html; charset=utf-8"); Transport tr = session.getTransport("smtp"); tr.connect(servidorSMTP, usuario, password); message1.saveChanges(); tr.sendMessage(message1, message1.getAllRecipients()); tr.close(); } catch (MessagingException e) { e.printStackTrace(); } }
From source file:com.email.ReceiveEmail.java
/** * This account fetches emails from a specified account and marks the emails * as seen, only touches the email account does not delete or move any * information./* w w w . j a v a 2 s . c o m*/ * * @param account SystemEmailModel */ public static void fetchEmail(SystemEmailModel account) { Authenticator auth = EmailAuthenticator.setEmailAuthenticator(account); Properties properties = EmailProperties.setEmailInProperties(account); try { Session session = Session.getInstance(properties, auth); Store store = session.getStore(); store.connect(account.getIncomingURL(), account.getIncomingPort(), account.getUsername(), account.getPassword()); Folder fetchFolder = store.getFolder(account.getIncomingFolder()); if (account.getIncomingFolder().trim().equals("")) { fetchFolder = store.getFolder("INBOX"); } if (fetchFolder.exists()) { fetchFolder.open(Folder.READ_WRITE); Message[] msgs = fetchFolder.getMessages(); // USE THIS FOR UNSEEN MAIL TO SEEN MAIL //Flags seen = new Flags(Flags.Flag.SEEN); //FlagTerm unseenFlagTerm = new FlagTerm(seen, false); //Message[] msgs = fetchFolder.search(unseenFlagTerm); //fetchFolder.setFlags(msgs, seen, true); if (msgs.length > 0) { List<String> messageList = new ArrayList<>(); for (Message msg : msgs) { boolean notDuplicate = true; String headerText = Arrays.toString(msg.getHeader("Message-ID")); for (String header : messageList) { if (header.equals(headerText)) { notDuplicate = false; break; } } if (notDuplicate) { //Add to header to stop duplicates messageList.add(headerText); attachmentCount = 1; attachmentList = new ArrayList<>(); //Setup Email For dbo.Email EmailMessageModel eml = new EmailMessageModel(); String emailTime = String.valueOf(new Date().getTime()); eml.setSection(account.getSection()); eml = saveEnvelope(msg, msg, eml); eml.setId(EMail.InsertEmail(eml)); //After Email has been inserted Gather Attachments saveAttachments(msg, msg, eml); //Create Email Body eml = EmailBodyToPDF.createEmailBodyIn(eml, emailTime, attachmentList); //Insert Email Body As Attachment (so it shows up in attachment table during Docketing) EmailAttachment.insertEmailAttachment(eml.getId(), eml.getEmailBodyFileName()); //Flag email As ready to file so it is available in the docket tab of SERB3.0 eml.setReadyToFile(1); EMail.setEmailReadyToFile(eml); } if (deleteEmailEnabled) { // Will Delete message from server // Un Comment line below to run msg.setFlag(Flags.Flag.DELETED, true); } } } fetchFolder.close(true); store.close(); } } catch (MessagingException ex) { if (ex != null) { System.out.println("Unable to connect to email Server for: " + account.getEmailAddress() + "\nPlease ensure you are connected to the network and" + " try again."); } ExceptionHandler.Handle(ex); } }