public static boolean emailValidator(String email) { boolean isValid = false; if (containsString(email, "@") == false) { return false; } if (containsString(email, ".") == false) { return false; } /* * if (email.equals("*****@*****.**")) { return false; } */ try { // // Create InternetAddress object and validated the supplied // address which is this case is an email address. InternetAddress internetAddress = new InternetAddress(email); internetAddress.validate(); isValid = true; } catch (AddressException e) { System.out.println("You are in catch block -- Exception Occurred for: " + email); } return isValid; }
public static boolean isValidEmailAddress(String email) { boolean result = true; try { InternetAddress emailAddr = new InternetAddress(email); emailAddr.validate(); } catch (AddressException ex) { result = false; } return result; }
public ReturnStatus askpassword(String email, String userName) { System.out.println("askpassword"); User usr = null; String qry = "select u from User u where u.userName = '******'"; System.out.println("Query = " + qry); // Valideer correctheid email try { javax.mail.internet.InternetAddress ia = new javax.mail.internet.InternetAddress(email); ia.validate(); } catch (javax.mail.internet.AddressException e) { return new ReturnStatus(false, "email is niet juist formaat (server controlled)."); } List<User> users = em.createQuery(qry, User.class).setFirstResult(0).setMaxResults(1).getResultList(); System.out.println("Query uitgevoerd"); if (users != null) { System.out.println("Er lijkt resultaat"); if (!users.isEmpty()) { System.out.println("Proberen te lezen"); usr = users.get(0); System.out.println("User gevonden : " + usr.getUserName()); if (usr.getEmail().equalsIgnoreCase(email)) { System.out.println("email ok"); String message = "Uw password voor username " + userName + " is : " + usr.getPassword(); boolean result = emailBean.sendEmailSSL(email, "password aanvraag", message); if (result) { return new ReturnStatus(true, "Mail verzonden"); } else { return new ReturnStatus(true, "Mail niet verzonden"); } } else { System.out.println("email is niet hetzelfde : " + usr.getEmail() + " en " + email); return new ReturnStatus( false, "email is niet hetzelfde : " + usr.getEmail() + " en " + email); } } else { System.out.println("Toch geen resultaat"); return new ReturnStatus(false, "gebruiker is niet bekend"); } } else { System.out.println("User niet gevonden"); return new ReturnStatus(false, "gebruiker is niet bekend"); } }
public void execute() throws Exception { Model model = (Model) this.getCtx().getModel(); model.validate(); if (model.getErrors().isEmpty()) { // Do this part first 'cause it could fail if (model.email != null || model.url != null) { // Check the URL URL url = null; try { url = new URL(model.url); } catch (MalformedURLException ex) { model.setError("url", ex.getMessage()); } // Check the address InternetAddress listAddress = null; try { listAddress = new InternetAddress(model.email); listAddress.validate(); listAddress.setPersonal(model.name); } catch (AddressException ex) { model.setError("email", ex.getMessage()); } if (model.getErrors().isEmpty()) { try { Backend.instance().getAdmin().setListAddresses(model.listId, listAddress, url); } catch (InvalidListDataException ex) { if (ex.isOwnerAddress()) model.setError("email", "Addresses cannot end with -owner"); if (ex.isVerpAddress()) model.setError("email", "Conflicts with the VERP address format"); } catch (DuplicateListDataException ex) { if (ex.isAddressTaken()) model.setError("email", "That address is already in use"); if (ex.isUrlTaken()) model.setError("url", "That url is already in use"); } } } if (model.getErrors().isEmpty()) Backend.instance() .getListMgr() .setList( model.listId, model.name, model.description, model.welcomeMessage, model.holdSubs); } }
public static void validEmailAddress(String addr, boolean personal) throws ServiceException { if (addr.indexOf('@') == -1) throw AccountServiceException.INVALID_ATTR_VALUE( "address '" + addr + "' does not include domain", null); try { InternetAddress ia = new JavaMailInternetAddress(addr, true); // is this even needed? ia.validate(); if (!personal && ia.getPersonal() != null && !ia.getPersonal().equals("")) throw AccountServiceException.INVALID_ATTR_VALUE("invalid email address: " + addr, null); } catch (AddressException e) { throw AccountServiceException.INVALID_ATTR_VALUE("invalid email address: " + addr, e); } }
public ReturnStatus register(String email, String userName, String password) { System.out.println("register"); if (checkUser(userName)) { return new ReturnStatus(false, "Gebruiker bestaat al. Kies een andere naam."); } // Valideer correctheid email try { javax.mail.internet.InternetAddress ia = new javax.mail.internet.InternetAddress(email); ia.validate(); } catch (javax.mail.internet.AddressException e) { return new ReturnStatus(false, "email is niet juist formaat (server controlled)."); } User user = new User(); user.setEmail(email); user.setUserName(userName); user.setPassword(password); System.out.println("create : " + userName); em.persist(user); String subject = "Accountaanvraag"; String message = "Uw aanvraag voor gebruikersnaam " + userName + " en wachtwoord " + password + " is verwerkt."; boolean result = emailBean.sendEmailSSL(email, subject, message); // boolean result = emailBean.sendEmailTLS(email, subject, message); // boolean result = emailBean.sendEmailSSL_HTML(email, subject, message); if (result) { return new ReturnStatus(true, "Mail verzonden"); } else { return new ReturnStatus(true, "Mail niet verzonden"); } }
// Переопределяем метод для валидации поля @Override public void validate(FacesContext fc, UIComponent uic, Object o) throws ValidatorException { String email = (String) o; if (email != "") { try { InternetAddress emailAddr = new InternetAddress(email); emailAddr.validate(); } catch (AddressException ex) { FacesContext context = FacesContext.getCurrentInstance(); ResourceBundle bundle = context.getApplication().getResourceBundle(context, "msgs"); String msgStr = bundle.getString("validatorEmailErrorFormat"); FacesMessage message = new FacesMessage(); message.setDetail(msgStr); message.setSummary(msgStr); message.setSeverity(FacesMessage.SEVERITY_ERROR); throw new ValidatorException(message); } } }
private InternetAddress[] createInternetAddressArray(String emailsString, LinkedList<String> list) throws AddressException { emailsString = removeSpaceAll(emailsString); StringBuilder sb = new StringBuilder(); list = new LinkedList<String>(); char[] emailsArray = emailsString.toCharArray(); for (int i = 0; i < emailsArray.length; i++) { if (emailsArray[i] == ',') { list.add(sb.toString()); sb = new StringBuilder(); } else { System.out.println(sb.toString()); sb.append(emailsArray[i]); } if (i == emailsArray.length - 1) { if (emailsArray[i] != ',') { list.add(sb.toString()); } } } String[] emailsListArray = new String[list.size()]; for (int i = 0; i < list.size(); i++) { if (!list.get(i).isEmpty()) { emailsListArray[i] = list.get(i); } } list = new LinkedList<String>(); for (int i = 0; i < emailsListArray.length; i++) { if (emailsListArray[i] != null) { list.add(emailsListArray[i]); } } for (int i = 0; i < list.size(); i++) { System.out.println("Email: " + list.get(i)); } LinkedList<InternetAddress> addressList = new LinkedList<InternetAddress>(); LinkedList<String> falseAddressesList = new LinkedList<String>(); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); InternetAddress address = new InternetAddress(list.get(i)); boolean isValidate; try { address.validate(); isValidate = true; System.out.println("TRUEEE!!!"); } catch (AddressException e) { System.out.println("FALSEEEEEE!!"); isValidate = false; falseAddressesList.add(list.get(i)); } if (isValidate) { addressList.add(address); } } if (!falseAddressesList.isEmpty()) { for (int i = 0; i < falseAddressesList.size(); i++) { wrongMailsStringBuilder.append(falseAddressesList.get(i) + ", "); } wrongMails = wrongMailsStringBuilder.toString(); } InternetAddress[] addressArray = new InternetAddress[addressList.size()]; for (int i = 0; i < addressList.size(); i++) { addressArray[i] = addressList.get(i); } return addressArray; }
private void mailSend() { Properties properties = new Properties(); properties.setProperty("mail.smtp.auth", "true"); properties.setProperty("mail.smtp.starttls.enable", "true"); properties.setProperty("mail.smtp.host", "smtp.gmail.com"); properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); properties.setProperty("mail.smtp.port", "587"); Authenticator auth = new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, userPassword); } }; Session session = Session.getInstance(properties, auth); Message message = new MimeMessage(session); String subject = subjectField.getText(); String from = "338399047"; String mailMessage = referenceTextArea.getText(); String to = ""; to = toField.getText(); boolean validateCc = true; boolean validateBcc = true; boolean wrongMail = true; if (ccField.getText().isEmpty() && bccField.getText().isEmpty()) { validateCc = true; validateBcc = true; wrongMail = false; } else if (!ccField.getText().isEmpty() && bccField.getText().isEmpty()) { if (countMail(ccField.getText()) == 1) { try { InternetAddress ccAddress = new InternetAddress(ccField.getText()); ccAddress.validate(); validateCc = true; validateBcc = false; } catch (AddressException e1) { JOptionPane.showMessageDialog( null, "Please enter a correct mail in the cc field!\n Or leave the ccfield empty!"); wrongMail = true; validateCc = false; } if (validateCc) { wrongMail = false; } } else { wrongMail = false; } } else if (ccField.getText().isEmpty() && !bccField.getText().isEmpty()) { if (countMail(bccField.getText()) == 1) { try { InternetAddress bccAddress = new InternetAddress(bccField.getText()); bccAddress.validate(); validateBcc = true; validateCc = false; } catch (AddressException e1) { JOptionPane.showMessageDialog( null, "Please enter a correct mail in the bcc field!\n Or leave the bccfield empty!"); wrongMail = true; validateBcc = false; } if (validateBcc) { wrongMail = false; } } else { System.out.println("??? 1"); wrongMail = false; } } else if (!ccField.getText().isEmpty() && !bccField.getText().isEmpty()) { if (countMail(ccField.getText()) == 1 && countMail(bccField.getText()) == 1) { try { InternetAddress ccAddress = new InternetAddress(ccField.getText()); ccAddress.validate(); validateCc = true; } catch (AddressException e1) { validateCc = false; } try { InternetAddress bccAddress = new InternetAddress(bccField.getText()); bccAddress.validate(); validateBcc = true; } catch (AddressException e1) { validateBcc = false; } if (validateBcc && validateCc) { wrongMail = false; } else { JOptionPane.showMessageDialog( null, "Please enter a correct mail in the bcc and cc field!\n Or leave both fields empty!"); wrongMail = true; } } else { System.out.println("??? 2"); wrongMail = false; } } SendMessage objectAddress = this; if (!wrongMail) { try { message.setSubject(subject); message.setFrom(new InternetAddress(from, "JavaProjekt")); message.setText(mailMessage); if (objectAddress.countMail(to) == 1) { message.setRecipient(RecipientType.TO, new InternetAddress(to)); } else if (countMail(to) > 1) { InternetAddress[] addressArray = this.createInternetAddressArray(to, toList); // guck message.setRecipients(RecipientType.TO, addressArray); } if (countMail(ccField.getText()) > 1) { message.setRecipients( RecipientType.CC, this.createInternetAddressArray(ccField.getText(), ccList)); validateCc = false; } if (countMail(bccField.getText()) > 1 && countMail(ccField.getText()) == 1) { validateCc = false; } if (!ccField.getText().isEmpty() && validateCc) { message.setRecipient(RecipientType.CC, new InternetAddress(ccField.getText())); } System.out.println("FIRST TRY" + validateBcc); if (countMail(bccField.getText()) > 1) { System.out.println("TEST 2"); message.setRecipients( RecipientType.BCC, this.createInternetAddressArray(bccField.getText(), bccList)); validateBcc = false; } System.out.println("SECOND TRY" + validateBcc); if (countMail(ccField.getText()) > 1 && countMail(bccField.getText()) == 1) { validateBcc = false; } if (!bccField.getText().isEmpty() && validateBcc) { message.setRecipient(RecipientType.BCC, new InternetAddress(bccField.getText())); System.out.println("flop"); } System.out.println("THIRD TRY"); Transport.send(message); if (wrongMails.isEmpty()) { JOptionPane.showMessageDialog(null, "Your E-Mail was sent."); } else { JOptionPane.showMessageDialog( null, "Your E-Mail was sent except of following emails: \n" + wrongMails + "\n" + "Please enter your email addresses in a corect format!\nFor example: [email protected]", "Warning!", JOptionPane.WARNING_MESSAGE); wrongMails = ""; wrongMailsStringBuilder = new StringBuilder(); } System.out.println("Finished!"); } catch (MessagingException | UnsupportedEncodingException e) { } } }
@SuppressWarnings("unchecked") public void sendButtonActionPerformed(java.awt.event.ActionEvent e) { try { sendButton.setEnabled(false); log.debug("initial checks"); InternetAddress email = new InternetAddress(emailField.getText()); email.validate(); log.debug("start send"); String charSet = "UTF-8"; // NO18N String requestURL = "http://jmri.org/problem-report.php"; // NOI18N MultipartMessage msg = new MultipartMessage(requestURL, charSet); // add reporter email address log.debug("start creating message"); msg.addFormField("reporter", emailField.getText()); // add if to Cc sender msg.addFormField("sendcopy", checkCopy.isSelected() ? "yes" : "no"); // add problem summary msg.addFormField("summary", summaryField.getText()); // build detailed error report (include context if selected) String report = descField.getText() + "\r\n"; if (checkContext.isSelected()) { report += "=========================================================\r\n"; // NOI18N report += (new ReportContext()).getReport(checkNetwork.isSelected() && checkNetwork.isEnabled()); } msg.addFormField("problem", report); log.debug("start adding attachments"); // add panel file if OK if (checkPanel.isSelected()) { log.debug("prepare panel attachment"); // Check that some startup panel files have been loaded for (PerformFileModel m : InstanceManager.getDefault(StartupActionsManager.class) .getActions(PerformFileModel.class)) { String fn = m.getFileName(); File f = new File(fn); log.debug("add startup panel file: {}", f); msg.addFilePart("logfileupload[]", f); } // Check that a manual panel file has been loaded File file = jmri.configurexml.LoadXmlUserAction.getCurrentFile(); if (file != null) { log.debug("add manual panel file: {}", file.getPath()); msg.addFilePart("logfileupload[]", jmri.configurexml.LoadXmlUserAction.getCurrentFile()); } else { // No panel file loaded log.warn("No manual panel file loaded - not sending"); } } // add profile files if OK if (checkProfile.isSelected()) { log.debug("prepare profile attachment"); // Check that a profile has been loaded Profile profile = ProfileManager.getDefault().getActiveProfile(); File file = profile.getPath(); if (file != null) { log.debug("add profile: {}", file.getPath()); // Now zip-up contents of profile // Create temp file that will be deleted when Java quits File temp = File.createTempFile("profile", ".zip"); temp.deleteOnExit(); FileOutputStream out = new FileOutputStream(temp); ZipOutputStream zip = new ZipOutputStream(out); addDirectory(zip, file); zip.close(); out.close(); msg.addFilePart("logfileupload[]", temp); } else { // No profile loaded log.warn("No profile loaded - not sending"); } } // add the log if OK if (checkLog.isSelected()) { log.debug("prepare log attachments"); // search for an appender that stores a file for (java.util.Enumeration<org.apache.log4j.Appender> en = org.apache.log4j.Logger.getRootLogger().getAllAppenders(); en.hasMoreElements(); ) { // does this have a file? org.apache.log4j.Appender a = en.nextElement(); // see if it's one of the ones we know if (log.isDebugEnabled()) { log.debug("check appender {}", a); } try { org.apache.log4j.FileAppender f = (org.apache.log4j.FileAppender) a; log.debug("find file: {}", f.getFile()); msg.addFilePart("logfileupload[]", new File(f.getFile()), "application/octet-stream"); } catch (ClassCastException ex) { } } } log.debug("done adding attachments"); // finalise and get server response (if any) log.debug("posting report..."); List<String> response = msg.finish(); log.debug("send complete"); log.debug("server response:"); boolean checkResponse = false; for (String line : response) { log.debug(" :{}", line); if (line.contains("<p>Message successfully sent!</p>")) { checkResponse = true; } } if (checkResponse) { JOptionPane.showMessageDialog( null, rb.getString("InfoMessage"), rb.getString("InfoTitle"), JOptionPane.INFORMATION_MESSAGE); // close containing Frame getTopLevelAncestor().setVisible(false); } else { JOptionPane.showMessageDialog( null, rb.getString("ErrMessage"), rb.getString("ErrTitle"), JOptionPane.ERROR_MESSAGE); sendButton.setEnabled(true); } } catch (IOException ex) { log.error("Error when attempting to send report: " + ex); sendButton.setEnabled(true); } catch (AddressException ex) { log.error("Invalid email address: " + ex); JOptionPane.showMessageDialog( null, rb.getString("ErrAddress"), rb.getString("ErrTitle"), JOptionPane.ERROR_MESSAGE); sendButton.setEnabled(true); } }