Java Code Examples for org.springframework.mail.javamail.MimeMessageHelper#setSubject()
The following examples show how to use
org.springframework.mail.javamail.MimeMessageHelper#setSubject() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MailService.java From tutorials with MIT License | 7 votes |
@Async public void sendEmail(String to, String subject, String content, boolean isMultipart, boolean isHtml) { log.debug("Send email[multipart '{}' and html '{}'] to '{}' with subject '{}' and content={}", isMultipart, isHtml, to, subject, content); // Prepare message using a Spring helper MimeMessage mimeMessage = javaMailSender.createMimeMessage(); try { MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, StandardCharsets.UTF_8.name()); message.setTo(to); message.setFrom(jHipsterProperties.getMail().getFrom()); message.setSubject(subject); message.setText(content, isHtml); javaMailSender.send(mimeMessage); log.debug("Sent email to User '{}'", to); } catch (Exception e) { if (log.isDebugEnabled()) { log.warn("Email could not be sent to user '{}'", to, e); } else { log.warn("Email could not be sent to user '{}': {}", to, e.getMessage()); } } }
Example 2
Source File: WebEmail.java From Autumn with GNU General Public License v2.0 | 6 votes |
public int sendHtmlEmail(String subject, String content, String to) { try { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8"); mimeMessage.setContent(content, "text/html;charset=utf-8"); messageHelper.setFrom(mailMessage.getFrom()); messageHelper.setSubject(subject); //主题 messageHelper.setTo(to); //发送给 messageHelper.setCc(mailMessage.getFrom()); //抄送 mailSender.send(mimeMessage); //发送邮件 } catch (Exception e) { logger.error("the email send error ! content: {}, exception: {}", content, e); return -1; }finally { } return 1; }
Example 3
Source File: MailController.java From spring-boot-study with MIT License | 6 votes |
/** * 基于 freemarker 模板发送 * */ @GetMapping("/sendTemplateMail") public String sendTemplateMail(){ MimeMessage message = null; try{ message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message,true); helper.setFrom(sendUser); helper.setTo("[email protected]"); helper.setSubject("主题邮件"); Map<String,Object> model =new HashMap<>(); model.put("welcome","欢迎您,hello world template email "); //使用 freeMarkerConfigurer 获取模板 index.ftl Template template = freeMarkerConfigurer.getConfiguration().getTemplate("index.ftl"); String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model); helper.setText(html, true); }catch (Exception e){ e.printStackTrace(); return "fail"; } mailSender.send(message); return "success"; }
Example 4
Source File: MailSender.java From cola-cloud with MIT License | 6 votes |
/** * 发送带附件的邮件 */ public void sendAttachmentMail(MailSenderParams params) { MimeMessage message = null; try { message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(new InternetAddress(this.getFrom(), MimeUtility.encodeText(this.name,"UTF-8", "B"))); helper.setTo(params.getMailTo()); helper.setSubject(params.getTitle()); helper.setText(params.getContent(), true); this.addAttachment(helper,params); } catch (Exception e) { throw new RuntimeException("发送邮件异常! from: " + name + "! to: " + params.getMailTo()); } javaMailSender.send(message); }
Example 5
Source File: MailServiceImpl.java From mySpringBoot with Apache License 2.0 | 6 votes |
/** * 发送静态资源 一张照片 * @param mail * @throws Exception */ @Override public void sendInlineMail(Mail mail){ try{ MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom(from); helper.setTo(mail.getTo()); helper.setSubject(mail.getSubject()); helper.setText("<html><body><img src=\"cid:chuchen\" ></body></html>", true); FileSystemResource file = new FileSystemResource(new File("C:\\Users\\Administrator\\Desktop\\设计图\\已完成\\微信图片_20180323135358.png")); // addInline函数中资源名称chuchen需要与正文中cid:chuchen对应起来 helper.addInline("chuchen", file); mailSender.send(mimeMessage); }catch (Exception e){ logger.error("发送邮件发生异常"); } }
Example 6
Source File: MailServiceImpl.java From spring-boot-demo with MIT License | 6 votes |
/** * 发送正文中有静态资源的邮件 * * @param to 收件人地址 * @param subject 邮件主题 * @param content 邮件内容 * @param rscPath 静态资源地址 * @param rscId 静态资源id * @param cc 抄送地址 * @throws MessagingException 邮件发送异常 */ @Override public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); if (ArrayUtil.isNotEmpty(cc)) { helper.setCc(cc); } FileSystemResource res = new FileSystemResource(new File(rscPath)); helper.addInline(rscId, res); mailSender.send(message); }
Example 7
Source File: MailServiceImpl.java From spring-boot-demo with MIT License | 6 votes |
/** * 发送带附件的邮件 * * @param to 收件人地址 * @param subject 邮件主题 * @param content 邮件内容 * @param filePath 附件地址 * @param cc 抄送地址 * @throws MessagingException 邮件发送异常 */ @Override public void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); if (ArrayUtil.isNotEmpty(cc)) { helper.setCc(cc); } FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = filePath.substring(filePath.lastIndexOf(File.separator)); helper.addAttachment(fileName, file); mailSender.send(message); }
Example 8
Source File: MailService.java From springbootexamples with Apache License 2.0 | 6 votes |
/** * 发送html格式的邮件 * @param toMail * @param subject * @param content */ public void sendHtmlMail(String toMail,String subject,String content) { MimeMessage createMimeMessage = sender.createMimeMessage(); try { //true表示需要创建一个multipart message 如何不设置就不会解析html MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(createMimeMessage,true); mimeMessageHelper.setFrom(formMail); mimeMessageHelper.setTo(toMail); mimeMessageHelper.setSubject(subject); mimeMessageHelper.setText(content, true); sender.send(createMimeMessage); logger.info("发送给"+toMail+"html邮件已经发送。 subject:"+subject); } catch (MessagingException e) { logger.info("发送给"+toMail+"html send mail error subject:"+subject); e.printStackTrace(); } }
Example 9
Source File: MailService.java From expper with GNU General Public License v3.0 | 6 votes |
@Async public void sendEmail(String to, String subject, String content, boolean isMultipart, boolean isHtml) { log.debug("Send e-mail[multipart '{}' and html '{}'] to '{}' with subject '{}' and content={}", isMultipart, isHtml, to, subject, content); // Prepare message using a Spring helper MimeMessage mimeMessage = javaMailSender.createMimeMessage(); try { MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, CharEncoding.UTF_8); message.setTo(to); message.setFrom(jHipsterProperties.getMailgun().getFrom()); message.setSubject(subject); message.setText(content, isHtml); javaMailSender.send(mimeMessage); log.debug("Sent e-mail to User '{}'", to); } catch (Exception e) { log.warn("E-mail could not be sent to user '{}', exception is: {}", to, e.getMessage()); } }
Example 10
Source File: MailTest.java From SpringBootLearn with Apache License 2.0 | 6 votes |
/** * 发送模板信息 * @throws Exception */ @Test public void sendTemplateMail() throws Exception { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom(username); helper.setTo("[email protected]"); helper.setSubject("测试主题:模板邮件"); /** * 模板内需要的参数,保持一致 */ Map<String, Object> params = new HashMap<>(); params.put("userName", "lihaodong"); /** * 配置FreeMarker模板路径 */ Configuration configuration = new Configuration(Configuration.VERSION_2_3_28); configuration.setClassForTemplateLoading(this.getClass(), "/templates"); String html = FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate("mail.ftl"), params); helper.setText(html, true); javaMailSender.send(mimeMessage); }
Example 11
Source File: MailServiceImpl.java From Java-API-Test-Examples with Apache License 2.0 | 6 votes |
/** * 发送带附件的邮件 * @param to * @param subject * @param content * @param filePath */ public void sendAttachmentsMail(String to, String subject, String content, String filePath){ MimeMessage message = mailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); // FileSystemResource file = new FileSystemResource(new File(filePath)); // String fileName = filePath.substring(filePath.lastIndexOf(File.separator)); File file = new File(filePath); //获取文件名 String fileName = file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf("/")+1); helper.addAttachment(fileName, file); //helper.addAttachment("test"+fileName, file); mailSender.send(message); log.info("带附件的邮件已经发送。"); } catch (MessagingException e) { log.error("发送带附件的邮件时发生异常!", e); } }
Example 12
Source File: MailService.java From pacbot with Apache License 2.0 | 5 votes |
private MimeMessagePreparator prepareTemplateBuildMimeMessagePreparator(String from, List<String> to, String subject, String mailMessageUrlOrBody, Map<String, Object> templateModelValues, final String attachmentUrl, final Boolean isPlainMessage) { MimeMessagePreparator messagePreparator = mimeMessage -> { MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true); messageHelper.setFrom(from); String[] toMailList = to.toArray(new String[to.size()]); messageHelper.setTo(toMailList); messageHelper.setSubject(subject); if(StringUtils.isNotEmpty(attachmentUrl) && isHttpUrl(attachmentUrl)) { URL url = new URL(attachmentUrl); String filename = url.getFile(); byte fileContent [] = getFileContent(url); messageHelper.addAttachment(filename, new ByteArrayResource(fileContent)); } String content = StringUtils.EMPTY; if(isPlainMessage) { content = mailContentBuilderService.buildPlainTextMail(mailMessageUrlOrBody, templateModelValues); } else { if(!isHttpUrl(mailMessageUrlOrBody)) { File template = new ClassPathResource("templates/".concat(mailMessageUrlOrBody).concat(".html")).getFile(); content = mailContentBuilderService.buildPlainTextMail(FileUtils.readFileToString(template, "UTF-8"), templateModelValues); } else { content = processTemplate(mailMessageUrlOrBody, templateModelValues); } } messageHelper.setText(content, true); }; return messagePreparator; }
Example 13
Source File: EmailService.java From Gatekeeper with Apache License 2.0 | 5 votes |
private MimeMessage generateMimeMessage(String to, String from, String cc, String subject) throws MessagingException { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, charset); InternetAddress fAddress = new InternetAddress(from); message.setTo(InternetAddress.parse(to, true)); if (cc != null) { message.setCc(InternetAddress.parse(cc, true)); } message.setFrom(fAddress); message.setSubject(subject); return mimeMessage; }
Example 14
Source File: GmailService.java From Spring5Tutorial with GNU Lesser General Public License v3.0 | 5 votes |
private void sendMessage(String to, String subject, String text) { MimeMessage message = mailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, false); message.setContent(text, "text/html; charset=UTF-8"); helper.setTo(to); helper.setSubject(subject); helper.setSentDate(new Date()); mailSender.send(message); } catch (MessagingException e) { throw new RuntimeException(e); } }
Example 15
Source File: JavaMailNotifier.java From cf-butler with Apache License 2.0 | 5 votes |
public void sendMail(String from, String to, String subject, String body, List<EmailAttachment> attachments) { try { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setSubject(subject); helper.setTo(to); helper.setText(body, true); attachments.forEach(ea -> addAttachment(helper, ea)); javaMailSender.send(message); log.info("Email sent to {} with subject: {}!", to, subject); } catch (MessagingException me) { log.warn("Could not send email!", me); } }
Example 16
Source File: EmailMimeMessagePreparator.java From cloudbreak with Apache License 2.0 | 5 votes |
public MimeMessagePreparator prepareMessage(String to, String subject, String body) { return mimeMessage -> { MimeMessageHelper message = new MimeMessageHelper(mimeMessage); message.setFrom(msgFrom); message.setTo(to); message.setSubject(subject); message.setText(body, true); }; }
Example 17
Source File: AsyncSendEmailService.java From Spring-Boot-Book with Apache License 2.0 | 5 votes |
@Async //这是一个异步方法 public void sendVerifyemail(String email) { try { Thread.sleep(3000); String secretKey = UUID.randomUUID().toString(); // 密钥 Timestamp outDate = new Timestamp(System.currentTimeMillis() + 30 * 60 * 1000);// 30分钟后过期 long date = outDate.getTime() / 1000 * 1000; userRepository.setOutDateAndValidataCode(outDate + "", secretKey, email); String key = email + "$" + date + "$" + secretKey; String digitalSignature = MD5Util.encode(key);// 数字签名 // String basePath = this.getRequest().getScheme() + "://" + this.getRequest().getServerName() + ":" + this.getRequest().getServerPort() + this.getRequest().getContextPath() + "/newPassword"; String resetPassHref = activeuserUrl + "?sid=" + digitalSignature + "&email=" + email; String emailContent = MessageUtil.getMessage(mailActiveContent, resetPassHref); MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom(mailFrom); helper.setTo(email); helper.setSubject(mailActiveSubject); helper.setText(emailContent, true); mailSender.send(mimeMessage); } catch (Exception e) { e.printStackTrace(); } System.out.println(email); }
Example 18
Source File: MailUtil.java From boubei-tss with Apache License 2.0 | 5 votes |
public static void sendHTML(String subject, String htmlText, String receiver[], String _ms, File...attaches) { // 邮件内容,注意加参数true StringBuffer html = new StringBuffer(); html.append("<html>"); html.append("<head>"); html.append("<style type='text/css'> " ); html.append(" body { margin:0; padding:5px; font-family: 微软雅黑; font-size: 15px;}"); html.append("</style>"); html.append("</head>"); html.append("<body>"); html.append(htmlText); html.append("</body>"); html.append("</html>"); JavaMailSenderImpl sender = MailUtil.getMailSender(_ms); MimeMessage mailMsg = sender.createMimeMessage(); try { // 设置utf-8或GBK编码,否则邮件会有乱码 MimeMessageHelper mh = new MimeMessageHelper(mailMsg, true, "utf-8"); mh.setFrom( getEmailFrom(_ms) ); // 发送者 mh.setTo( preCheatEmails(receiver) ); // 接受者 mh.setSubject(subject); // 主题 mh.setText( html.toString(), true ); for(File attach : attaches) { mh.addAttachment(MimeUtility.encodeWord(attach.getName()), attach); } sender.send(mailMsg); } catch (Exception e) { log.error("发送邮件【" +subject+ "】出错了, " + Arrays.asList(receiver) + ", " + e.getMessage()); } }
Example 19
Source File: MailController.java From spring-boot-study with MIT License | 5 votes |
/** * 发送带静态资源的邮件 * */ @GetMapping("/sendInlineMail") public String sendInlineMail(){ MimeMessage message=null; try{ message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message,true); helper.setFrom(sendUser); helper.setTo("[email protected]"); helper.setSubject("主题邮件"); StringBuilder sb=new StringBuilder(); sb.append("<h1>尊敬的客户您好!</h1>") .append("<p>欢迎您访问我的博客 www.fishpro.com.cn</p><p><img src='cid:pic' /></p>"); helper.setText(sb.toString(),true); //获取附件资源 FileSystemResource fileSystemResource =new FileSystemResource(new File("")); //把附件资源加入到发送消息中 helper.addInline("pic",fileSystemResource); }catch (Exception e){ e.printStackTrace(); return "fail"; } mailSender.send(message); return "success"; }
Example 20
Source File: PublisherEarningReminderEmailTransformer.java From website with GNU Affero General Public License v3.0 | 5 votes |
@Transformer public MimeMessage transform(final User user) throws MessagingException, IOException { MimeMessage message = getEmailSender().createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message); helper.setTo(user.getEmail()); helper.setFrom(getDefaultFromAddress(), getDefaultFromName()); helper.setReplyTo(getDefaultFromAddress()); helper.setSubject(getSubject()); Map<String, Object> model = buildModel(user); helper.setText(getText(model), true); return message; }