Return to Snippet

Revision: 19751
at October 29, 2009 23:55 by arunpjohny


Initial Code
public void mail(final String fromAddress, final String toAddress,
			final String cc, final String subject, final String body,
			Boolean html, final Map<String, File> attach)
			throws MailerException {
		final boolean isHtml = BooleanUtils.toBooleanDefaultIfNull(html, false);
		final boolean multipart = attach != null && attach.size() > 0;
		MimeMessagePreparator preparator = new MimeMessagePreparator() {
			public void prepare(MimeMessage mimeMessage) throws Exception {
				MimeMessageHelper messageHelper = new MimeMessageHelper(
						mimeMessage, multipart, "UTF-8");
				messageHelper.setSubject(subject);
				messageHelper.setTo(toAddress);
				if (!StringUtils.isEmpty(cc)) {
					messageHelper.setCc(cc.split(","));
				}
				// messageHelper.setBcc(bccEmail);
				messageHelper.setFrom(fromAddress);
				messageHelper.setText(body, isHtml);

				if (attach != null) {
					for (Map.Entry<String, File> entry : attach.entrySet()) {
						messageHelper.addAttachment(entry.getKey(), entry
								.getValue());
					}
				}
			}
		};

		try {
			this.mailSender.send(preparator);
		} catch (MailException e) {
			log.error("Error while sending mail", e);
			throw new MailException("Unable to send mail because of a unknown error, please try again", e);
		}
	}

Initial URL


Initial Description


Initial Title
Send mail using Spring JavaMailSender

Initial Tags


Initial Language
Java