@SuppressWarnings("unchecked") @Override public void run() { // 解析邮件模板配置文件,跟据mailId取得邮件模板 Map<String, Object> map = new HashMap<String, Object>(); // 取得模板路径 String templatePath = StringUtils.trimToEmpty((String) map.get("location")); // 取得邮件默认主题 String subject = StringUtils.trimToEmpty((String) map.get("subject")); // 取得邮件的发送地址 String senderAddress = StringUtils.trimToEmpty((String) map.get("senderAddress")); // 取得回复地址 String replyTo = StringUtils.trimToEmpty((String) map.get("replyTo")); // 取得邮件的发送人的名字 String senderDispaly = StringUtils.trimToEmpty((String) map.get("senderDispaly")); mail.setSubject(StringUtils.isEmpty(mail.getSubject()) ? subject : mail.getSubject()); mail.setSenderAddress( StringUtils.isEmpty(mail.getSenderAddress()) ? senderAddress : mail.getSenderAddress()); mail.setSenderDispaly( StringUtils.isEmpty(mail.getSenderDispaly()) ? senderDispaly : mail.getSenderDispaly()); mail.setReplyTo(StringUtils.isEmpty(mail.getReplyTo()) ? replyTo : mail.getReplyTo()); // 取得内嵌资源 Map<String, String> mapInlineResource = new HashMap<String, String>(); // 封装内嵌资源 if (mapInlineResource != null && !mapInlineResource.isEmpty()) { for (Iterator<String> it = mapInlineResource.keySet().iterator(); it.hasNext(); ) { // 得到内嵌资源的key String key = it.next(); // 得到key对应的内嵌资源的路径 String keyValue = mapInlineResource.get(key); mail.addAttachs(getMailAttachByFilePath(key, keyValue)); } } // 取得附件资源 Map<String, String> mapAttach = (Map<String, String>) map.get("attach"); // 封装附件资源 if (mapAttach != null && !mapAttach.isEmpty()) { for (Iterator<String> it = mapAttach.keySet().iterator(); it.hasNext(); ) { // 得到内嵌资源的key String fileName = it.next(); // 得到key对应的内嵌资源的路径 String filePath = mapAttach.get(fileName); mail.addResource(getMailAttachByFilePath(fileName, filePath)); } } String mailContent = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templatePath, "UTF-8", model); mail.setContent(mailContent); // 调用邮件处理接口 EmailMimeMessageUtils.sendMail(mailSender, mail); }
public static void main(String[] args) throws Exception { // Create and configure HTTPS client Client client = new Client(new Context(), Protocol.HTTPS); Series<Parameter> parameters = client.getContext().getParameters(); parameters.add("truststorePath", "certs/client-truststore.jks"); parameters.add("truststorePassword", "password"); parameters.add("truststoreType", "JKS"); // Create and configure client resource ClientResource clientResource = new ClientResource("https://localhost:8183/accounts/chunkylover53/mails/123"); clientResource.setNext(client); // Preemptively configure the authentication credentials ChallengeResponse authentication = new ChallengeResponse(ChallengeScheme.HTTP_BASIC, "chunkylover53", "pwd"); clientResource.setChallengeResponse(authentication); // Communicate with remote resource MailResource mailClient = clientResource.wrap(MailResource.class); Mail m = mailClient.retrieve(); System.out.println("Subject: " + m.getSubject()); System.out.println("Content: " + m.getContent()); // Store HTTPS client client.stop(); }
/** Test of getSubject method, of class Mail. */ @Test public void testGetSubject() { System.out.println("getSubject"); Mail instance = new Mail(); String expResult = ""; String result = instance.getSubject(); assertEquals(expResult, result); // TODO review the generated test code and remove the default call to fail. fail("The test case is a prototype."); }
public synchronized void sendMail(Mail m) throws MessagingException { MimeMessage message = new MimeMessage(session); DataHandler handler = new DataHandler(new ByteArrayDataSource(m.getBody().getBytes(), "text/plain")); message.setSender(new InternetAddress(m.getSender())); message.setSubject(m.getSubject()); message.setDataHandler(handler); message.setRecipient(Message.RecipientType.TO, new InternetAddress(m.getRecipient())); Transport tr = session.getTransport("smtp"); tr.connect(user, password); tr.send(message); tr.close(); }
@Test public void testCreateMessage() throws Exception { Properties props = System.getProperties(); Session session = Session.getDefaultInstance(props); Mail mail = new Mail(); mail.setContent("content"); mail.setSubject("subject"); mail.setReceipts(Arrays.asList("*****@*****.**")); List<Message> messages = mail.createMessages(session); assertEquals(1, messages.size()); assertEquals(mail.getSubject(), messages.get(0).getSubject()); }