Java Code Examples for org.springframework.mail.SimpleMailMessage#setText()

The following examples show how to use org.springframework.mail.SimpleMailMessage#setText() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MailServiceImpl.java    From Java-API-Test-Examples with Apache License 2.0 6 votes vote down vote up
/**
 * 发送文本邮件
 * @param to
 * @param subject
 * @param content
 */
@Override
public void sendSimpleMail(String to, String subject, String content) {
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(from);
    message.setTo(to);
    message.setSubject(subject);
    message.setText(content);

    try {
        mailSender.send(message);
        log.info("简单邮件已经发送。");
    } catch (Exception e) {
        log.error("发送简单邮件时发生异常!", e);
    }

}
 
Example 2
Source File: AbstarctMailSender.java    From beihu-boot with Apache License 2.0 6 votes vote down vote up
private void doSendMail(String email, String message, String detail) {
    if (javaMailSender == null || StringUtils.isBlank(sendFrom) ||
            StringUtils.isBlank(email) || StringUtils.isBlank(prefix)) {
        logger.error("=====================【邮件服务异常】初始化失败! =======================");
        return;
    }
    String[] tos = StringUtils.split(email, ",");
    if (tos.length < 1) {
        logger.warn("=====================【邮件服务异常】无发送列表! =======================");
        return;
    }
    try {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(sendFrom);
        simpleMailMessage.setTo(tos);
        simpleMailMessage.setSubject(prefix + " : " + message);
        simpleMailMessage.setText(detail);
        simpleMailMessage.setSentDate(new Date());
        javaMailSender.send(simpleMailMessage);//发送邮件
        logger.info("==========================邮件发送成功==========================");
    } catch (Exception e) {
        logger.error("==========================邮件发送失败==========================", e);
    }
}
 
Example 3
Source File: EmailUtil.java    From pre with GNU General Public License v3.0 6 votes vote down vote up
public void sendSimpleMail(String to, HttpServletRequest request) {
    SimpleMailMessage message = new SimpleMailMessage();
    //设定邮件参数
    //发送者
    message.setFrom(username);
    //接收者
    message.setTo(to);
    //主题
    message.setSubject("修改pre系统用户邮箱");
    //邮件内容
    // 验证码
    String code = PreUtil.codeGen(4);
    message.setText("【Pre系统】" + ",验证码:" + code + "。" + "你正在使用邮箱验证码修改功能,该验证码仅用于身份验证,请勿透露给他人使用");
    // 发送邮件
    javaMailSender.send(message);
    request.getSession().setAttribute(PreConstant.RESET_MAIL, code.toLowerCase());
}
 
Example 4
Source File: DefaultEmailIdentifierNotificationStrategy.java    From openregistry with Apache License 2.0 6 votes vote down vote up
public void notifyPerson(Person person, Role role, Type addressType,
		Identifier idToNotifyAbout, String activationKeyString)
		throws IllegalArgumentException, IllegalStateException {

	if (person == null) {
		throw new IllegalArgumentException("Person must be specified");
	}
	if (idToNotifyAbout == null) {
		throw new IllegalArgumentException("Identifier must be specified");
	}
	
	Name officialName = person.getOfficialName();
	
	SimpleMailMessage msg = new SimpleMailMessage();
	
	msg.setTo(findEmailAddressesForNotification(person, role, addressType));
	msg.setFrom(messageSource.getMessage("activation.notify.from", null, null));
	msg.setSubject(messageSource.getMessage("activation.notify.subject", null, null));
	String greeting = (officialName == null) ? "" : officialName.getFormattedName();
	msg.setText(messageSource.getMessage
			("activation.notify.body", 
			new String[] {greeting,idToNotifyAbout.getValue(),activationKeyString},
			null));
	mailSender.send(msg);
}
 
Example 5
Source File: MailPublisher.java    From logsniffer with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void publish(final Event event) throws PublishException {
	try {
		final VelocityContext context = velocityRenderer.getContext(event);
		final SimpleMailMessage email = new SimpleMailMessage();
		email.setFrom(getFrom());
		email.setSubject(velocityRenderer.render(getSubject(), context));
		email.setText(velocityRenderer.render(getTextMessage(), context) + " ");
		final String to2 = getTo();
		email.setTo(to2.split(",|\\s"));
		mailSender.send(email);
		logger.info("Sent event notification to: {}", to2);
	} catch (final MailException e) {
		throw new PublishException("Failed to send event notification to mail: " + getTo(), e);
	}
}
 
Example 6
Source File: MailController.java    From spring-boot-study with MIT License 6 votes vote down vote up
/**
 * 发送文本标题 文本内容
 * */
@GetMapping("/sendMail")
public String sendMail(){
    try{

        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(sendUser);
        message.setTo("[email protected]");
        message.setSubject("主题邮件");
        message.setText("邮件内容");
        mailSender.send(message);
        return "success";
    }catch (Exception e){
        e.printStackTrace();
        return "fail";
    }
}
 
