本文整理匯總了Java中org.springframework.mail.javamail.JavaMailSender.createMimeMessage方法的典型用法代碼示例。如果您正苦於以下問題:Java JavaMailSender.createMimeMessage方法的具體用法?Java JavaMailSender.createMimeMessage怎麽用?Java JavaMailSender.createMimeMessage使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.springframework.mail.javamail.JavaMailSender
的用法示例。
在下文中一共展示了JavaMailSender.createMimeMessage方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: sendPasswordResetMail
import org.springframework.mail.javamail.JavaMailSender; //導入方法依賴的package包/類
@Async
public void sendPasswordResetMail(String target,String reseturl,JavaMailSender javaMailSender) throws InterruptedException {
try {
MimeMessage mimeMsg = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, false, "utf-8");
String content = (String) PropertyPlaceholder.getProperty("mail.template");
content = content.replace("{reseturl}", reseturl);
helper.setText(content, true);
helper.setTo(new String[]{target});
String title = (String) PropertyPlaceholder.getProperty("mail.title");
helper.setSubject(title);
String authormail = (String) PropertyPlaceholder.getProperty("mail.sendfrom");
String authorname = (String) PropertyPlaceholder.getProperty("mail.sendname");
helper.setFrom(authormail,authorname);
javaMailSender.send(mimeMsg);
log.info("Password Reset Mail has been send to "+target);
} catch(Exception e) {
e.printStackTrace();
}
}
示例2: createHtmlEmail
import org.springframework.mail.javamail.JavaMailSender; //導入方法依賴的package包/類
private static void createHtmlEmail(int i){
try {
JavaMailSender sender = getEmailSender();
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setTo("[email protected]");
helper.setFrom("[email protected]");
helper.setSubject("Test-Mail " + i);
helper.setText("<html><head></head><body>This is the test mail number " + i + "</body>", true);
sender.send(message);
} catch (MessagingException e){
throw new RuntimeException("Failed to create mail", e);
}
}
示例3: mailTo
import org.springframework.mail.javamail.JavaMailSender; //導入方法依賴的package包/類
@Async
public void mailTo(String[] targetuser,String title,String content,JavaMailSender javaMailSender) {//郵件功能
try {
MimeMessage mimeMsg = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, false, "utf-8");
helper.setText(content, true);
helper.setBcc(targetuser);
helper.setSubject(title);
String authormail = (String) PropertyPlaceholder.getProperty("mail.sendfrom");
String authorname = (String) PropertyPlaceholder.getProperty("mail.sendname");
helper.setFrom(authormail,authorname);
javaMailSender.send(mimeMsg);
log.info("Mail send to "+Arrays.toString(targetuser));
} catch(Exception e) {
e.printStackTrace();
}
}
示例4: testCreateMimeMessageWithExceptionInInputStream
import org.springframework.mail.javamail.JavaMailSender; //導入方法依賴的package包/類
@Test
public void testCreateMimeMessageWithExceptionInInputStream() throws Exception {
InputStream inputStream = mock(InputStream.class);
AmazonSimpleEmailService emailService = mock(AmazonSimpleEmailService.class);
JavaMailSender mailSender = new SimpleEmailServiceJavaMailSender(emailService);
IOException ioException = new IOException("error");
when(inputStream.read(ArgumentMatchers.any(byte[].class), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt())).thenThrow(ioException);
try {
mailSender.createMimeMessage(inputStream);
fail("MailPreparationException expected due to error while creating mail");
} catch (MailParseException e) {
assertTrue(e.getMessage().startsWith("Could not parse raw MIME content"));
assertSame(ioException, e.getCause().getCause());
}
}
示例5: sendMailSpring
import org.springframework.mail.javamail.JavaMailSender; //導入方法依賴的package包/類
/**
* 發送郵件 (完整版)(結合Spring)
*
* //@param javaMailSender: 發送Bean
* //@param sendFrom : 發送人郵箱
* //@param sendNick : 發送人昵稱
* @param toAddress : 收件人郵箱
* @param mailSubject : 郵件主題
* @param mailBody : 郵件正文
* @param mailBodyIsHtml: 郵件正文格式,true:HTML格式;false:文本格式
* @param attachments : 附件
*/
@SuppressWarnings("null")
public static boolean sendMailSpring(String toAddress, String mailSubject, String mailBody, boolean mailBodyIsHtml,File[] attachments) {
JavaMailSender javaMailSender = null;//ResourceBundle.getInstance().getJavaMailSender();
try {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, ArrayUtils.isNotEmpty(attachments), "UTF-8"); // 設置utf-8或GBK編碼,否則郵件會有亂碼;multipart,true表示文件上傳
helper.setFrom(username, sendNick);
helper.setTo(toAddress);
// 設置收件人抄送的名片和地址(相當於群發了)
//helper.setCc(InternetAddress.parse(MimeUtility.encodeText("郵箱001") + " <@163.com>," + MimeUtility.encodeText("郵箱002") + " <@foxmail.com>"));
helper.setSubject(mailSubject);
helper.setText(mailBody, mailBodyIsHtml);
// 添加附件
if (ArrayUtils.isNotEmpty(attachments)) {
for (File file : attachments) {
helper.addAttachment(MimeUtility.encodeText(file.getName()), file);
}
}
// 群發
//MimeMessage[] mailMessages = { mimeMessage };
javaMailSender.send(mimeMessage);
return true;
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return false;
}
示例6: sendMailSpring
import org.springframework.mail.javamail.JavaMailSender; //導入方法依賴的package包/類
/**
* 發送郵件 (完整版)(結合Spring)
*
* //@param javaMailSender: 發送Bean
* //@param sendFrom : 發送人郵箱
* //@param sendNick : 發送人昵稱
* @param toAddress : 收件人郵箱
* @param mailSubject : 郵件主題
* @param mailBody : 郵件正文
* @param mailBodyIsHtml: 郵件正文格式,true:HTML格式;false:文本格式
* @param attachments : 附件
*/
@SuppressWarnings("null")
public static boolean sendMailSpring(String toAddress, String mailSubject, String mailBody, boolean mailBodyIsHtml,File[] attachments) {
JavaMailSender javaMailSender = null;//ResourceBundle.getInstance().getJavaMailSender();
try {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, ArrayUtils.isNotEmpty(attachments), "UTF-8"); // 設置utf-8或GBK編碼,否則郵件會有亂碼;multipart,true表示文件上傳
helper.setFrom(sendFrom, sendNick);
helper.setTo(toAddress);
// 設置收件人抄送的名片和地址(相當於群發了)
//helper.setCc(InternetAddress.parse(MimeUtility.encodeText("郵箱001") + " <@163.com>," + MimeUtility.encodeText("郵箱002") + " <@foxmail.com>"));
helper.setSubject(mailSubject);
helper.setText(mailBody, mailBodyIsHtml);
// 添加附件
if (ArrayUtils.isNotEmpty(attachments)) {
for (File file : attachments) {
helper.addAttachment(MimeUtility.encodeText(file.getName()), file);
}
}
// 群發
//MimeMessage[] mailMessages = { mimeMessage };
javaMailSender.send(mimeMessage);
return true;
} catch (Exception e) {
logger.info("{}", e);
}
return false;
}
示例7: sendMailSpring
import org.springframework.mail.javamail.JavaMailSender; //導入方法依賴的package包/類
/**
* 發送郵件 (完整版)(結合Spring)
*
* @param toAddress : 收件人郵箱
* @param mailSubject : 郵件主題
* @param mailBody : 郵件正文
* @param mailBodyIsHtml: 郵件正文格式,true:HTML格式;false:文本格式
* @param attachments : 附件
*/
public static boolean sendMailSpring(String toAddress, String mailSubject, String mailBody,
boolean mailBodyIsHtml,File[] attachments) {
JavaMailSender javaMailSender = (JavaMailSender) SpringContentAwareUtil.getBeanByName("javaMailSender");
try {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, (attachments!=null && attachments.length>0), "UTF-8"); // 設置utf-8或GBK編碼,否則郵件會有亂碼;multipart,true表示文件上傳
helper.setFrom(username, sendNick);
helper.setTo(toAddress);
// 設置收件人抄送的名片和地址(相當於群發了)
//helper.setCc(InternetAddress.parse(MimeUtility.encodeText("郵箱001") + " <@163.com>," + MimeUtility.encodeText("郵箱002") + " <@foxmail.com>"));
helper.setSubject(mailSubject);
helper.setText(mailBody, mailBodyIsHtml);
// 添加附件
if (attachments!=null && attachments.length>0) {
for (File file : attachments) {
helper.addAttachment(MimeUtility.encodeText(file.getName()), file);
}
}
// 群發
//MimeMessage[] mailMessages = { mimeMessage };
javaMailSender.send(mimeMessage);
return true;
} catch (Exception e) {
logger.info("{}", e);
}
return false;
}
示例8: send
import org.springframework.mail.javamail.JavaMailSender; //導入方法依賴的package包/類
public static void send(EmailHelper emailHelper, JavaMailSender javaMailSender) throws MessagingException {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
helper.setTo(emailHelper.to);
helper.setFrom(emailHelper.from); //to avoid spam filters
helper.setReplyTo(emailHelper.replyTo);
helper.setSubject(emailHelper.subject);
helper.setText(emailHelper.text, emailHelper.html);
javaMailSender.send(message);
}
示例9: createMimeMessage_withDefaultPropertiesAndNoEncodingAndFileTypeMap_returnsSessionWithEmptyProperties
import org.springframework.mail.javamail.JavaMailSender; //導入方法依賴的package包/類
@Test
public void createMimeMessage_withDefaultPropertiesAndNoEncodingAndFileTypeMap_returnsSessionWithEmptyProperties() throws Exception {
// Arrange
JavaMailSender mailSender = new SimpleEmailServiceJavaMailSender(null);
// Act
MimeMessage mimeMessage = mailSender.createMimeMessage();
// Assert
assertNotNull(mimeMessage);
assertEquals(0, mimeMessage.getSession().getProperties().size());
}
示例10: testCreateMimeMessageFromPreDefinedMessage
import org.springframework.mail.javamail.JavaMailSender; //導入方法依賴的package包/類
@Test
public void testCreateMimeMessageFromPreDefinedMessage() throws Exception {
JavaMailSender mailSender = new SimpleEmailServiceJavaMailSender(null);
MimeMessage original = createMimeMessage();
MimeMessage mimeMessage = mailSender.createMimeMessage(new ByteArrayInputStream(getMimeMessageAsByteArray(original)));
assertNotNull(mimeMessage);
assertEquals(original.getSubject(), mimeMessage.getSubject());
assertEquals(original.getContent(), mimeMessage.getContent());
assertEquals(original.getRecipients(Message.RecipientType.TO)[0], mimeMessage.getRecipients(Message.RecipientType.TO)[0]);
}