Java Code Examples for org.springframework.mail.javamail.MimeMessageHelper#setCc()
The following examples show how to use
org.springframework.mail.javamail.MimeMessageHelper#setCc() .
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: MailPlugin.java From iotplatform with Apache License 2.0 | 6 votes |
private void sendMail(SendMailActionMsg msg) throws MessagingException { log.debug("Sending mail {}", msg); MimeMessage mailMsg = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mailMsg, "UTF-8"); helper.setFrom(msg.getFrom()); helper.setTo(msg.getTo()); if (!StringUtils.isEmpty(msg.getCc())) { helper.setCc(msg.getCc()); } if (!StringUtils.isEmpty(msg.getBcc())) { helper.setBcc(msg.getBcc()); } helper.setSubject(msg.getSubject()); helper.setText(msg.getBody()); mailSender.send(helper.getMimeMessage()); log.debug("Mail sent {}", msg); }
Example 2
Source File: MailServiceImpl.java From olat with Apache License 2.0 | 6 votes |
public void sendMailWithTemplate(final TemplateMailTO mailParameters) throws MailException { MimeMessagePreparator preparator = new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper message = new MimeMessageHelper(mimeMessage); message.setTo(mailParameters.getToMailAddress()); message.setFrom(mailParameters.getFromMailAddress()); message.setSubject(mailParameters.getSubject()); if (mailParameters.hasCcMailAddress()) { message.setCc(mailParameters.getCcMailAddress()); } if (mailParameters.hasReplyTo()) { message.setReplyTo(mailParameters.getReplyTo()); } String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, mailParameters.getTemplateLocation(), mailParameters.getTemplateProperties()); log.debug("*** TEST text='" + text + "'"); message.setText(text, true); message.setValidateAddresses(true); } }; this.mailSender.send(preparator); }
Example 3
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 4
Source File: MailServiceImpl.java From olat with Apache License 2.0 | 6 votes |
@Override public void sendMailWithTemplate(final TemplateMailTO mailParameters) throws MailException { MimeMessagePreparator preparator = new MimeMessagePreparator() { @Override public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper message = new MimeMessageHelper(mimeMessage); message.setTo(mailParameters.getToMailAddress()); message.setFrom(mailParameters.getFromMailAddress()); message.setSubject(mailParameters.getSubject()); if (mailParameters.hasCcMailAddress()) { message.setCc(mailParameters.getCcMailAddress()); } if (mailParameters.hasReplyTo()) { message.setReplyTo(mailParameters.getReplyTo()); } String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, mailParameters.getTemplateLocation(), mailParameters.getTemplateProperties()); log.debug("*** TEST text='" + text + "'"); message.setText(text, true); message.setValidateAddresses(true); } }; this.mailSender.send(preparator); }
Example 5
Source File: DefaultEmailSender.java From flash-waimai with MIT License | 6 votes |
@Override public boolean sendEmail(String from, String to, String cc, String title, String content, String attachmentFilename, InputStreamSource inputStreamSource) { MimeMessage message = null; try { message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); if(StringUtils.isNotEmpty(cc)) { helper.setCc(cc); } helper.setSubject(title); helper.setText(content, true); if(inputStreamSource!=null) { helper.addAttachment(attachmentFilename, inputStreamSource); } javaMailSender.send(message); return true; } catch (Exception e) { e.printStackTrace(); } return false; }
Example 6
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 7
Source File: JavaMailUtils.java From seppb with MIT License | 6 votes |
private static void buildMimeMessage(MailDTO mailDTO, MimeMessage mimeMessage) throws MessagingException { MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom(verifyNotNull(mailDTO.getFrom(), "邮件发送人不能为空")); helper.setTo(verifyNotNull(mailDTO.getTo(), "邮件接收人不能为空")); helper.setText(mailDTO.getContent(), mailDTO.isHtml()); helper.setSubject(mailDTO.getSubject()); if (!isEmpty(mailDTO.getTocc()) && isNotBlank(mailDTO.getTocc()[0])) { helper.setCc(mailDTO.getTocc()); } if (mailDTO.isHasImage()) { helper.addInline(mailDTO.getImageId(), mailDTO.ResourceImage()); } if (mailDTO.isHasAttachment()) { FileSystemResource docx = new FileSystemResource(new File(mailDTO.getAttachmentPath())); helper.addAttachment(mailDTO.getAttachmentName(), docx); } }
Example 8
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 9
Source File: MailService.java From pacbot with Apache License 2.0 | 5 votes |
private MimeMessagePreparator buildMimeMessagePreparator(String cc,String from, List<String> to, String subject, String mailMessageUrlOrBody, Map<String, Object> placeholderValues, final String attachmentUrl, final Boolean isPlainMessage) { MimeMessagePreparator messagePreparator = mimeMessage -> { MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true); messageHelper.setFrom(from); if(!StringUtils.isEmpty(cc)){ messageHelper.setCc(cc); } 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, placeholderValues); } else { if(!isHttpUrl(mailMessageUrlOrBody)) { File template = new ClassPathResource("templates/".concat(mailMessageUrlOrBody).concat(".html")).getFile(); content = mailContentBuilderService.buildPlainTextMail(FileUtils.readFileToString(template, "UTF-8"), placeholderValues); } else { String mailBody = mailContentBuilderService.getRemoteMailContent(mailMessageUrlOrBody); content = mailContentBuilderService.buildPlainTextMail(mailBody, placeholderValues); } } messageHelper.setText(content, true); }; return messagePreparator; }
Example 10
Source File: EmailJob.java From quartz-manager with MIT License | 5 votes |
@SuppressWarnings("unchecked") private void sendEmail(JobDataMap map) { String subject = map.getString("subject"); String messageBody = map.getString("messageBody"); List<String> to = (List<String>) map.get("to"); List<String> cc = (List<String>) map.get("cc"); List<String> bcc = (List<String>) map.get("bcc"); MimeMessage message = mailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, false); for(String receipient : to) { helper.setFrom("[email protected]", "Julius from Dynamic Quartz"); helper.setTo(receipient); helper.setSubject(subject); helper.setText(messageBody); if(!isEmpty(cc)) helper.setCc(cc.stream().toArray(String[]::new)); if(!isEmpty(bcc)) helper.setBcc(bcc.stream().toArray(String[]::new)); mailSender.send(message); } } catch (MessagingException | UnsupportedEncodingException e) { log.error("An error occurred: {}", e.getLocalizedMessage()); } }
Example 11
Source File: MailService.java From cloud-portal with MIT License | 5 votes |
@SuppressWarnings("rawtypes") public void send(String to, String subject, String templatePath, List<File> attachmentList, Map variableMap) { // logging LOG.info("Sending mail from '{}' to address '{}' and cc addresses '{}' with subject '{}'", from, to, cc, subject); // really send mail if (send) { // create mime message preparator MimeMessagePreparator preparator = new MimeMessagePreparator() { @SuppressWarnings("unchecked") @Override public void prepare(MimeMessage mimeMessage) throws Exception { // get mail text by substituting variables in mail template with velocity String text = velocityService.evaluate(templatePath, variableMap); // create message helper MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true); messageHelper.setFrom(from); messageHelper.setTo(to); messageHelper.setCc(cc.split(",")); messageHelper.setSubject(subject); messageHelper.setText(text, true); // add attachments if available for (File file : attachmentList) { messageHelper.addAttachment(file.getName(), file); } } }; // send HTML mail mailSender.send(preparator); } }
Example 12
Source File: JavaMailAgent.java From kaif with Apache License 2.0 | 5 votes |
public void prepare(MimeMessage mimeMessage) throws MessagingException { MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, Charsets.UTF_8.toString()); try { messageHelper.setFrom(message.getFrom(), message.getFromName()); } catch (UnsupportedEncodingException e) { throw new MessagingException( "UnsupportedEncodingException while encode 'the name of FROM person'", e); } messageHelper.setTo(message.getTo()); if (message.getBcc() != null) { messageHelper.setBcc(message.getBcc()); } if (message.getCc() != null) { messageHelper.setCc(message.getCc()); } if (message.getReplyTo() != null) { messageHelper.setReplyTo(message.getReplyTo()); } messageHelper.setSubject(message.getSubject()); messageHelper.setText(message.getText()); }
Example 13
Source File: SmtpMailer.java From alf.io with GNU General Public License v3.0 | 5 votes |
@Override public void send(EventAndOrganizationId event, String fromName, String to, List<String> cc, String subject, String text, Optional<String> html, Attachment... attachments) { var conf = configurationManager.getFor(Set.of(SMTP_FROM_EMAIL, MAIL_REPLY_TO, SMTP_HOST, SMTP_PORT, SMTP_PROTOCOL, SMTP_USERNAME, SMTP_PASSWORD, SMTP_PROPERTIES), ConfigurationLevel.event(event)); MimeMessagePreparator preparator = mimeMessage -> { MimeMessageHelper message = html.isPresent() || !ArrayUtils.isEmpty(attachments) ? new MimeMessageHelper(mimeMessage, true, "UTF-8") : new MimeMessageHelper(mimeMessage, "UTF-8"); message.setSubject(subject); message.setFrom(conf.get(SMTP_FROM_EMAIL).getRequiredValue(), fromName); String replyTo = conf.get(MAIL_REPLY_TO).getValueOrDefault(""); if(StringUtils.isNotBlank(replyTo)) { message.setReplyTo(replyTo); } message.setTo(to); if(cc != null && !cc.isEmpty()){ message.setCc(cc.toArray(new String[0])); } if (html.isPresent()) { message.setText(text, html.get()); } else { message.setText(text, false); } if (attachments != null) { for (Attachment a : attachments) { message.addAttachment(a.getFilename(), new ByteArrayResource(a.getSource()), a.getContentType()); } } message.getMimeMessage().saveChanges(); message.getMimeMessage().removeHeader("Message-ID"); }; toMailSender(conf).send(preparator); }
Example 14
Source File: MailServiceImpl.java From spring-boot-demo with MIT License | 5 votes |
/** * 发送HTML邮件 * * @param to 收件人地址 * @param subject 邮件主题 * @param content 邮件内容 * @param cc 抄送地址 * @throws MessagingException 邮件发送异常 */ @Override public void sendHtmlMail(String to, String subject, String content, 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); } mailSender.send(message); }
Example 15
Source File: EmailSender.java From open-cloud with MIT License | 5 votes |
/** * 发送邮件 */ public void sendSimpleMail(JavaMailSenderImpl javaMailSender, EmailMessage emailMessage) throws Exception { MimeMessage message = null; message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo(emailMessage.getTo()); if (emailMessage.getCc() != null && emailMessage.getCc().length > 0) { helper.setCc(emailMessage.getCc()); } helper.setFrom(javaMailSender.getUsername()); helper.setSubject(emailMessage.getSubject()); helper.setText(emailMessage.getContent(), true); this.addAttachment(helper, emailMessage); javaMailSender.send(message); }
Example 16
Source File: WebEmail.java From Autumn with GNU General Public License v2.0 | 5 votes |
public void sendHtmlMailOnThymeleaf(String to){ // Prepare the evaluation context final Context ctx = new Context(); ctx.setVariable("username", "test"); ctx.setVariable("link", "https://shuaijunlan.cn/autumn-cms/index.do"); // Prepare message using a Spring helper try { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8"); messageHelper.setFrom(mailMessage.getFrom()); messageHelper.setSubject("激活邮件"); //主题 messageHelper.setTo(to); //发送给 messageHelper.setCc(mailMessage.getFrom()); //抄送 // Create the HTML body using Thymeleaf final String htmlContent = this.templateEngine.process("register-auth", ctx); mimeMessage.setContent(htmlContent, "text/html;charset=utf-8"); // send mail this.mailSender.send(mimeMessage); } catch (MessagingException e) { e.printStackTrace(); } }
Example 17
Source File: EmailUtil.java From SpringBoot-Home with Apache License 2.0 | 4 votes |
/** * 构建复杂邮件信息类 * @param mail * @throws MessagingException */ public void sendMail(MailDomain mail) throws MessagingException { //true表示支持复杂类型 MimeMessageHelper messageHelper = new MimeMessageHelper(mailSender.createMimeMessage(), true); //邮件发信人从配置项读取 mail.setSender(sender); //邮件发信人 messageHelper.setFrom(mail.getSender()); //邮件收信人 messageHelper.setTo(mail.getReceiver().split(",")); //邮件主题 messageHelper.setSubject(mail.getSubject()); //邮件内容 if (mail.getIsTemplate()) { // templateEngine 替换掉动态参数,生产出最后的html String emailContent = templateEngine.process(mail.getEmailTemplateName(), mail.getEmailTemplateContext()); messageHelper.setText(emailContent, true); }else { messageHelper.setText(mail.getText()); } //抄送 if (!StringUtils.isEmpty(mail.getCc())) { messageHelper.setCc(mail.getCc().split(",")); } //密送 if (!StringUtils.isEmpty(mail.getBcc())) { messageHelper.setCc(mail.getBcc().split(",")); } //添加邮件附件 if (mail.getFilePath() != null) { File file = new File(mail.getFilePath()); messageHelper.addAttachment(file.getName(), file); } //发送时间 if (StringUtils.isEmpty(mail.getSentDate())) { messageHelper.setSentDate(mail.getSentDate()); } //正式发送邮件 mailSender.send(messageHelper.getMimeMessage()); }
Example 18
Source File: EmailServiceImpl.java From konker-platform with Apache License 2.0 | 4 votes |
@Override public ServiceResponse<Status> send(String sender, List<User> recipients, List<User> recipientsCopied, String subject, String templateName, Map<String, Object> templateParam, Locale locale) { try { if (!Optional.ofNullable(sender).isPresent() || sender.isEmpty()) { return ServiceResponseBuilder.<Status>error().withMessage(Validations.SENDER_NULL.getCode()).build(); } if (!Optional.ofNullable(recipients).isPresent() || recipients.isEmpty()) { return ServiceResponseBuilder.<Status>error().withMessage(Validations.RECEIVERS_NULL.getCode()).build(); } if (!Optional.ofNullable(subject).isPresent() || subject.isEmpty()) { return ServiceResponseBuilder.<Status>error().withMessage(Validations.SUBJECT_EMPTY.getCode()).build(); } if (!Optional.ofNullable(templateName).isPresent() || templateName.isEmpty()) { return ServiceResponseBuilder.<Status>error().withMessage(Validations.BODY_EMPTY.getCode()).build(); } final Context ctx = new Context(locale); ctx.setVariables(templateParam); final MimeMessage mimeMessage = this.mailSender.createMimeMessage(); final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8"); message.setSubject(subject); message.setFrom(sender); message.setTo(recipients.stream().map(r -> r.getEmail()).collect(Collectors.toList()).toArray(new String[0])); if (Optional.ofNullable(recipientsCopied).isPresent()) { message.setCc(recipientsCopied.stream().map(r -> r.getEmail()).collect(Collectors.toList()).toArray(new String[0])); } final String htmlContent = this.templateEngine.process(templateName, ctx); message.setText(htmlContent, true); this.mailSender.send(mimeMessage); return ServiceResponseBuilder.<Status>ok().build(); } catch (MessagingException e) { return ServiceResponseBuilder.<Status>error() .withMessage(e.getMessage()) .build(); } }
Example 19
Source File: MailComposerImpl.java From yes-cart with Apache License 2.0 | 4 votes |
void composeMessage(final MimeMessage message, final String shopCode, final String locale, final List<String> mailTemplateChain, final String templateName, final String from, final String toEmail, final String ccEmail, final String bccEmail, final Map<String, Object> model) throws MessagingException, IOException, ClassNotFoundException { final MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); helper.setTo(toEmail); helper.setSentDate(new Date()); if (ccEmail != null) { helper.setCc(ccEmail); } if (bccEmail != null) { helper.setBcc(bccEmail); } final String textContent = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".txt", model); final String htmlContent = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".html", model); final String propString = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".properties", model); final Properties prop = new Properties(); if (propString != null) { prop.load(new StringReader(propString)); } helper.setSubject(prop.getProperty("subject")); if (from == null) { helper.setFrom(prop.getProperty("from")); } else { helper.setFrom(from); } final Map<String, byte[]> attachments = collectAttachments(model); composeMessage(helper, textContent, htmlContent, attachments, mailTemplateChain, shopCode, locale, templateName); }
Example 20
Source File: EmailMessageSender.java From magic-starter with GNU Lesser General Public License v3.0 | 4 votes |
/** * 处理复杂邮件类型 * * @param message 邮件内容 * @return boolean */ private boolean processMimeEmail(EmailMessage message) { // 注意邮件发送可能出现异常,注意catch try { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom("\"" + message.getFrom() + "\" <" + username + ">"); helper.setTo(ArrayUtil.toArray(message.getTos(), String.class)); helper.setSubject(message.getSubject()); if (StrUtil.isNotBlank(message.getTemplate())) { // 使用模板生成邮件内容 Context context = new Context(); // 变量设置 message.getParams().forEach(context::setVariable); String content = templateEngine.process(message.getTemplate(), context); helper.setText(content, true); } else { helper.setText(message.getContent(), true); } // 设置抄送人列表 if (CollUtil.isEmpty(message.getCcs())) { helper.setCc(ArrayUtil.toArray(message.getCcs(), String.class)); } // 设置附件 if (CollUtil.isNotEmpty(message.getAttachments())) { for (EmailAttachment attachment : message.getAttachments()) { helper.addAttachment(attachment.getAttachmentName(), attachment.getAttachment()); } } // 设置静态资源 if (CollUtil.isNotEmpty(message.getStaticResources())) { for (EmailStaticResource staticResource : message.getStaticResources()) { helper.addInline(staticResource.getResourceId(), staticResource.getResource()); } } mailSender.send(mimeMessage); return true; } catch (Exception e) { log.error("复杂邮件发送异常!", e); return false; } }