Example 7
Source File: MailMessage.java    From Blog with Apache License 2.0 5 votes vote down vote up
public SimpleMailMessage create(String toMail, String subject, String text) {
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(fromMail);
    message.setTo(toMail);
    message.setSubject(subject);
    message.setText(text);
    return message;
}
 
Example 8
Source File: EmailBuilder.java    From zheshiyigeniubidexiangmu with MIT License 5 votes vote down vote up
/**
 * 发送简单邮件
 * @param from 发件人
 * @param to 收件人
 * @param subject 主题
 * @param text 正文
 */
public void sendSimpleEmail(String from, String to, String subject, String text){
    SimpleMailMessage message = new SimpleMailMessage();//消息构造器
    message.setFrom(from);//发件人
    message.setTo(to);//收件人
    message.setSubject(subject);//主题
    message.setText(text);//正文
    this.send(message);
}
 
Example 9
Source File: MailSender.java    From restfiddle with Apache License 2.0 5 votes vote down vote up
public void sendMail(String from, String to, String subject, String msg) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(msg);

javaMailSender.send(message);
   }
 
Example 10
Source File: MailTest.java    From sso with MIT License 5 votes vote down vote up
@Test
    public void connect() {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(mail);
        message.setTo(mail); //自己给自己发送邮件
        message.setSubject("主题:测试邮件");
        message.setText("测试邮件内容");

        //可以进行测试
//        mailSender.send(message);
    }
 
Example 11
Source File: DemoController.java    From seed with Apache License 2.0 5 votes vote down vote up
@GetMapping("/mail")
public CommResult mail(){
    //发送一封简单的邮件
    SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
    simpleMailMessage.setFrom(this.mailFrom);
    simpleMailMessage.setTo("[email protected]");
    simpleMailMessage.setSubject("下午开会准时参加");
    simpleMailMessage.setText("15点26楼会议室");
    this.javaMailSender.send(simpleMailMessage);
    //发送一封复杂的邮件
    //注意addInline()里面的"huiyi"要与"cid:huiyi"一致
    //注意addAttachment()方法用于添加附件
    try{
        MimeMessage mimeMessage = this.javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom(this.mailFrom);
        helper.setTo("[email protected]");
        helper.setSubject("请查收会议纪要");
        helper.setText("<html><body><span style='color:#F00'>会议截图如下,完整图片见附件。</span><br><img src=\\\"cid:huiyi\\\" ></body></html>", true);
        helper.addInline("huiyi", new FileSystemResource(new File("E:\\Jadyer\\Stripes.jpg")));
        helper.addAttachment("会议纪要完整图片.jpg", new FileSystemResource(new File("E:\\Jadyer\\Fedora13.jpg")));
        this.javaMailSender.send(mimeMessage);
    }catch(MessagingException e){
        return CommResult.fail(CodeEnum.SYSTEM_BUSY.getCode(), "邮件发送失败,堆栈轨迹如下:"+ JadyerUtil.extractStackTrace(e));
    }
    return CommResult.success();
}
 
Example 12
Source File: RecordingMailSenderUnitTests.java    From salespoint with Apache License 2.0 5 votes vote down vote up
@Test // #149
void exposesSentEmails() {

	SimpleMailMessage message = new SimpleMailMessage();
	message.setFrom("me");
	message.setTo("you");
	message.setSubject("subject");
	message.setText("text");

	RecordingMailSender sender = new RecordingMailSender();
	sender.send(message);

	assertThat(sender.getSentMessages()).containsExactly(message);
}
 
Example 13
Source File: MailMessageNotifier.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
/**
 * Send mail messages.
 *
 * @param simpleMessages
 */
@Override
public void send(GenericNotifyMessage msg) {
    String mailMsgType = msg.getParameterAsString(KEY_MAILMSG_TYPE, "simple");
    switch (mailMsgType) {
        case KEY_MAILMSG_VALUE_SIMPLE:
            SimpleMailMessage simpleMsg = new SimpleMailMessage();
            // Add "<>" symbol to send out?
            /*
             * Preset from account, otherwise it would be wrong: 501 mail from
             * address must be same as authorization user.
             */
            simpleMsg.setFrom(config.getUsername() + "<" + config.getUsername() + ">");
            simpleMsg.setTo(msg.getToObjects().stream().map(to -> to = to + "<" + to + ">").collect(toList()).toArray(new String[]{}));
            simpleMsg.setSubject(msg.getParameterAsString(KEY_MAILMSG_SUBJECT, "Super Devops Messages"));
            simpleMsg.setSentDate(msg.getParameter(KEY_MSG_SENDDATE, new Date()));
            simpleMsg.setBcc(safeList(msg.getParameter(KEY_MAILMSG_BCC)).toArray(new String[]{}));
            simpleMsg.setCc(safeList(msg.getParameter(KEY_MAILMSG_CC)).toArray(new String[]{}));
            simpleMsg.setReplyTo(msg.getParameter(KEY_MAILMSG_REPLYTO));
            simpleMsg.setText(config.getResolvedMessage(msg.getTemplateKey(), msg.getParameters()));

            mailSender.send(simpleMsg);
            break;
        case KEY_MAILMSG_VALUE_MIME: // TODO implements!!!
            log.warn("No implements MimeMailMessage!!!");
            break;
        default:
            throw new UnsupportedOperationException(format("No supported mail message type of %s", mailMsgType));
    }

}
 
