/** Prepares the message to be sent by the email sender */ protected MimeMessage prepareEmailSenderMessage( Session session, String to, String replyTo, String subject, String body) throws Exception { MimeMessage message = new MimeMessage(session); // From InternetAddress fromAddress = new InternetAddress(emailAddress); fromAddress.setPersonal(emailPersonalName); message.setFrom(fromAddress); // Reply to InternetAddress[] replyToAddress = new InternetAddress[1]; replyToAddress[0] = new InternetAddress(replyTo); replyToAddress[0].setPersonal(emailPersonalName); message.setReplyTo(replyToAddress); // To InternetAddress toAddress = new InternetAddress(to); message.addRecipient(Message.RecipientType.TO, toAddress); // Subject/Body message.setSubject(subject); message.setText(body); return message; }
/** * Generate a MimeMessage with the specified attributes * * @param destEmailAddr The destination email address * @param fromEmailAddr The sender email address * @param fromName The senders display name * @param emailSubject The subject * @param emailPlainText The message body (plaintext) * @return The MimeMessage object * @throws Exception */ public static MimeMessage generateMail( String destEmailAddr, String destPersonalName, String fromEmailAddr, String fromName, String emailSubject, String emailPlainText) throws Exception { MimeMessage message = new MimeMessage(Session.getDefaultInstance(System.getProperties(), null)); InternetAddress[] toAddrs = InternetAddress.parse(destEmailAddr, false); if (destPersonalName != null) { toAddrs[0].setPersonal(destPersonalName); } InternetAddress from = new InternetAddress(fromEmailAddr); from.setPersonal(fromName); message.setRecipients(Message.RecipientType.TO, toAddrs); message.setFrom(from); message.setSubject(emailSubject); message.setSentDate(new java.util.Date()); MimeMultipart msgbody = new MimeMultipart(); MimeBodyPart html = new MimeBodyPart(); html.setDataHandler(new DataHandler(new MailDataSource(emailPlainText, "text/plain"))); msgbody.addBodyPart(html); message.setContent(msgbody); return message; }
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); } }
/** * Convenience method to create a message associated with the session ses. If success is true, * String reasonFailed is ignored. If success is false and reasonFailed is null, it will be set to * "Unknown". * * @param ses * @param time * @param attempt * @param result * @return * @throws MessagingException * @throws MessagingException * @throws MessagingException * @throws AddressException */ private SMTPMessage createMessage( Session ses, Date time, boolean success, String result, String reasonFailed) throws MessagingException { SMTPMessage message = new SMTPMessage(ses); String tag; if (success) { tag = "[Success]"; } else { tag = "[Failed]"; } try { InternetAddress from = new InternetAddress(); from.setAddress(ses.getProperty("mail.smtp.from")); from.setPersonal("Hermes2 Admin"); message.setFrom(from); message.setSubject(tag + "Hermes2 housecleaning"); String theContent = "======== Report for Hermes2 Housecleaning ======== " + "\n" + "\n Hermes2 housecleaning has recently been invoked" + "\n Date: " + time + "\n Result: " + result; if (success) { message.setText(theContent); } else { if (reasonFailed == null) { reasonFailed = "Unknown"; } message.setText( theContent + "\n Reason for failure: " + reasonFailed + "\n\n" + " Please refer to the log files for more details."); } } catch (MessagingException e) { AdminError("Unable to create the message. Messaging Exception thrown."); stackTraceToLog(e); throw e; } catch (UnsupportedEncodingException e) { AdminError("Unsupported encoding in email 'from' name."); } return message; }
@Test public void testUTF8() throws Exception { envVars.put("EMAIL_LIST", "[email protected], cc:[email protected], 愛嬋 <*****@*****.**>"); Set<InternetAddress> internetAddresses = emailRecipientUtils.convertRecipientString("$EMAIL_LIST", envVars); assertEquals(2, internetAddresses.size()); assertTrue(internetAddresses.contains(new InternetAddress("*****@*****.**"))); InternetAddress addr = new InternetAddress("*****@*****.**"); addr.setPersonal(MimeUtility.encodeWord("愛嬋", "UTF-8", "B")); assertTrue(internetAddresses.contains(addr)); internetAddresses = emailRecipientUtils.convertRecipientString("$EMAIL_LIST", envVars, EmailRecipientUtils.CC); assertEquals(1, internetAddresses.size()); assertTrue(internetAddresses.contains(new InternetAddress("*****@*****.**"))); }
@Override public void onEnable() { pdfFile = this.getDescription(); CraftMailUtil.init(this); saveDefaultConfig(); Configuration config = getConfig(); boolean keepSettingUp = true; useSMTP = config.getBoolean("mail.enabled"); if (useSMTP) { String from = config.getString("mail.smtp.from"); String fromName = config.getString("mail.smtp.name"); InternetAddress fromAddress = null; try { fromAddress = new InternetAddress(from); } catch (AddressException ex) { log.severe(CraftMailUtil.prependName("Invalid from address specified in config.yml!")); if (errorMessage == null) errorMessage = "Failed to set up SMTP! Please check console for details."; keepSettingUp = false; } keepSettingUp = keepSettingUp && fromAddress != null; if (keepSettingUp) { try { fromAddress.setPersonal(fromName); } catch (UnsupportedEncodingException e) { log.severe(CraftMailUtil.prependName("Invalid from name specified in config.yml!")); if (errorMessage == null) errorMessage = "Invalid from name specified in config.yml!"; } final InternetAddress fromAddressFinal = fromAddress; final String host = config.getString("mail.smtp.host"); final String port = config.getString("mail.smtp.port"); final String username = config.getString("mail.smtp.auth.username"); final String password = config.getString("mail.smtp.auth.password"); final String authType = config.getString("mail.smtp.auth.type"); final CraftMail plugin = this; getServer() .getScheduler() .scheduleAsyncDelayedTask( this, new Runnable() { @Override public void run() { EmailSender.initialise( fromAddressFinal, host, port, username, password, authType); plugin.startSendAsyncTask(); } }); } } if (keepSettingUp) { String hostname = config.getString("sql.hostname"); int port = config.getInt("sql.port"); String database = config.getString("sql.database"); if (database == null) database = String.valueOf(config.getInt("sql.database")); String url = "jdbc:mysql://" + hostname + ":" + port + "/" + database; String username = config.getString("sql.username"); if (username == null) username = String.valueOf(config.getInt("sql.username")); String password = config.getString("sql.password"); if (password == null) password = String.valueOf(config.getInt("sql.password")); if (!DatabaseManager.init(this, url, username, password)) { keepSettingUp = false; log.severe(CraftMailUtil.prependName("Failed to set up database!")); if (errorMessage == null) errorMessage = "Failed to set up database! Please check console for details."; } } getCommand("mail").setExecutor(new CraftMailCommandExecutor(this)); playerListener = new CraftMailPlayerListener(this); getServer().getPluginManager().registerEvents(playerListener, this); log.info(pdfFile.getFullName() + " is enabled!"); }