public void send() { // Your SMTP server address here. String smtpHost = "myserver.jeffcorp.com"; // The sender's email address String from = "*****@*****.**"; // The recepients email address String to = "*****@*****.**"; Properties props = new Properties(); // The protocol to use is SMTP props.put("mail.smtp.host", smtpHost); Session session = Session.getDefaultInstance(props, null); try { InternetAddress[] address = {new InternetAddress(to)}; MimeMessage message; message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipients(Message.RecipientType.TO, to); message.setSubject("Hello from Jeff"); message.setSentDate(sendDate); message.setText("Hello Jeff, \nHow are things going?"); Transport.send(message); System.out.println("email has been sent."); } catch (Exception e) { System.out.println(e); } }
public void mail(String toAddress, String subject, String body) { Properties props = new Properties(); props.put("mail.smtp.host", host); props.put("mail.smtp.port", port); props.put("mail.smtp.ssl.enable", true); Authenticator authenticator = null; if (login) { props.put("mail.smtp.auth", true); authenticator = new Authenticator() { private PasswordAuthentication pa = new PasswordAuthentication(username, password); @Override public PasswordAuthentication getPasswordAuthentication() { return pa; } }; } Session s = Session.getInstance(props, authenticator); s.setDebug(debug); MimeMessage email = new MimeMessage(s); try { email.setFrom(new InternetAddress(from)); email.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress)); email.setSubject(subject); email.setSentDate(new Date()); email.setText(body); Transport.send(email); } catch (MessagingException ex) { ex.printStackTrace(); } }
public void sendEMailToUser(ICFSecuritySecUserObj toUser, String msgSubject, String msgBody) throws IOException, MessagingException, NamingException { final String S_ProcName = "sendEMailToUser"; Properties props = System.getProperties(); Context ctx = new InitialContext(); String smtpEmailFrom = (String) ctx.lookup("java:comp/env/CFInternet25SmtpEmailFrom"); if ((smtpEmailFrom == null) || (smtpEmailFrom.length() <= 0)) { throw CFLib.getDefaultExceptionFactory() .newNullArgumentException( getClass(), S_ProcName, 0, "JNDI lookup for CFInternet25SmtpEmailFrom"); } smtpUsername = (String) ctx.lookup("java:comp/env/CFInternet25SmtpUsername"); if ((smtpUsername == null) || (smtpUsername.length() <= 0)) { throw CFLib.getDefaultExceptionFactory() .newNullArgumentException( getClass(), S_ProcName, 0, "JNDI lookup for CFInternet25SmtpUsername"); } smtpPassword = (String) ctx.lookup("java:comp/env/CFInternet25SmtpPassword"); if ((smtpPassword == null) || (smtpPassword.length() <= 0)) { throw CFLib.getDefaultExceptionFactory() .newNullArgumentException( getClass(), S_ProcName, 0, "JNDI lookup for CFInternet25SmtpPassword"); } Session emailSess = Session.getInstance( props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(smtpUsername, smtpPassword); } }); MimeMessage msg = new MimeMessage(emailSess); msg.setFrom(new InternetAddress(smtpEmailFrom)); InternetAddress mailTo[] = InternetAddress.parse(toUser.getRequiredEMailAddress(), false); msg.setRecipient(Message.RecipientType.TO, mailTo[0]); msg.setSubject((msgSubject != null) ? msgSubject : "No subject"); msg.setSentDate(new Date()); msg.setContent(msgBody, "text/html"); msg.saveChanges(); Transport.send(msg); }