本文整理匯總了Java中org.springframework.mail.javamail.JavaMailSender.send方法的典型用法代碼示例。如果您正苦於以下問題:Java JavaMailSender.send方法的具體用法?Java JavaMailSender.send怎麽用?Java JavaMailSender.send使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.springframework.mail.javamail.JavaMailSender
的用法示例。
在下文中一共展示了JavaMailSender.send方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: sendMail
import org.springframework.mail.javamail.JavaMailSender; //導入方法依賴的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);
}
};
}
示例5: main
import org.springframework.mail.javamail.JavaMailSender; //導入方法依賴的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);
}
}
示例6: testSendMultipleMailsWithException
import org.springframework.mail.javamail.JavaMailSender; //導入方法依賴的package包/類
@Test
public void testSendMultipleMailsWithException() throws Exception {
AmazonSimpleEmailService emailService = mock(AmazonSimpleEmailService.class);
JavaMailSender mailSender = new SimpleEmailServiceJavaMailSender(emailService);
MimeMessage failureMail = createMimeMessage();
when(emailService.sendRawEmail(ArgumentMatchers.isA(SendRawEmailRequest.class))).
thenReturn(new SendRawEmailResult()).
thenThrow(new AmazonClientException("error")).
thenReturn(new SendRawEmailResult());
try {
mailSender.send(createMimeMessage(), failureMail, createMimeMessage());
fail("Exception expected due to error while sending mail");
} catch (MailSendException e) {
assertEquals(1, e.getFailedMessages().size());
assertTrue(e.getFailedMessages().containsKey(failureMail));
}
}
示例7: 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;
}
示例8: 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;
}
示例9: 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;
}
示例10: 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);
}
示例11: testSendMimeMessage
import org.springframework.mail.javamail.JavaMailSender; //導入方法依賴的package包/類
@Test
public void testSendMimeMessage() throws MessagingException, IOException {
AmazonSimpleEmailService emailService = mock(AmazonSimpleEmailService.class);
JavaMailSender mailSender = new SimpleEmailServiceJavaMailSender(emailService);
ArgumentCaptor<SendRawEmailRequest> request = ArgumentCaptor.forClass(SendRawEmailRequest.class);
when(emailService.sendRawEmail(request.capture())).thenReturn(new SendRawEmailResult().withMessageId("123"));
MimeMessage mimeMessage = createMimeMessage();
mailSender.send(mimeMessage);
SendRawEmailRequest rawEmailRequest = request.getValue();
assertTrue(Arrays.equals(getMimeMessageAsByteArray(mimeMessage), rawEmailRequest.getRawMessage().getData().array()));
}
示例12: testSendMultipleMimeMessages
import org.springframework.mail.javamail.JavaMailSender; //導入方法依賴的package包/類
@Test
public void testSendMultipleMimeMessages() throws Exception {
AmazonSimpleEmailService emailService = mock(AmazonSimpleEmailService.class);
JavaMailSender mailSender = new SimpleEmailServiceJavaMailSender(emailService);
when(emailService.sendRawEmail(ArgumentMatchers.isA(SendRawEmailRequest.class))).thenReturn(new SendRawEmailResult().withMessageId("123"));
mailSender.send(createMimeMessage(), createMimeMessage());
verify(emailService, times(2)).sendRawEmail(ArgumentMatchers.isA(SendRawEmailRequest.class));
}