Example 14
Source File: ApplicationTests.java    From springBoot-study with Apache License 2.0 5 votes vote down vote up
@Test
public void sendSimpleMail() throws Exception {
	SimpleMailMessage message = new SimpleMailMessage();
	message.setFrom("[email protected]");
	message.setTo("[email protected]");
	message.setSubject("主题:这是一封测试邮件!");
	message.setText("测试邮件内容");

	mailSender.send(message);
	System.out.println("发送成功!");
}
 
Example 15
Source File: Controller.java    From open-capacity-platform with Apache License 2.0 5 votes vote down vote up
@GetMapping("/send")
public String send() {
	SimpleMailMessage message = new SimpleMailMessage();

	message.setFrom("[email protected]");
	message.setTo("[email protected]");
	message.setSubject("测试邮件");
	message.setText("好好学习");
	javaMailSender.send(message);
	return "hello";
}
 
Example 16
Source File: NotifyService.java    From dts-shop with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 邮件消息通知, 接收者在spring.mail.sendto中指定
 *
 * @param subject
 *            邮件标题
 * @param content
 *            邮件内容
 */
@Async
public void notifyMail(String subject, String content) {
	if (mailSender == null)
		return;

	SimpleMailMessage message = new SimpleMailMessage();
	message.setFrom(sendFrom);
	message.setTo(sendTo);
	message.setSubject(subject);
	message.setText(content);
	mailSender.send(message);
}
 
Example 17
Source File: EmailBuilder.java    From zheshiyigeniubidexiangmu with MIT License 5 votes vote down vote up
/**
 * 发送简单邮件
 * @param from 发件人
 * @param to 收件人
 * @param subject 主题
 * @param text 正文
 */
public void sendSimpleEmail(String from, String to, String subject, String text){
    SimpleMailMessage message = new SimpleMailMessage();//消息构造器
    message.setFrom(from);//发件人
    message.setTo(to);//收件人
    message.setSubject(subject);//主题
    message.setText(text);//正文
    this.send(message);
}
 
Example 18
Source File: MailSenderUtil.java    From ChengFeng1.5 with MIT License 5 votes vote down vote up
public void sendSimpleMail(String to, String subject, String content) {
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(from);
    message.setTo(to);
    message.setSubject(subject);
    message.setText(content);
    try {
        mailSender.send(message);
        log.info("简单邮件已经发送。");
    } catch (Exception e) {
        log.error("发送简单邮件时发⽣异常!", e);
    }
}
 
Example 19
Source File: MailTool.java    From maintain with MIT License 5 votes vote down vote up
public static void sendMail(JavaMailSender mailSender, String from, String to, String subject, String text) {
	try {
		
		SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom(from);// 发送者.
		message.setTo(to);// 接收者.
		message.setSubject(subject);// 邮件主题.
		message.setText(text);// 邮件内容.

		mailSender.send(message);// 发送邮件
		
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 20
Source File: NeteaseMailUtil.java    From charging_pile_cloud with MIT License 4 votes vote down vote up
/**
 * @param to    目的邮箱
 * @param title 标题
 * @param text  内容
 * @return
 */
private static boolean sendMailText(String to, String title, String text) {
    JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
    SimpleMailMessage mailMessage = new SimpleMailMessage();
    Properties prop = new Properties();
    System.out.println("sendMail...util...");
    try {
        //设定mail server
        senderImpl.setHost(emailHost);
        //senderImpl.setPort(587);

        // 设置收件人,寄件人 用数组发送多个邮件
        // String[] array = new String[]    {"[email protected]","[email protected]"};
        // mailMessage.setTo(array);
        mailMessage.setTo(to);
        mailMessage.setFrom(sendEmail);
        mailMessage.setSubject(title);
        mailMessage.setText(text);

        senderImpl.setUsername(emailUsername);
        senderImpl.setPassword(authorizationPwd);

        prop.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

        prop.setProperty("mail.smtp.socketFactory.fallback", "false");

        prop.setProperty("mail.smtp.port", "465");
        prop.setProperty("mail.smtp.socketFactory.port", "465");

        // 设置传输协议
        prop.setProperty("mail.transport.protocol", "smtp");

        prop.put("mail.smtp.auth", isAuth);
        prop.put("mail.smtp.timeout", linkTimeout);
        senderImpl.setJavaMailProperties(prop);
        System.out.println("发送信息:" + text);

        MimeMessage msg = senderImpl.createMimeMessage();
        msg.addRecipients(MimeMessage.RecipientType.CC, InternetAddress.parse(sendEmail));
        //发送邮件
        senderImpl.send(mailMessage);

        System.out.println("发送邮件成功");

        return true;
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("发送邮件失败");
        return false;
    }
}