use of org.springframework.mail.javamail.MimeMessageHelper in project tutorials by eugenp.
the class EmailServiceImpl method sendMessageWithAttachment.
@Override
public void sendMessageWithAttachment(String to, String subject, String text, String pathToAttachment) {
try {
MimeMessage message = emailSender.createMimeMessage();
// pass 'true' to the constructor to create a multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(text);
FileSystemResource file = new FileSystemResource(new File(pathToAttachment));
helper.addAttachment("Invoice", file);
emailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
use of org.springframework.mail.javamail.MimeMessageHelper in project spring-boot by Linda-Tan.
the class TestController method sendEmail.
@RequestMapping("sendemail")
public BaseResponse sendEmail() {
try {
final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
final MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setFrom("li926893@gmail.com");
message.setTo("tyf201314@qq.com");
message.setSubject("测试邮件主题");
message.setText("测试邮件内容");
this.mailSender.send(mimeMessage);
return new ObjectRestResponse("hellowzho直接后果orld");
} catch (Exception ex) {
log.error("发送失败", ex);
return new ObjectRestResponse("发送失败");
}
}
use of org.springframework.mail.javamail.MimeMessageHelper in project spring-boot by Linda-Tan.
the class EmailService method sendBytemplate.
/**
* 发送邮件
* @param mail
* @throws Exception
*/
public void sendBytemplate(Email mail) throws Exception {
Assert.notNull(mail, "email must not be null");
log.info("发送邮件:{}", mail.getContent());
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(fromUser);
helper.setTo(mail.getEmail());
helper.setSubject(mail.getSubject());
if (StringUtils.isNotBlank(mail.getContent()))
helper.setText(mail.getContent());
// 如果有抄送、密送
if (null != mail.getBcc() && mail.getBcc().length > 0)
helper.setBcc(mail.getBcc());
if (null != mail.getCc() && mail.getCc().length > 0)
helper.setCc(mail.getCc());
// 如果有指定模板
if (StringUtils.isNotBlank(mail.getTemplate())) {
Context context = new Context();
context.setVariable("email", mail.getKvMap());
String text = templateEngine.process(mail.getTemplate(), context);
helper.setText(text, true);
}
// 如果有附件
if (null != mail.getAddAttachments() && mail.getAddAttachments().length > 0) {
for (File file : mail.getAddAttachments()) {
helper.addAttachment(file.getName(), file);
}
}
mailSender.send(message);
}
use of org.springframework.mail.javamail.MimeMessageHelper in project ngtesting-platform by aaronchen2k.
the class MailServiceImpl method send.
public void send(String subject, String text, String toEmail) {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper messageHelper = null;
messageHelper = new MimeMessageHelper(mimeMessage, ENCODE);
if (StringUtils.isEmpty(subject)) {
subject = mailMessage.getSubject();
}
try {
messageHelper.setSubject(subject);
messageHelper.setText(text, true);
messageHelper.setTo(toEmail);
messageHelper.setFrom(mailMessage.getFrom());
} catch (MessagingException e) {
e.printStackTrace();
}
mailSender.send(mimeMessage);
}
use of org.springframework.mail.javamail.MimeMessageHelper in project FuryViewer by TheDoctor-95.
the class MailService method sendEmail.
@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, CharEncoding.UTF_8);
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());
}
}
}
Aggregations