本文整理汇总了Java中org.springframework.mail.javamail.MimeMessagePreparator类的典型用法代码示例。如果您正苦于以下问题:Java MimeMessagePreparator类的具体用法?Java MimeMessagePreparator怎么用?Java MimeMessagePreparator使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MimeMessagePreparator类属于org.springframework.mail.javamail包,在下文中一共展示了MimeMessagePreparator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendEmail
import org.springframework.mail.javamail.MimeMessagePreparator; //导入依赖的package包/类
@Override
public void sendEmail(final UserDTO user, String url) {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setTo(user.getEmail());
message.setSubject(SUBJECT);
message.setFrom(EMAIL_FROM); // could be parameterized...
Map model = new HashMap();
model.put("user", user);
model.put("url", url);
String text = VelocityEngineUtils.mergeTemplateIntoString(
velocityEngine, "org/enricogiurin/sushibar/registration-confirmation.vm", model);
message.setText(text, true);
}
};
this.emailSender.send(preparator);
}
示例2: sendWithAttachment
import org.springframework.mail.javamail.MimeMessagePreparator; //导入依赖的package包/类
@Override
public void sendWithAttachment(String to, String subject, String templateName, Map<String, String> data,
InputStream attachment, String attachmentName) {
LOG.debug("Sending email from {} to {} with subject {}", from, to, subject);
MimeMessagePreparator messagePreparator = message -> {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setFrom(from);
helper.setSubject(subject);
helper.setText(templateService.createTemplate(templateName, data));
helper.addAttachment(attachmentName, new ByteArrayResource(IOUtils.toByteArray(attachment)));
};
mailSender.send(messagePreparator);
IOUtils.closeQuietly(attachment);
}
示例3: send
import org.springframework.mail.javamail.MimeMessagePreparator; //导入依赖的package包/类
/**
* Sends e-mail using Velocity template for the body and the properties
* passed in as Velocity variables.
*
* @param msg The e-mail message to be sent, except for the body.
* @param hTemplateVariables Variables to use when processing the template.
*/
protected void send(SimpleMailMessage msg, String language, String template, Map<String, Object> hTemplateVariables) {
LOG.info("Send email ...");
MimeMessagePreparator preparator = (MimeMessage mimeMessage) -> {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "UTF-8");
message.setTo(msg.getTo());
message.setFrom(msg.getFrom());
message.setSubject(msg.getSubject());
String body = VelocityEngineUtils.mergeTemplateIntoString(
velocityEngine,
"/" + template + "." + language + ".vm",
"UTF-8",
hTemplateVariables);
LOG.log(Level.INFO, "Body: {0}", body);
message.setText(body, true);
};
mailSender.send(preparator);
LOG.log(Level.INFO, "Sender {0}", msg.getFrom());
LOG.log(Level.INFO, "Recipient {0}", msg.getTo());
}
示例4: sendMailWithTemplate
import org.springframework.mail.javamail.MimeMessagePreparator; //导入依赖的package包/类
@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);
}
示例5: sendMailPassword
import org.springframework.mail.javamail.MimeMessagePreparator; //导入依赖的package包/类
@Test
public void sendMailPassword() {
final PasswordResource resource = newResource();
exOnPrepare = null;
final UserOrg user = new UserOrg();
user.setFirstName("John");
user.setLastName("Doe");
user.setId("fdauganB");
user.setMails(Collections.singletonList("[email protected]"));
resource.sendMailPassword(user, "password");
MailServicePlugin mailService = resource.servicePluginLocator.getResource("service:mail:smtp:local",
MailServicePlugin.class);
Mockito.verify(mailService, Mockito.atLeastOnce()).send(ArgumentMatchers.eq("service:mail:smtp:local"),
ArgumentMatchers.any(MimeMessagePreparator.class));
}
示例6: sendMailPasswordWithException
import org.springframework.mail.javamail.MimeMessagePreparator; //导入依赖的package包/类
@Test
public void sendMailPasswordWithException() throws MessagingException {
final PasswordResource resource = newResource();
Mockito.when(resource.configurationResource.get("password.mail.url")).thenReturn("host");
final MailServicePlugin mailServicePlugin = resource.servicePluginLocator.getResource("service:mail:smtp:local",
MailServicePlugin.class);
Mockito.when(mailServicePlugin.send(ArgumentMatchers.eq("service:mail:smtp:local"),
ArgumentMatchers.any(MimeMessagePreparator.class))).thenAnswer(
i -> {
throw new BusinessException(null, MimeMessagePreparator.class);
});
final UserOrg user = new UserOrg();
user.setFirstName("John");
user.setLastName("Doe");
user.setId("fdauganB");
user.setMails(Collections.singletonList("[email protected]"));
resource.sendMailPassword(user, "passwd");
}
示例7: sendEmail
import org.springframework.mail.javamail.MimeMessagePreparator; //导入依赖的package包/类
@Override
public boolean sendEmail(final List<String> emails, final String subject, final String body) {
if (emails == null || emails.isEmpty()) {
return false;
}
asyncExecute(new Runnable() {
public void run() {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper msg = new MimeMessageHelper(mimeMessage);
msg.setTo(emails.toArray(new String[0]));
msg.setSubject(subject);
msg.setFrom(Config.SUPPORT_EMAIL);
msg.setText(body, true); // body is assumed to be HTML
}
};
try {
mailSender.send(preparator);
logger.debug("Email sent to {}, {}", emails, subject);
} catch (MailException ex) {
logger.error("Failed to send email. {}", ex.getMessage());
}
}
});
return true;
}
示例8: sendActivation
import org.springframework.mail.javamail.MimeMessagePreparator; //导入依赖的package包/类
@Override
public void sendActivation(User user) throws InvalidTokenException {
if (user.getActivationToken() == null) {
throw new InvalidTokenException("No activation token found for " + user.toString());
}
Map<String, Object> model = getBaseModel(user);
model.put("url", hostname + "#/activation/" + user.getUsername() + "/" + user.getActivationToken().getToken());
final String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "templates/sendActivation.vm", "UTF-8", model);
MimeMessagePreparator preparator = mimeMessage -> {
mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));
mimeMessage.setFrom(new InternetAddress(mailFromAddress));
mimeMessage.setSubject("Kanbanboard WGM Accountaktivierung");
mimeMessage.setText(body, "UTF-8", "html");
};
try {
mailSender.send(preparator);
log.info("Activation Mail sent to "+ user.getEmail());
} catch (MailException e) {
log.error("Could not send activation mail to " + user.getEmail() + ". The error was :", e);
}
}
示例9: sendPasswordReset
import org.springframework.mail.javamail.MimeMessagePreparator; //导入依赖的package包/类
@Override
public void sendPasswordReset(User user) throws InvalidTokenException {
if (user.getPasswordResetToken() == null) {
throw new InvalidTokenException("No password reset token found for " + user.toString());
}
final Map<String, Object> model = getBaseModel(user);
model.put("url", hostname + "#/reset/" + user.getUsername() + "/" + user.getPasswordResetToken().getToken());
final String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "templates/sendPasswordReset.vm", "UTF-8", model);
MimeMessagePreparator preparator = mimeMessage -> {
mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));
mimeMessage.setFrom(new InternetAddress(mailFromAddress));
mimeMessage.setSubject("Kanbanboard WGM Passwort Reset");
mimeMessage.setText(body, "UTF-8", "html");
};
try {
mailSender.send(preparator);
log.info("Reset Mail sent to {}", user.getEmail());
} catch (MailException e) {
log.error("Could not send mail to " + user.getEmail() + ". The error was :", e);
}
}
示例10: sendMail
import org.springframework.mail.javamail.MimeMessagePreparator; //导入依赖的package包/类
private Consumer<UserEntity> sendMail(JavaMailSender mailSender, final MessageEntity messageEntity) {
return foundUser -> {
if (ofNullable(foundUser.getEmail()).isPresent()) {
MimeMessagePreparator preparator = mimeMessage -> {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
message.setFrom("[email protected]");
message.setSubject("New message from @" + messageEntity.getFrom());
message.setTo(foundUser.getEmail());
String textContent = new MailTemplates()
.create("message")
.data("text", messageEntity.getText())
.getText();
String htmlContent = new MailTemplates()
.create("message.html")
.data("text", htmlContent(messageEntity))
.getText();
message.setText(textContent, htmlContent);
};
mailSender.send(preparator);
}
};
}
示例11: senderEmail
import org.springframework.mail.javamail.MimeMessagePreparator; //导入依赖的package包/类
private void senderEmail(final Integer uid, final String username,
final String to, final String resetKey, final String resetPwd,
final EmailSender email, final MessageTemplate tpl) {
JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setHost(email.getHost());
sender.setPort(email.getPort());
sender.setUsername(email.getUsername());
sender.setPassword(email.getPassword());
sender.send(new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage)
throws MessagingException, UnsupportedEncodingException {
MimeMessageHelper msg = new MimeMessageHelper(mimeMessage,
false, email.getEncoding());
msg.setSubject(tpl.getForgotPasswordSubject());
msg.setTo(to);
msg.setFrom(email.getUsername(), email.getPersonal());
String text = tpl.getForgotPasswordText();
text = StringUtils.replace(text, "${uid}", String.valueOf(uid));
text = StringUtils.replace(text, "${username}", username);
text = StringUtils.replace(text, "${resetKey}", resetKey);
text = StringUtils.replace(text, "${resetPwd}", resetPwd);
msg.setText(text);
}
});
}
示例12: main
import org.springframework.mail.javamail.MimeMessagePreparator; //导入依赖的package包/类
public static void main(String[] args) {
try {
System.out.println("----->Sending email message using gmail...");
GoogleAPIServiceAccountClientData serviceAccountClientID = new GoogleAPIServiceAccountClientData(APP_CODE,
GoogleAPIClientID.of("327116756300-thcjqf1mvrn0geefnu6ef3pe2sm61i2q.apps.googleusercontent.com"),
GoogleAPIClientEMailAddress.of("[email protected]account.com"),
GoogleAPIClientIDP12KeyPath.loadedFromFileSystem(SERVICE_ACCOUNT_P12_SECRET_PATH),
EMail.of("[email protected]"),
GmailScopes.all());
JavaMailSender mailSender = GMailAPIMailSender.create(serviceAccountClientID);
// [1] - Create a MimeMessagePreparator
MimeMessagePreparator msgPreparator = _createMimeMessagePreparator(EMail.of("[email protected]"),
EMail.of("[email protected]"),
"A TEST mail message sent using GMail API",
"Just testing GMail API");
// [2] - Send the message
mailSender.send(msgPreparator);
System.out.println("----->Message sent!!");
} catch(Throwable th) {
th.printStackTrace(System.out);
}
}
示例13: send
import org.springframework.mail.javamail.MimeMessagePreparator; //导入依赖的package包/类
@Override
public void send(final SimpleMailMessage msg,
final Map<String, Object> hTemplateVariables, final String templateFileName) {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
@Override
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setTo(msg.getTo());
message.setFrom(msg.getFrom());
message.setSubject(msg.getSubject());
String body = VelocityEngineUtils.mergeTemplateIntoString(
velocityEngine, templateFileName, hTemplateVariables);
//logger.info("body={}", body);
message.setText(body, true);
}
};
mailSender.send(preparator);
}
示例14: sendRegistrationConfirmEmail
import org.springframework.mail.javamail.MimeMessagePreparator; //导入依赖的package包/类
private void sendRegistrationConfirmEmail(final User user) {
final MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setTo(user.getEmail());
message.setFrom("[email protected]");
message.setSubject("注册coconut成功");
Map model = new HashMap();
model.put("user", user);
String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "/templates/registration-confirm-mail.html", "gb2312", model);
message.setText(text, true);
}
};
this.mailSender.send(preparator);
}
示例15: mimeMessagePreparator
import org.springframework.mail.javamail.MimeMessagePreparator; //导入依赖的package包/类
public MimeMessagePreparator mimeMessagePreparator(final EmailData emailData,final ByteArrayOutputStream attachmentAsByteArrResource){
MimeMessagePreparator mailMsg = new MimeMessagePreparator() {
@Override
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper msgHelper = new MimeMessageHelper(mimeMessage, true);
msgHelper.setSubject(emailData.getSubject());
msgHelper.setTo(emailData.getTo().toArray(new String[emailData.getTo().size()]));
msgHelper.setCc(emailData.getCc().toArray(new String[emailData.getCc().size()]));
if(!CollectionUtils.isEmpty(emailData.getBcc())){
msgHelper.setBcc(emailData.getBcc().toArray(new String[emailData.getBcc().size()]));
}
msgHelper.setText(emailData.getBody(), true);
if (attachmentAsByteArrResource != null){
saveHtmlToPdf(emailData.getBody(), attachmentAsByteArrResource);
if(attachmentAsByteArrResource.size() > 0){
msgHelper.addAttachment("Ticket.pdf", new ByteArrayResource(attachmentAsByteArrResource.toByteArray()));
}
}
msgHelper.setFrom(emailData.getFrom());
}
};
return mailMsg;
}