当前位置: 首页>>代码示例>>Java>>正文


Java JavaMailSender.send方法代码示例

本文整理汇总了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();
	}
}
 
开发者ID:zjlywjh001,项目名称:PhrackCTF-Platform-Personal,代码行数:21,代码来源:MailUtil.java

示例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);
    }
}
 
开发者ID:gessnerfl,项目名称:fake-smtp-server,代码行数:17,代码来源:TestDataCreator.java

示例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();
	}
}
 
开发者ID:zjlywjh001,项目名称:PhrackCTF-Platform-Team,代码行数:18,代码来源:MailUtil.java

示例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);
    }
  };
}
 
开发者ID:adrobisch,项目名称:putput,代码行数:27,代码来源:NewMessageFlow.java

示例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);
	}
}
 
开发者ID:opendata-euskadi,项目名称:r01fb,代码行数:25,代码来源:GMailAPI2Test.java

示例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));
    }
}
 
开发者ID:spring-cloud,项目名称:spring-cloud-aws,代码行数:21,代码来源:SimpleEmailServiceJavaMailSenderTest.java

示例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;
}
 
开发者ID:mmwhd,项目名称:stage-job,代码行数:47,代码来源:MailUtil.java

示例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;
}
 
开发者ID:kevinKaiF,项目名称:xxl-job,代码行数:46,代码来源:MailUtil.java

示例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;
}
 
开发者ID:xuxueli,项目名称:xxl-incubator,代码行数:43,代码来源:MailUtil.java

示例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);

}
 
开发者ID:solita,项目名称:kansalaisaloite,代码行数:15,代码来源:EmailSender.java

示例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()));
}
 
开发者ID:spring-cloud,项目名称:spring-cloud-aws,代码行数:13,代码来源:SimpleEmailServiceJavaMailSenderTest.java

示例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));
}
 
开发者ID:spring-cloud,项目名称:spring-cloud-aws,代码行数:12,代码来源:SimpleEmailServiceJavaMailSenderTest.java


注:本文中的org.springframework.mail.javamail.JavaMailSender.send方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。