List of usage examples for javax.mail Authenticator Authenticator
Authenticator
From source file:net.sourceforge.msscodefactory.cfasterisk.v2_2.CFAstSMWar.CFAstSMWarRequestResetPasswordHtml.java
protected void sendPasswordResetEMail(HttpServletRequest request, ICFAstSecUserObj resetUser, ICFAstClusterObj cluster) throws AddressException, MessagingException, NamingException { final String S_ProcName = "sendPasswordResetEMail"; Properties props = System.getProperties(); String clusterDescription = cluster.getRequiredDescription(); Context ctx = new InitialContext(); String smtpEmailFrom = (String) ctx.lookup("java:comp/env/CFAst22SmtpEmailFrom"); if ((smtpEmailFrom == null) || (smtpEmailFrom.length() <= 0)) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "JNDI lookup for CFAst22SmtpEmailFrom"); }//from w w w. ja v a 2 s . co m smtpUsername = (String) ctx.lookup("java:comp/env/CFAst22SmtpUsername"); if ((smtpUsername == null) || (smtpUsername.length() <= 0)) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "JNDI lookup for CFAst22SmtpUsername"); } smtpPassword = (String) ctx.lookup("java:comp/env/CFAst22SmtpPassword"); if ((smtpPassword == null) || (smtpPassword.length() <= 0)) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "JNDI lookup for CFAst22SmtpPassword"); } Session emailSess = Session.getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(smtpUsername, smtpPassword); } }); String thisURI = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getRequestURI().toString(); int lastSlash = thisURI.lastIndexOf('/'); String baseURI = thisURI.substring(0, lastSlash); UUID resetUUID = resetUser.getOptionalPasswordResetUuid(); String msgBody = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n" + "<HTML>\n" + "<BODY>\n" + "<p>\n" + "You requested a password reset for " + resetUser.getRequiredEMailAddress() + " used for accessing " + clusterDescription + ".\n" + "<p>" + "Please click on the following link to reset your password:<br>\n" + "<A HRef=\"" + baseURI + "/CFAstSMWarResetPasswordHtml?ResetUUID=" + resetUUID.toString() + "\">" + baseURI + "/CFAstSMWarResetPasswordHtml?ResetUUID=" + resetUUID.toString() + "</A>\n" + "<p>" + "Or click on the following link to cancel the reset request:<br>\n" + "<A HRef=\"" + baseURI + "/CFAstSMWarCancelResetPasswordHtml?ResetUUID=" + resetUUID.toString() + "\">" + baseURI + "/CFAstSMWarCancelResetPasswordHtml?ResetUUID=" + resetUUID.toString() + "</A>\n" + "</BODY>\n" + "</HTML>\n"; MimeMessage msg = new MimeMessage(emailSess); msg.setFrom(new InternetAddress(smtpEmailFrom)); InternetAddress mailTo[] = InternetAddress.parse(resetUser.getRequiredEMailAddress(), false); msg.setRecipient(Message.RecipientType.TO, mailTo[0]); msg.setSubject("You requested a password reset for your account with " + clusterDescription + "?"); msg.setContent(msgBody, "text/html"); msg.setSentDate(new Date()); msg.saveChanges(); Transport.send(msg); }
From source file:fr.paris.lutece.portal.service.mail.MailUtil.java
/** * Open mail session with the SMTP server using the given credentials. Will * use no authentication if strUsername is null or blank. * * @param strHost The SMTP name or IP address. * @param nPort The port to use//from www . j ava2s. c o m * @param strUsername the username * @param strPassword the password * @return the mails session object */ protected static Session getMailSession(String strHost, int nPort, final String strUsername, final String strPassword) { String strDebug = AppPropertiesService.getProperty(PROPERTY_MAIL_SESSION_DEBUG, Boolean.FALSE.toString()); boolean bSessionDebug = Boolean.parseBoolean(strDebug); // Initializes a mail session with the SMTP server Properties props = System.getProperties(); props.put(MAIL_HOST, strHost); props.put(MAIL_TRANSPORT_PROTOCOL, SMTP); props.put(MAIL_PROPTOCOL_HOST, strHost); props.put(MAIL_PROPTOCOL_PORT, nPort); Authenticator auth; if (StringUtils.isNotBlank(strUsername)) { props.put(MAIL_SMTP_AUTH, TRUE); // using authenticator class that return a PasswordAuthentication auth = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(strUsername, strPassword); } }; } else { // no authentication data provided, no authenticator auth = null; } Session mailSession = Session.getDefaultInstance(props, auth); // Activate debugging mailSession.setDebug(bSessionDebug); return mailSession; }
From source file:com.sonicle.webtop.core.app.WebTopSession.java
public javax.mail.Session getMailSession() { synchronized (lock1) { UserProfileId pid = getProfileId(); if (pid != null && mailSession == null) { CoreServiceSettings css = new CoreServiceSettings(CoreManifest.ID, pid.getDomainId()); String smtphost = css.getSMTPHost(); int smtpport = css.getSMTPPort(); boolean starttls = css.isSMTPStartTLS(); boolean auth = css.isSMTPAuthentication(); Properties props = new Properties(System.getProperties()); //props.setProperty("mail.socket.debug", "true"); //props.setProperty("mail.imap.parse.debug", "true"); props.setProperty("mail.smtp.host", smtphost); props.setProperty("mail.smtp.port", "" + smtpport); if (starttls) { props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.ssl.trust", "*"); props.put("mail.smtp.ssl.checkserveridentity", "false"); }/*from ww w .j a va2 s.co m*/ props.setProperty("mail.imaps.ssl.trust", "*"); props.setProperty("mail.imap.folder.class", "com.sonicle.mail.imap.SonicleIMAPFolder"); props.setProperty("mail.imaps.folder.class", "com.sonicle.mail.imap.SonicleIMAPFolder"); props.setProperty("mail.imap.enableimapevents", "true"); // Support idle events Authenticator authenticator = null; if (auth) { props.setProperty("mail.smtp.auth", "true"); authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { Principal principal = (Principal) SecurityUtils.getSubject().getPrincipal(); String login = principal.getFullInternetName(); String password = new String(principal.getPassword()); //logger.info("getPasswordAuthentication: "+login+" / *****"); return new PasswordAuthentication(login, password); } }; } mailSession = javax.mail.Session.getInstance(props, authenticator); } } return mailSession; }
From source file:com.tremolosecurity.proxy.auth.PasswordReset.java
private void sendEmail(SmtpMessage msg) throws MessagingException { Properties props = new Properties(); props.setProperty("mail.smtp.host", this.reset.getSmtpServer()); props.setProperty("mail.smtp.port", Integer.toString(reset.getSmtpPort())); props.setProperty("mail.smtp.user", reset.getSmtpUser()); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.smtp.starttls.enable", Boolean.toString(reset.isSmtpTLS())); //props.setProperty("mail.debug", "true"); //props.setProperty("mail.socket.debug", "true"); if (reset.getSmtpLocalhost() != null && !reset.getSmtpLocalhost().isEmpty()) { props.setProperty("mail.smtp.localhost", reset.getSmtpLocalhost()); }/*from w w w. j a v a2 s . c om*/ if (reset.isUseSocks()) { props.setProperty("mail.smtp.socks.host", reset.getSocksHost()); props.setProperty("mail.smtp.socks.port", Integer.toString(reset.getSocksPort())); props.setProperty("mail.smtps.socks.host", reset.getSocksHost()); props.setProperty("mail.smtps.socks.port", Integer.toString(reset.getSocksPort())); } //Session session = Session.getInstance(props, new SmtpAuthenticator(this.smtpUser,this.smtpPassword)); Session session = Session.getDefaultInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(reset.getSmtpUser(), reset.getSmtpPassword()); } }); //Session session = Session.getInstance(props, null); session.setDebugOut(System.out); //session.setDebug(true); //Transport tr = session.getTransport("smtp"); //tr.connect(); //tr.connect(this.smtpHost,this.smtpPort, this.smtpUser, this.smtpPassword); Message msgToSend = new MimeMessage(session); msgToSend.setFrom(new InternetAddress(msg.from)); msgToSend.addRecipient(Message.RecipientType.TO, new InternetAddress(msg.to)); msgToSend.setSubject(msg.subject); msgToSend.setText(msg.msg); msgToSend.saveChanges(); Transport.send(msgToSend); //tr.sendMessage(msg, msg.getAllRecipients()); //tr.close(); }
From source file:com.tremolosecurity.provisioning.core.ProvisioningEngineImpl.java
private void sendEmail(SmtpMessage msg) throws MessagingException { Properties props = new Properties(); boolean doAuth = false; props.setProperty("mail.smtp.host", prov.getSmtpHost()); props.setProperty("mail.smtp.port", Integer.toString(prov.getSmtpPort())); if (prov.getSmtpUser() != null && !prov.getSmtpUser().isEmpty()) { logger.debug("SMTP user found '" + prov.getSmtpUser() + "', enabling authentication"); props.setProperty("mail.smtp.user", prov.getSmtpUser()); props.setProperty("mail.smtp.auth", "true"); doAuth = true;/*from www.jav a 2s . c om*/ } else { logger.debug("No SMTP user, disabling authentication"); doAuth = false; props.setProperty("mail.smtp.auth", "false"); } props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.smtp.starttls.enable", Boolean.toString(prov.isSmtpTLS())); if (logger.isDebugEnabled()) { props.setProperty("mail.debug", "true"); props.setProperty("mail.socket.debug", "true"); } if (prov.getLocalhost() != null && !prov.getLocalhost().isEmpty()) { props.setProperty("mail.smtp.localhost", prov.getLocalhost()); } if (prov.isUseSOCKSProxy()) { props.setProperty("mail.smtp.socks.host", prov.getSocksProxyHost()); props.setProperty("mail.smtp.socks.port", Integer.toString(prov.getSocksProxyPort())); props.setProperty("mail.smtps.socks.host", prov.getSocksProxyHost()); props.setProperty("mail.smtps.socks.port", Integer.toString(prov.getSocksProxyPort())); } //Session session = Session.getInstance(props, new SmtpAuthenticator(this.smtpUser,this.smtpPassword)); Session session = null; if (doAuth) { logger.debug("Creating authenticated session"); session = Session.getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(prov.getSmtpUser(), prov.getSmtpPassword()); } }); } else { logger.debug("Creating unauthenticated session"); session = Session.getInstance(props); } if (logger.isDebugEnabled()) { session.setDebugOut(System.out); session.setDebug(true); } //Transport tr = session.getTransport("smtp"); //tr.connect(); //tr.connect(this.smtpHost,this.smtpPort, this.smtpUser, this.smtpPassword); Message msgToSend = new MimeMessage(session); msgToSend.setFrom(new InternetAddress(msg.from)); msgToSend.addRecipient(Message.RecipientType.TO, new InternetAddress(msg.to)); msgToSend.setSubject(msg.subject); if (msg.contentType != null) { msgToSend.setContent(msg.msg, msg.contentType); } else { msgToSend.setText(msg.msg); } msgToSend.saveChanges(); Transport.send(msgToSend); //tr.sendMessage(msg, msg.getAllRecipients()); //tr.close(); }