List of usage examples for org.apache.commons.mail Email setFrom
public Email setFrom(final String email) throws EmailException
From source file:com.neu.controller.MessageController.java
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { DataSource ds = (DataSource) this.getApplicationContext().getBean("myDataSource"); String action = request.getParameter("action"); ModelAndView mv = new ModelAndView(); HttpSession session = request.getSession(); String userName = (String) session.getAttribute("userName"); if (action.equalsIgnoreCase("reply")) { try {/*from w ww.j a v a 2 s. c om*/ String receiver = request.getParameter("to"); System.out.println("Printing receiver in reply case: " + receiver); QueryRunner run = new QueryRunner(ds); ResultSetHandler<UsersBean> user = new BeanHandler<UsersBean>(UsersBean.class); UsersBean ub = run.query("select * from userstable where userName =?", user, receiver); if (ub != null) { System.out.println("printing userEmail received from DB: " + ub.getUserEmail()); mv.addObject("toEmail", ub.getUserEmail()); mv.addObject("to", receiver); } mv.setViewName("reply"); } catch (SQLException e) { System.out.println(e); } } else if (action.equalsIgnoreCase("sent")) { System.out.println("In sent case"); try { String receiver = request.getParameter("to"); String receiverEmail = request.getParameter("toEmail"); System.out.println("printing receiver email: " + receiverEmail); QueryRunner run = new QueryRunner(ds); ResultSetHandler<UsersBean> user = new BeanHandler<UsersBean>(UsersBean.class); UsersBean ub = run.query("select * from userstable where userName =?", user, userName); if (ub != null) { String senderEmail = ub.getUserEmail(); System.out.println("printing senderemail: " + senderEmail); ResultSetHandler<MessageBean> msg = new BeanHandler<MessageBean>(MessageBean.class); Object[] params = new Object[4]; params[0] = userName; params[1] = request.getParameter("message"); Date d = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd"); String messageDate = format.format(d); params[2] = messageDate; params[3] = receiver; int inserts = run.update( "Insert into messages (fromUser,message,messageDate,userName) values(?,?,?,?)", params);//Logic to send the email try { Email email = new SimpleEmail(); email.setHostName("smtp.googlemail.com");//If a server is capable of sending email, then you don't need the authentication. In this case, an email server needs to be running on that machine. Since we are running this application on the localhost and we don't have a email server, we are simply asking gmail to relay this email. email.setSmtpPort(465); email.setAuthenticator( new DefaultAuthenticator("contactapplication2017@gmail.com", "springmvc")); email.setSSLOnConnect(true); email.setFrom(senderEmail);//This email will appear in the from field of the sending email. It doesn't have to be a real email address.This could be used for phishing/spoofing! email.setSubject("Thanks for Signing Up!"); email.setMsg("Welcome to Web tools Lab 5 Spring Application sign up email test!"); email.addTo(receiverEmail);//Will come from the database email.send(); } catch (Exception e) { System.out.println("Email Exception" + e.getMessage()); e.printStackTrace(); } mv.setViewName("messageSent"); } else { mv.addObject("error", "true"); mv.setViewName("index"); } } catch (Exception ex) { System.out.println("Error Message" + ex.getMessage()); ex.printStackTrace(); } } return mv; }
From source file:adams.core.net.SimpleApacheSendEmail.java
/** * Sends an email.// w w w .ja va 2 s .co m * * @param email the email to send * @return true if successfully sent * @throws Exception in case of invalid internet addresses or messaging problem */ @Override public boolean sendMail(Email email) throws Exception { org.apache.commons.mail.Email mail; String id; MultiPartEmail mpemail; EmailAttachment attachment; if (email.getAttachments().length > 0) { mail = new MultiPartEmail(); mpemail = (MultiPartEmail) mail; for (File file : email.getAttachments()) { attachment = new EmailAttachment(); attachment.setPath(file.getAbsolutePath()); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setName(file.getName()); mpemail.attach(attachment); } } else { mail = new SimpleEmail(); } mail.setFrom(email.getFrom().getValue()); for (EmailAddress address : email.getTo()) mail.addTo(address.getValue()); for (EmailAddress address : email.getCC()) mail.addCc(address.getValue()); for (EmailAddress address : email.getBCC()) mail.addBcc(address.getValue()); mail.setSubject(email.getSubject()); mail.setMsg(email.getBody()); mail.setHostName(m_Server); mail.setSmtpPort(m_Port); mail.setStartTLSEnabled(m_UseTLS); mail.setSSLOnConnect(m_UseSSL); if (m_RequiresAuth) mail.setAuthentication(m_User, m_Password.getValue()); mail.setSocketTimeout(m_Timeout); try { id = mail.send(); if (isLoggingEnabled()) getLogger().info("Message sent: " + id); } catch (Exception e) { getLogger().log(Level.SEVERE, "Failed to send email: " + mail, e); return false; } return true; }
From source file:com.jnd.sonar.analysisreport.AnalysisReportHelper.java
public void sendNotificationSMS(String send_sms_to_provider, String send_sms_to, String from, String username, String password, String hostname, String portno, boolean setSSLOnConnectFlag, String subject, String message) {// w ww . jav a 2s .c o m try { send_sms_to_provider = settings.getString(TO_SMS_PROVIDER_PROPERTY); send_sms_to = settings.getString(TO_SMS_PROPERTY); Email smsObject = new SimpleEmail(); DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Calendar cal = Calendar.getInstance(); String dateStr = dateFormat.format(cal.getTime()); smsObject.setHostName(hostname); smsObject.setSmtpPort(Integer.parseInt(portno)); smsObject.setAuthenticator(new DefaultAuthenticator(username, password)); smsObject.setSSL(true); smsObject.setFrom(from); smsObject.setSubject(""); smsObject.setMsg("Sonar analysis completed successfully at " + dateStr + " . Please visit " + settings.getString("sonar.host.url") + " for more details!"); //multiple SMS recipients. String[] addrs = StringUtils.split(to_email, "\t\r\n;, "); for (String addr : addrs) { smsObject.addTo(send_sms_to); } smsObject.send(); } catch (EmailException e) { throw new SonarException("Unable to send sms", e); } catch (Exception ex) { // TODO Auto-generated catch block ex.printStackTrace(); } }
From source file:com.neu.controller.LoginController.java
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { DataSource ds = (DataSource) this.getApplicationContext().getBean("myDataSource"); String action = request.getParameter("action"); ModelAndView mv = new ModelAndView(); HttpSession session = request.getSession(); if (action.equalsIgnoreCase("login")) { try {//www . j a va 2s . c om String userName = request.getParameter("user"); String password = request.getParameter("password"); QueryRunner run = new QueryRunner(ds); ResultSetHandler<UsersBean> user = new BeanHandler<UsersBean>(UsersBean.class); Object[] params = new Object[2]; params[0] = userName; params[1] = password; UsersBean ub = run.query("select * from userstable where userName =? and userPassword=?", user, params); if (ub != null) { ResultSetHandler<List<MessageBean>> messages = new BeanListHandler<MessageBean>( MessageBean.class); List<MessageBean> msg = run.query("select * from messages where userName =?", messages, userName); session.setAttribute("userName", userName); session.setAttribute("messageList", msg); mv.setViewName("userhome"); } else { mv.addObject("error", "true"); mv.setViewName("index"); } } catch (Exception ex) { System.out.println("Error Message" + ex.getMessage()); } } else if (action.equalsIgnoreCase("logout")) { session.invalidate(); mv.setViewName("index"); } else if (action.equalsIgnoreCase("signup")) { System.out.println("sign up"); // // String userName = request.getParameter("user"); // String password = request.getParameter("password"); // String emailObj = request.getParameter("emailObj"); // // System.out.println("printing details: " + userName + " " +password + " "+emailObj); mv.setViewName("signup"); } else if (action.equalsIgnoreCase("signupsubmit")) { System.out.println("sign up submit"); String userName = request.getParameter("user"); String password = request.getParameter("password"); String email = request.getParameter("email"); System.out.println("printing details: " + userName + " " + password + " " + email); if (userName.equals("") || (password.equals("")) || (email.equals(""))) { System.out.println("empty values"); mv.addObject("error", "true"); } else { ResultSetHandler<UsersBean> user = new BeanHandler<UsersBean>(UsersBean.class); Object[] params = new Object[3]; params[0] = userName; params[1] = password; params[2] = email; QueryRunner run = new QueryRunner(ds); int inserts = run.update("insert into userstable (UserName,UserPassword,UserEmail) values (?,?,?)", params);//Logic to insert into table System.out.println("inserts value " + inserts); if (inserts > 0) { mv.addObject("success", "true"); Email emailObj = new SimpleEmail(); emailObj.setHostName("smtp.googlemail.com");//If a server is capable of sending emailObj, then you don't need the authentication. In this case, an emailObj server needs to be running on that machine. Since we are running this application on the localhost and we don't have a emailObj server, we are simply asking gmail to relay this emailObj. emailObj.setSmtpPort(465); emailObj.setAuthenticator( new DefaultAuthenticator("contactapplication2017@gmail.com", "springmvc")); emailObj.setSSLOnConnect(true); emailObj.setFrom("webtools@hello.com");//This emailObj will appear in the from field of the sending emailObj. It doesn't have to be a real emailObj address.This could be used for phishing/spoofing! emailObj.setSubject("TestMail"); emailObj.setMsg("This is spring MVC Contact Application sending you the email"); emailObj.addTo(email);//Will come from the sign up details emailObj.send(); } } mv.setViewName("signup"); } return mv; }
From source file:com.mirth.connect.server.util.SMTPConnection.java
public void send(String toList, String ccList, String from, String subject, String body) throws EmailException { Email email = new SimpleEmail(); email.setHostName(host);/*from w w w . jav a 2 s .com*/ email.setSmtpPort(Integer.parseInt(port)); email.setSocketConnectionTimeout(socketTimeout); email.setDebug(true); if (useAuthentication) { email.setAuthentication(username, password); } if (StringUtils.equalsIgnoreCase(secure, "TLS")) { email.setTLS(true); } else if (StringUtils.equalsIgnoreCase(secure, "SSL")) { email.setSSL(true); } for (String to : StringUtils.split(toList, ",")) { email.addTo(to); } if (StringUtils.isNotEmpty(ccList)) { for (String cc : StringUtils.split(ccList, ",")) { email.addCc(cc); } } email.setFrom(from); email.setSubject(subject); email.setMsg(body); email.send(); }
From source file:com.doculibre.constellio.wicket.panels.admin.indexing.AdminIndexingPanel.java
private void sendEmail(String hostName, int smtpPort, DefaultAuthenticator authenticator, String sender, String subject, String message, String receiver) throws EmailException { Email email = new SimpleEmail(); email.setHostName(hostName);/*ww w . ja v a 2 s . c om*/ email.setSmtpPort(smtpPort); email.setAuthenticator(authenticator); email.setFrom(sender); email.setSubject(subject); email.setMsg(message); email.addTo(receiver); email.send(); }
From source file:io.mapzone.controller.email.EmailService.java
public void send(Email email) throws EmailException { String env = System.getProperty("io.mapzone.controller.SMTP"); if (env == null) { throw new IllegalStateException( "Environment variable missing: io.mapzone.controller.SMTPP. Format: <host>|<login>|<passwd>|<from>"); }/* w ww.jav a 2 s . com*/ String[] parts = StringUtils.split(env, "|"); if (parts.length < 3 || parts.length > 4) { throw new IllegalStateException( "Environment variable wrong: io.mapzone.controller.SMTP. Format: <host>|<login>|<passwd>|<from> : " + env); } email.setDebug(true); email.setHostName(parts[0]); //email.setSmtpPort( 465 ); //email.setSSLOnConnect( true ); email.setAuthenticator(new DefaultAuthenticator(parts[1], parts[2])); if (email.getFromAddress() == null && parts.length == 4) { email.setFrom(parts[3]); } if (email.getSubject() == null) { throw new EmailException("Missing subject."); } email.send(); }
From source file:com.googlecode.fascinator.portal.process.EmailNotifier.java
/** * Send the actual email.//from ww w . jav a2 s.c o m * * @param oid * @param from * @param recipient * @param subject * @param body * @return */ public boolean email(String oid, String from, String recipient, String subject, String body) { try { Email email = new SimpleEmail(); log.debug("Email host: " + host); log.debug("Email port: " + port); log.debug("Email username: " + username); log.debug("Email from: " + from); log.debug("Email to: " + recipient); log.debug("Email Subject is: " + subject); log.debug("Email Body is: " + body); email.setHostName(host); email.setSmtpPort(Integer.parseInt(port)); email.setAuthenticator(new DefaultAuthenticator(username, password)); // the method setSSL is deprecated on the newer versions of commons // email... email.setSSL("true".equalsIgnoreCase(ssl)); email.setTLS("true".equalsIgnoreCase(tls)); email.setFrom(from); email.setSubject(subject); email.setMsg(body); if (recipient.indexOf(",") >= 0) { String[] recs = recipient.split(","); for (String rec : recs) { email.addTo(rec); } } else { email.addTo(recipient); } email.send(); } catch (Exception ex) { log.debug("Error sending notification mail for oid:" + oid, ex); return false; } return true; }
From source file:com.cws.esolutions.security.quartz.PasswordExpirationNotifier.java
/** * @see org.quartz.Job#execute(org.quartz.JobExecutionContext) *///from w w w . j a va 2 s. c o m public void execute(final JobExecutionContext context) { final String methodName = PasswordExpirationNotifier.CNAME + "#execute(final JobExecutionContext jobContext)"; if (DEBUG) { DEBUGGER.debug(methodName); DEBUGGER.debug("JobExecutionContext: {}", context); } final Map<String, Object> jobData = context.getJobDetail().getJobDataMap(); if (DEBUG) { DEBUGGER.debug("jobData: {}", jobData); } try { UserManager manager = UserManagerFactory .getUserManager(bean.getConfigData().getSecurityConfig().getUserManager()); if (DEBUG) { DEBUGGER.debug("UserManager: {}", manager); } List<String[]> accounts = manager.listUserAccounts(); if (DEBUG) { DEBUGGER.debug("accounts: {}", accounts); } if ((accounts == null) || (accounts.size() == 0)) { return; } Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, 30); Long expiryTime = cal.getTimeInMillis(); if (DEBUG) { DEBUGGER.debug("Calendar: {}", cal); DEBUGGER.debug("expiryTime: {}", expiryTime); } for (String[] account : accounts) { if (DEBUG) { DEBUGGER.debug("Account: {}", (Object) account); } List<Object> accountDetail = manager.loadUserAccount(account[0]); if (DEBUG) { DEBUGGER.debug("List<Object>: {}", accountDetail); } try { Email email = new SimpleEmail(); email.setHostName((String) jobData.get("mailHost")); email.setSmtpPort(Integer.parseInt((String) jobData.get("portNumber"))); if ((Boolean) jobData.get("isSecure")) { email.setSSLOnConnect(true); } if ((Boolean) jobData.get("isAuthenticated")) { email.setAuthenticator(new DefaultAuthenticator((String) jobData.get("username"), PasswordUtils.decryptText((String) (String) jobData.get("password"), (String) jobData.get("salt"), secConfig.getSecretAlgorithm(), secConfig.getIterations(), secConfig.getKeyBits(), secConfig.getEncryptionAlgorithm(), secConfig.getEncryptionInstance(), systemConfig.getEncoding()))); } email.setFrom((String) jobData.get("emailAddr")); email.addTo((String) accountDetail.get(6)); email.setSubject((String) jobData.get("messageSubject")); email.setMsg(String.format((String) jobData.get("messageBody"), (String) accountDetail.get(4))); if (DEBUG) { DEBUGGER.debug("SimpleEmail: {}", email); } email.send(); } catch (EmailException ex) { ERROR_RECORDER.error(ex.getMessage(), ex); } catch (SecurityException sx) { ERROR_RECORDER.error(sx.getMessage(), sx); } } } catch (UserManagementException umx) { ERROR_RECORDER.error(umx.getMessage(), umx); } }
From source file:com.mirth.connect.server.util.ServerSMTPConnection.java
public void send(String toList, String ccList, String from, String subject, String body, String charset) throws EmailException { Email email = new SimpleEmail(); // Set the charset if it was specified. Otherwise use the system's default. if (StringUtils.isNotBlank(charset)) { email.setCharset(charset);//w w w. ja va 2 s . com } email.setHostName(host); email.setSmtpPort(Integer.parseInt(port)); email.setSocketConnectionTimeout(socketTimeout); email.setDebug(true); if (useAuthentication) { email.setAuthentication(username, password); } if (StringUtils.equalsIgnoreCase(secure, "TLS")) { email.setStartTLSEnabled(true); } else if (StringUtils.equalsIgnoreCase(secure, "SSL")) { email.setSSLOnConnect(true); email.setSslSmtpPort(port); } // These have to be set after the authenticator, so that a new mail session isn't created ConfigurationController configurationController = ControllerFactory.getFactory() .createConfigurationController(); email.getMailSession().getProperties().setProperty("mail.smtp.ssl.protocols", StringUtils.join( MirthSSLUtil.getEnabledHttpsProtocols(configurationController.getHttpsClientProtocols()), ' ')); email.getMailSession().getProperties().setProperty("mail.smtp.ssl.ciphersuites", StringUtils.join( MirthSSLUtil.getEnabledHttpsCipherSuites(configurationController.getHttpsCipherSuites()), ' ')); for (String to : StringUtils.split(toList, ",")) { email.addTo(to); } if (StringUtils.isNotEmpty(ccList)) { for (String cc : StringUtils.split(ccList, ",")) { email.addCc(cc); } } email.setFrom(from); email.setSubject(subject); email.setMsg(body); email.send(); }