JavaMailSenderImpl,发送邮件!

这段代码展示了如何通过JavaMailSenderImpl设置和发送邮件。首先,根据配置前缀获取邮件参数,包括用户名、密码、主机和端口,并设置编码。然后,创建MimeMessage,填充发件人、收件人、抄送人、主题和正文信息。如果存在附件,还会添加到邮件中。最后,尝试发送邮件,若发送失败则返回false,成功则返回true。
摘要由CSDN通过智能技术生成

设置邮箱参数

private JavaMailSenderImpl getMailSender(String configPrefix) {
		if (StringUtils.isEmpty(configPrefix))
			configPrefix = "mail.";
		else if (configPrefix.charAt(configPrefix.length() - 1) != '.')
			configPrefix += '.';
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
		Map<String, String> config;
		if ("mail.".equals(configPrefix)) {
			config = this.paramConfig.getConfigValueWithPrefix(configPrefix);
		} else {
			config = this.paramConfig.getConfigValueWithPrefix(configPrefix, true);
		}
		if (CollectionUtils.isEmpty(config)) {
			return null;
		}
		prepareConfig(config);
		mailSender.setDefaultEncoding("utf-8");
		mailSender.setPassword(config.remove(EmailConstant.KEY_PASSWORD));
		mailSender.setUsername(config.remove(EmailConstant.KEY_USER_NAME));
		mailSender.setHost(config.remove(EmailConstant.KEY_MAIL_HOST));
		mailSender.setPort(BeanUtils.convert(config.remove(EmailConstant.KEY_MAIL_HOST_PORT), -1, Integer.class));
		mailSender.setJavaMailProperties(MapUtils.toProperties(config));
		return mailSender;
	}
public Boolean sendMail(EmailSenderEntity entity, String configPrefix) {
		Assert.notNull(entity, "entity is required");
		JavaMailSenderImpl sender = this.getMailSender(configPrefix);
		if (sender == null) {
			log.warn("No email send configurated.");
			return false;
		}
		if (entity.getFrom() == null) {
			Properties prop = sender.getJavaMailProperties();
			String nick = prop.getProperty(EmailConstant.KEY_NICK);
			String address = prop.getProperty(EmailConstant.KEY_MAIL_FROM);
			entity.setFrom(EmailUtils.formatUserNameAddress(nick, address));
		}
		MimeMessage mimeMessage = sender.createMimeMessage();
		try {
			EmailUtils.prepareEmailMessage(mimeMessage, entity);
			sender.send(mimeMessage);
		} catch (Exception ex) {
			log.error("EmailSenderService->sendMail:", ex);
			return false;
		}
		return true;
	}

public static void prepareEmailMessage(MimeMessage mimeMessage, EmailSenderEntity entity) throws Exception {
		List<MultipartFile> files = entity.getFiles();
		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, !CollectionUtils.isEmpty(files));
		if (entity.getFrom() != null) {
			helper.setFrom(entity.getFrom());
		}
		if (!CollectionUtils.isEmpty(entity.getTo())) {
			helper.setTo(entity.getTo().toArray(new String[0]));
		}
		if (!CollectionUtils.isEmpty(entity.getCc())) {
			List<String> ccList = new ArrayList<>();
			for (String cc : entity.getCc()) {
				if (!StringUtils.isEmpty(cc)) {
					ccList.add(cc);
				}
			}
			if (!ccList.isEmpty())
				helper.setCc(ccList.toArray(new String[0]));
		}
		helper.setSubject(BeanUtils.ifnull(entity.getSubject(), "无主题"));
		if (entity.getBody() != null) {
			helper.setText(entity.getBody(), entity.getBody().indexOf("<body>") > -1);
		}
		// 附件操作
		if (files != null) {
			for (MultipartFile file : files) {
				MultipartFile mpf = (MultipartFile) file;
				if (mpf.getSize() == 0) {
					continue;
				}
				BytesDataSource ds = new BytesDataSource(mpf.getOriginalFilename(), mpf.getBytes());
				ds.setContentType(mpf.getContentType());
				helper.addAttachment(ds.getName(), ds);
			}
		}
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值