JavaMail是提供给开发者处理电子邮件相关的编程接口。它是Sun发布的用来处理email的API。正常我们会用JavaMail相关api来写发送邮件的相关代码。

使用过Spring的众多开发者都知道Spring提供了非常好用的 JavaMailSender接口实现邮件发送。在Spring Boot的Starter模块中也为此提供了自动化配置。

SpringBoot集成邮件引入依赖

org.springframework.boot

spring-boot-starter-mail

底层是使用MailMessage接口

public interface MailMessage {

void setFrom(String var1) throws MailParseException;

void setReplyTo(String var1) throws MailParseException;

void setTo(String var1) throws MailParseException;

void setTo(String... var1) throws MailParseException;

void setCc(String var1) throws MailParseException;

void setCc(String... var1) throws MailParseException;

void setBcc(String var1) throws MailParseException;

void setBcc(String... var1) throws MailParseException;

void setSentDate(Date var1) throws MailParseException;

void setSubject(String var1) throws MailParseException;

void setText(String var1) throws MailParseException;

}

MailMessage有两个实现类,分别是SimpleMailMessage和MimeMailMessage

SimpleMailMessage源码

public class SimpleMailMessage implements MailMessage, Serializable {

@Nullable

private String from;

@Nullable

private String replyTo;

@Nullable

private String[] to;

@Nullable

private String[] cc;

@Nullable

private String[] bcc;

@Nullable

private Date sentDate;

@Nullable

private String subject;

@Nullable

private String text;

public SimpleMailMessage() {

}

public SimpleMailMessage(SimpleMailMessage original) {

Assert.notNull(original, "'original' message argument must not be null");

this.from = original.getFrom();

this.replyTo = original.getReplyTo();

this.to = copyOrNull(original.getTo());

this.cc = copyOrNull(original.getCc());

this.bcc = copyOrNull(original.getBcc());

this.sentDate = original.getSentDate();

this.subject = original.getSubject();

this.text = original.getText();

}

public void setFrom(String from) {

this.from = from;

}

@Nullable

public String getFrom() {

return this.from;

}

public void setReplyTo(String replyTo) {

this.replyTo = replyTo;

}

@Nullable

public String getReplyTo() {

return this.replyTo;

}

public void setTo(String to) {

this.to = new String[]{to};

}

public void setTo(String... to) {

this.to = to;

}

@Nullable

public String[] getTo() {

return this.to;

}

public void setCc(String cc) {

this.cc = new String[]{cc};

}

public void setCc(String... cc) {

this.cc = cc;

}

@Nullable

public String[] getCc() {

return this.cc;

}

public void setBcc(String bcc) {

this.bcc = new String[]{bcc};

}

public void setBcc(String... bcc) {

this.bcc = bcc;

}

@Nullable

public String[] getBcc() {

return this.bcc;

}

public void setSentDate(Date sentDate) {

this.sentDate = sentDate;

}

@Nullable

public Date getSentDate() {

return this.sentDate;

}

public void setSubject(String subject) {

this.subject = subject;

}

@Nullable

public String getSubject() {

return this.subject;

}

public void setText(String text) {

this.text = text;

}

@Nullable

public String getText() {

return this.text;

}

public void copyTo(MailMessage target) {

Assert.notNull(target, "'target' MailMessage must not be null");

if (this.getFrom() != null) {

target.setFrom(this.getFrom());

}

if (this.getReplyTo() != null) {

target.setReplyTo(this.getReplyTo());

}

if (this.getTo() != null) {

target.setTo(copy(this.getTo()));

}

if (this.getCc() != null) {

target.setCc(copy(this.getCc()));

}

if (this.getBcc() != null) {

target.setBcc(copy(this.getBcc()));

}

if (this.getSentDate() != null) {

target.setSentDate(this.getSentDate());

}

if (this.getSubject() != null) {

target.setSubject(this.getSubject());

}

if (this.getText() != null) {

target.setText(this.getText());

}

}

public boolean equals(Object other) {

if (this == other) {

return true;

} else if (!(other instanceof SimpleMailMessage)) {

return false;

} else {

SimpleMailMessage otherMessage = (SimpleMailMessage)other;

return ObjectUtils.nullSafeEquals(this.from, otherMessage.from) && ObjectUtils.nullSafeEquals(this.replyTo, otherMessage.replyTo) && ObjectUtils.nullSafeEquals(this.to, otherMessage.to) && ObjectUtils.nullSafeEquals(this.cc, otherMessage.cc) && ObjectUtils.nullSafeEquals(this.bcc, otherMessage.bcc) && ObjectUtils.nullSafeEquals(this.sentDate, otherMessage.sentDate) && ObjectUtils.nullSafeEquals(this.subject, otherMessage.subject) && ObjectUtils.nullSafeEquals(this.text, otherMessage.text);

}

}

public int hashCode() {

int hashCode = ObjectUtils.nullSafeHashCode(this.from);

hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.replyTo);

hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.to);

hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.cc);

hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.bcc);

hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.sentDate);

hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.subject);

return hashCode;

}

public String toString() {

StringBuilder sb = new StringBuilder("SimpleMailMessage: ");

sb.append("from=").append(this.from).append("; ");

sb.append("replyTo=").append(this.replyTo).append("; ");

sb.append("to=").append(StringUtils.arrayToCommaDelimitedString(this.to)).append("; ");

sb.append("cc=").append(StringUtils.arrayToCommaDelimitedString(this.cc)).append("; ");

sb.append("bcc=").append(StringUtils.arrayToCommaDelimitedString(this.bcc)).append("; ");

sb.append("sentDate=").append(this.sentDate).append("; ");

sb.append("subject=").append(this.subject).append("; ");

sb.append("text=").append(this.text);

return sb.toString();

}

@Nullable

private static String[] copyOrNull(@Nullable String[] state) {

return state == null ? null : copy(state);

}

private static String[] copy(String[] state) {

String[] copy = new String[state.length];

System.arraycopy(state, 0, copy, 0, state.length);

return copy;

}

}

MimeMailMessage源码

public class MimeMailMessage implements MailMessage {

private final MimeMessageHelper helper;

public MimeMailMessage(MimeMessageHelper mimeMessageHelper) {

this.helper = mimeMessageHelper;

}

public MimeMailMessage(MimeMessage mimeMessage) {

this.helper = new MimeMessageHelper(mimeMessage);

}

public final MimeMessageHelper getMimeMessageHelper() {

return this.helper;

}

public final MimeMessage getMimeMessage() {

return this.helper.getMimeMessage();

}

public void setFrom(String from) throws MailParseException {

try {

this.helper.setFrom(from);

} catch (MessagingException var3) {

throw new MailParseException(var3);

}

}

public void setReplyTo(String replyTo) throws MailParseException {

try {

this.helper.setReplyTo(replyTo);

} catch (MessagingException var3) {

throw new MailParseException(var3);

}

}

public void setTo(String to) throws MailParseException {

try {

this.helper.setTo(to);

} catch (MessagingException var3) {

throw new MailParseException(var3);

}

}

public void setTo(String... to) throws MailParseException {

try {

this.helper.setTo(to);

} catch (MessagingException var3) {

throw new MailParseException(var3);

}

}

public void setCc(String cc) throws MailParseException {

try {

this.helper.setCc(cc);

} catch (MessagingException var3) {

throw new MailParseException(var3);

}

}

public void setCc(String... cc) throws MailParseException {

try {

this.helper.setCc(cc);

} catch (MessagingException var3) {

throw new MailParseException(var3);

}

}

public void setBcc(String bcc) throws MailParseException {

try {

this.helper.setBcc(bcc);

} catch (MessagingException var3) {

throw new MailParseException(var3);

}

}

public void setBcc(String... bcc) throws MailParseException {

try {

this.helper.setBcc(bcc);

} catch (MessagingException var3) {

throw new MailParseException(var3);

}

}

public void setSentDate(Date sentDate) throws MailParseException {

try {

this.helper.setSentDate(sentDate);

} catch (MessagingException var3) {

throw new MailParseException(var3);

}

}

public void setSubject(String subject) throws MailParseException {

try {

this.helper.setSubject(subject);

} catch (MessagingException var3) {

throw new MailParseException(var3);

}

}

public void setText(String text) throws MailParseException {

try {

this.helper.setText(text);

} catch (MessagingException var3) {

throw new MailParseException(var3);

}

}

}

参数说明:

subject :邮件主题

content :邮件主题内容

from:发件人Email地址

to:收件人Email地址

MimeMessageHelper是处理JavaMail比较顺手的组件之一,可以让你摆脱繁复的JavaMail API

可用于发送附件和嵌入式资源(inline resources),允许添加附件和内嵌资源(inline resources)。

内嵌资源可能是你在信件中希望使用的图像或样式表,但是又不想把它们作为附件。

public class MimeMessageHelper {

public static final int MULTIPART_MODE_NO = 0;

public static final int MULTIPART_MODE_MIXED = 1;

public static final int MULTIPART_MODE_RELATED = 2;

public static final int MULTIPART_MODE_MIXED_RELATED = 3;

private static final String MULTIPART_SUBTYPE_MIXED = "mixed";

private static final String MULTIPART_SUBTYPE_RELATED = "related";

private static final String MULTIPART_SUBTYPE_ALTERNATIVE = "alternative";

private static final String CONTENT_TYPE_ALTERNATIVE = "text/alternative";

private static final String CONTENT_TYPE_HTML = "text/html";

private static final String CONTENT_TYPE_CHARSET_SUFFIX = ";charset=";

private static final String HEADER_PRIORITY = "X-Priority";

private static final String HEADER_CONTENT_ID = "Content-ID";

private final MimeMessage mimeMessage;

@Nullable

private MimeMultipart rootMimeMultipart;

@Nullable

private MimeMultipart mimeMultipart;

@Nullable

private final String encoding;

private FileTypeMap fileTypeMap;

private boolean validateAddresses;

public MimeMessageHelper(MimeMessage mimeMessage) {

this(mimeMessage, (String)null);

}

public MimeMessageHelper(MimeMessage mimeMessage, @Nullable String encoding) {

this.validateAddresses = false;

this.mimeMessage = mimeMessage;

this.encoding = encoding != null ? encoding : this.getDefaultEncoding(mimeMessage);

this.fileTypeMap = this.getDefaultFileTypeMap(mimeMessage);

}

public MimeMessageHelper(MimeMessage mimeMessage, boolean multipart) throws MessagingException {

this(mimeMessage, multipart, (String)null);

}

public MimeMessageHelper(MimeMessage mimeMessage, boolean multipart, @Nullable String encoding) throws MessagingException {

this(mimeMessage, multipart ? 3 : 0, encoding);

}

public MimeMessageHelper(MimeMessage mimeMessage, int multipartMode) throws MessagingException {

this(mimeMessage, multipartMode, (String)null);

}

public MimeMessageHelper(MimeMessage mimeMessage, int multipartMode, @Nullable String encoding) throws MessagingException {

this.validateAddresses = false;

this.mimeMessage = mimeMessage;

this.createMimeMultiparts(mimeMessage, multipartMode);

this.encoding = encoding != null ? encoding : this.getDefaultEncoding(mimeMessage);

this.fileTypeMap = this.getDefaultFileTypeMap(mimeMessage);

}

public final MimeMessage getMimeMessage() {

return this.mimeMessage;

}

protected void createMimeMultiparts(MimeMessage mimeMessage, int multipartMode) throws MessagingException {

switch(multipartMode) {

case 0:

this.setMimeMultiparts((MimeMultipart)null, (MimeMultipart)null);

break;

case 1:

MimeMultipart mixedMultipart = new MimeMultipart("mixed");

mimeMessage.setContent(mixedMultipart);

this.setMimeMultiparts(mixedMultipart, mixedMultipart);

break;

case 2:

MimeMultipart relatedMultipart = new MimeMultipart("related");

mimeMessage.setContent(relatedMultipart);

this.setMimeMultiparts(relatedMultipart, relatedMultipart);

break;

case 3:

MimeMultipart rootMixedMultipart = new MimeMultipart("mixed");

mimeMessage.setContent(rootMixedMultipart);

MimeMultipart nestedRelatedMultipart = new MimeMultipart("related");

MimeBodyPart relatedBodyPart = new MimeBodyPart();

relatedBodyPart.setContent(nestedRelatedMultipart);

rootMixedMultipart.addBodyPart(relatedBodyPart);

this.setMimeMultiparts(rootMixedMultipart, nestedRelatedMultipart);

break;

default:

throw new IllegalArgumentException("Only multipart modes MIXED_RELATED, RELATED and NO supported");

}

}

protected final void setMimeMultiparts(@Nullable MimeMultipart root, @Nullable MimeMultipart main) {

this.rootMimeMultipart = root;

this.mimeMultipart = main;

}

public final boolean isMultipart() {

return this.rootMimeMultipart != null;

}

public final MimeMultipart getRootMimeMultipart() throws IllegalStateException {

if (this.rootMimeMultipart == null) {

throw new IllegalStateException("Not in multipart mode - create an appropriate MimeMessageHelper via a constructor that takes a 'multipart' flag if you need to set alternative texts or add inline elements or attachments.");

} else {

return this.rootMimeMultipart;

}

}

public final MimeMultipart getMimeMultipart() throws IllegalStateException {

if (this.mimeMultipart == null) {

throw new IllegalStateException("Not in multipart mode - create an appropriate MimeMessageHelper via a constructor that takes a 'multipart' flag if you need to set alternative texts or add inline elements or attachments.");

} else {

return this.mimeMultipart;

}

}

@Nullable

protected String getDefaultEncoding(MimeMessage mimeMessage) {

return mimeMessage instanceof SmartMimeMessage ? ((SmartMimeMessage)mimeMessage).getDefaultEncoding() : null;

}

@Nullable

public String getEncoding() {

return this.encoding;

}

protected FileTypeMap getDefaultFileTypeMap(MimeMessage mimeMessage) {

if (mimeMessage instanceof SmartMimeMessage) {

FileTypeMap fileTypeMap = ((SmartMimeMessage)mimeMessage).getDefaultFileTypeMap();

if (fileTypeMap != null) {

return fileTypeMap;

}

}

ConfigurableMimeFileTypeMap fileTypeMap = new ConfigurableMimeFileTypeMap();

fileTypeMap.afterPropertiesSet();

return fileTypeMap;

}

public void setFileTypeMap(@Nullable FileTypeMap fileTypeMap) {

this.fileTypeMap = fileTypeMap != null ? fileTypeMap : this.getDefaultFileTypeMap(this.getMimeMessage());

}

public FileTypeMap getFileTypeMap() {

return this.fileTypeMap;

}

public void setValidateAddresses(boolean validateAddresses) {

this.validateAddresses = validateAddresses;

}

public boolean isValidateAddresses() {

return this.validateAddresses;

}

protected void validateAddress(InternetAddress address) throws AddressException {

if (this.isValidateAddresses()) {

address.validate();

}

}

protected void validateAddresses(InternetAddress[] addresses) throws AddressException {

InternetAddress[] var2 = addresses;

int var3 = addresses.length;

for(int var4 = 0; var4 < var3; ++var4) {

InternetAddress address = var2[var4];

this.validateAddress(address);

}

}

public void setFrom(InternetAddress from) throws MessagingException {

Assert.notNull(from, "From address must not be null");

this.validateAddress(from);

this.mimeMessage.setFrom(from);

}

public void setFrom(String from) throws MessagingException {

Assert.notNull(from, "From address must not be null");

this.setFrom(this.parseAddress(from));

}

public void setFrom(String from, String personal) throws MessagingException, UnsupportedEncodingException {

Assert.notNull(from, "From address must not be null");

this.setFrom(this.getEncoding() != null ? new InternetAddress(from, personal, this.getEncoding()) : new InternetAddress(from, personal));

}

public void setReplyTo(InternetAddress replyTo) throws MessagingException {

Assert.notNull(replyTo, "Reply-to address must not be null");

this.validateAddress(replyTo);

this.mimeMessage.setReplyTo(new InternetAddress[]{replyTo});

}

public void setReplyTo(String replyTo) throws MessagingException {

Assert.notNull(replyTo, "Reply-to address must not be null");

this.setReplyTo(this.parseAddress(replyTo));

}

public void setReplyTo(String replyTo, String personal) throws MessagingException, UnsupportedEncodingException {

Assert.notNull(replyTo, "Reply-to address must not be null");

InternetAddress replyToAddress = this.getEncoding() != null ? new InternetAddress(replyTo, personal, this.getEncoding()) : new InternetAddress(replyTo, personal);

this.setReplyTo(replyToAddress);

}

public void setTo(InternetAddress to) throws MessagingException {

Assert.notNull(to, "To address must not be null");

this.validateAddress(to);

this.mimeMessage.setRecipient(RecipientType.TO, to);

}

public void setTo(InternetAddress[] to) throws MessagingException {

Assert.notNull(to, "To address array must not be null");

this.validateAddresses(to);

this.mimeMessage.setRecipients(RecipientType.TO, to);

}

public void setTo(String to) throws MessagingException {

Assert.notNull(to, "To address must not be null");

this.setTo(this.parseAddress(to));

}

public void setTo(String[] to) throws MessagingException {

Assert.notNull(to, "To address array must not be null");

InternetAddress[] addresses = new InternetAddress[to.length];

for(int i = 0; i < to.length; ++i) {

addresses[i] = this.parseAddress(to[i]);

}

this.setTo(addresses);

}

public void addTo(InternetAddress to) throws MessagingException {

Assert.notNull(to, "To address must not be null");

this.validateAddress(to);

this.mimeMessage.addRecipient(RecipientType.TO, to);

}

public void addTo(String to) throws MessagingException {

Assert.notNull(to, "To address must not be null");

this.addTo(this.parseAddress(to));

}

public void addTo(String to, String personal) throws MessagingException, UnsupportedEncodingException {

Assert.notNull(to, "To address must not be null");

this.addTo(this.getEncoding() != null ? new InternetAddress(to, personal, this.getEncoding()) : new InternetAddress(to, personal));

}

public void setCc(InternetAddress cc) throws MessagingException {

Assert.notNull(cc, "Cc address must not be null");

this.validateAddress(cc);

this.mimeMessage.setRecipient(RecipientType.CC, cc);

}

public void setCc(InternetAddress[] cc) throws MessagingException {

Assert.notNull(cc, "Cc address array must not be null");

this.validateAddresses(cc);

this.mimeMessage.setRecipients(RecipientType.CC, cc);

}

public void setCc(String cc) throws MessagingException {

Assert.notNull(cc, "Cc address must not be null");

this.setCc(this.parseAddress(cc));

}

public void setCc(String[] cc) throws MessagingException {

Assert.notNull(cc, "Cc address array must not be null");

InternetAddress[] addresses = new InternetAddress[cc.length];

for(int i = 0; i < cc.length; ++i) {

addresses[i] = this.parseAddress(cc[i]);

}

this.setCc(addresses);

}

public void addCc(InternetAddress cc) throws MessagingException {

Assert.notNull(cc, "Cc address must not be null");

this.validateAddress(cc);

this.mimeMessage.addRecipient(RecipientType.CC, cc);

}

public void addCc(String cc) throws MessagingException {

Assert.notNull(cc, "Cc address must not be null");

this.addCc(this.parseAddress(cc));

}

public void addCc(String cc, String personal) throws MessagingException, UnsupportedEncodingException {

Assert.notNull(cc, "Cc address must not be null");

this.addCc(this.getEncoding() != null ? new InternetAddress(cc, personal, this.getEncoding()) : new InternetAddress(cc, personal));

}

public void setBcc(InternetAddress bcc) throws MessagingException {

Assert.notNull(bcc, "Bcc address must not be null");

this.validateAddress(bcc);

this.mimeMessage.setRecipient(RecipientType.BCC, bcc);

}

public void setBcc(InternetAddress[] bcc) throws MessagingException {

Assert.notNull(bcc, "Bcc address array must not be null");

this.validateAddresses(bcc);

this.mimeMessage.setRecipients(RecipientType.BCC, bcc);

}

public void setBcc(String bcc) throws MessagingException {

Assert.notNull(bcc, "Bcc address must not be null");

this.setBcc(this.parseAddress(bcc));

}

public void setBcc(String[] bcc) throws MessagingException {

Assert.notNull(bcc, "Bcc address array must not be null");

InternetAddress[] addresses = new InternetAddress[bcc.length];

for(int i = 0; i < bcc.length; ++i) {

addresses[i] = this.parseAddress(bcc[i]);

}

this.setBcc(addresses);

}

public void addBcc(InternetAddress bcc) throws MessagingException {

Assert.notNull(bcc, "Bcc address must not be null");

this.validateAddress(bcc);

this.mimeMessage.addRecipient(RecipientType.BCC, bcc);

}

public void addBcc(String bcc) throws MessagingException {

Assert.notNull(bcc, "Bcc address must not be null");

this.addBcc(this.parseAddress(bcc));

}

public void addBcc(String bcc, String personal) throws MessagingException, UnsupportedEncodingException {

Assert.notNull(bcc, "Bcc address must not be null");

this.addBcc(this.getEncoding() != null ? new InternetAddress(bcc, personal, this.getEncoding()) : new InternetAddress(bcc, personal));

}

private InternetAddress parseAddress(String address) throws MessagingException {

InternetAddress[] parsed = InternetAddress.parse(address);

if (parsed.length != 1) {

throw new AddressException("Illegal address", address);

} else {

InternetAddress raw = parsed[0];

try {

return this.getEncoding() != null ? new InternetAddress(raw.getAddress(), raw.getPersonal(), this.getEncoding()) : raw;

} catch (UnsupportedEncodingException var5) {

throw new MessagingException("Failed to parse embedded personal name to correct encoding", var5);

}

}

}

public void setPriority(int priority) throws MessagingException {

this.mimeMessage.setHeader("X-Priority", Integer.toString(priority));

}

public void setSentDate(Date sentDate) throws MessagingException {

Assert.notNull(sentDate, "Sent date must not be null");

this.mimeMessage.setSentDate(sentDate);

}

public void setSubject(String subject) throws MessagingException {

Assert.notNull(subject, "Subject must not be null");

if (this.getEncoding() != null) {

this.mimeMessage.setSubject(subject, this.getEncoding());

} else {

this.mimeMessage.setSubject(subject);

}

}

public void setText(String text) throws MessagingException {

this.setText(text, false);

}

public void setText(String text, boolean html) throws MessagingException {

Assert.notNull(text, "Text must not be null");

Object partToUse;

if (this.isMultipart()) {

partToUse = this.getMainPart();

} else {

partToUse = this.mimeMessage;

}

if (html) {

this.setHtmlTextToMimePart((MimePart)partToUse, text);

} else {

this.setPlainTextToMimePart((MimePart)partToUse, text);

}

}

public void setText(String plainText, String htmlText) throws MessagingException {

Assert.notNull(plainText, "Plain text must not be null");

Assert.notNull(htmlText, "HTML text must not be null");

MimeMultipart messageBody = new MimeMultipart("alternative");

this.getMainPart().setContent(messageBody, "text/alternative");

MimeBodyPart plainTextPart = new MimeBodyPart();

this.setPlainTextToMimePart(plainTextPart, plainText);

messageBody.addBodyPart(plainTextPart);

MimeBodyPart htmlTextPart = new MimeBodyPart();

this.setHtmlTextToMimePart(htmlTextPart, htmlText);

messageBody.addBodyPart(htmlTextPart);

}

private MimeBodyPart getMainPart() throws MessagingException {

MimeMultipart mimeMultipart = this.getMimeMultipart();

MimeBodyPart bodyPart = null;

for(int i = 0; i < mimeMultipart.getCount(); ++i) {

BodyPart bp = mimeMultipart.getBodyPart(i);

if (bp.getFileName() == null) {

bodyPart = (MimeBodyPart)bp;

}

}

if (bodyPart == null) {

MimeBodyPart mimeBodyPart = new MimeBodyPart();

mimeMultipart.addBodyPart(mimeBodyPart);

bodyPart = mimeBodyPart;

}

return bodyPart;

}

private void setPlainTextToMimePart(MimePart mimePart, String text) throws MessagingException {

if (this.getEncoding() != null) {

mimePart.setText(text, this.getEncoding());

} else {

mimePart.setText(text);

}

}

private void setHtmlTextToMimePart(MimePart mimePart, String text) throws MessagingException {

if (this.getEncoding() != null) {

mimePart.setContent(text, "text/html;charset=" + this.getEncoding());

} else {

mimePart.setContent(text, "text/html");

}

}

public void addInline(String contentId, DataSource dataSource) throws MessagingException {

Assert.notNull(contentId, "Content ID must not be null");

Assert.notNull(dataSource, "DataSource must not be null");

MimeBodyPart mimeBodyPart = new MimeBodyPart();

mimeBodyPart.setDisposition("inline");

mimeBodyPart.setHeader("Content-ID", "");

mimeBodyPart.setDataHandler(new DataHandler(dataSource));

this.getMimeMultipart().addBodyPart(mimeBodyPart);

}

public void addInline(String contentId, File file) throws MessagingException {

Assert.notNull(file, "File must not be null");

FileDataSource dataSource = new FileDataSource(file);

dataSource.setFileTypeMap(this.getFileTypeMap());

this.addInline(contentId, (DataSource)dataSource);

}

public void addInline(String contentId, Resource resource) throws MessagingException {

Assert.notNull(resource, "Resource must not be null");

String contentType = this.getFileTypeMap().getContentType(resource.getFilename());

this.addInline(contentId, resource, contentType);

}

public void addInline(String contentId, InputStreamSource inputStreamSource, String contentType) throws MessagingException {

Assert.notNull(inputStreamSource, "InputStreamSource must not be null");

if (inputStreamSource instanceof Resource && ((Resource)inputStreamSource).isOpen()) {

throw new IllegalArgumentException("Passed-in Resource contains an open stream: invalid argument. JavaMail requires an InputStreamSource that creates a fresh stream for every call.");

} else {

DataSource dataSource = this.createDataSource(inputStreamSource, contentType, "inline");

this.addInline(contentId, dataSource);

}

}

public void addAttachment(String attachmentFilename, DataSource dataSource) throws MessagingException {

Assert.notNull(attachmentFilename, "Attachment filename must not be null");

Assert.notNull(dataSource, "DataSource must not be null");

try {

MimeBodyPart mimeBodyPart = new MimeBodyPart();

mimeBodyPart.setDisposition("attachment");

mimeBodyPart.setFileName(MimeUtility.encodeText(attachmentFilename));

mimeBodyPart.setDataHandler(new DataHandler(dataSource));

this.getRootMimeMultipart().addBodyPart(mimeBodyPart);

} catch (UnsupportedEncodingException var4) {

throw new MessagingException("Failed to encode attachment filename", var4);

}

}

public void addAttachment(String attachmentFilename, File file) throws MessagingException {

Assert.notNull(file, "File must not be null");

FileDataSource dataSource = new FileDataSource(file);

dataSource.setFileTypeMap(this.getFileTypeMap());

this.addAttachment(attachmentFilename, (DataSource)dataSource);

}

public void addAttachment(String attachmentFilename, InputStreamSource inputStreamSource) throws MessagingException {

String contentType = this.getFileTypeMap().getContentType(attachmentFilename);

this.addAttachment(attachmentFilename, inputStreamSource, contentType);

}

public void addAttachment(String attachmentFilename, InputStreamSource inputStreamSource, String contentType) throws MessagingException {

Assert.notNull(inputStreamSource, "InputStreamSource must not be null");

if (inputStreamSource instanceof Resource && ((Resource)inputStreamSource).isOpen()) {

throw new IllegalArgumentException("Passed-in Resource contains an open stream: invalid argument. JavaMail requires an InputStreamSource that creates a fresh stream for every call.");

} else {

DataSource dataSource = this.createDataSource(inputStreamSource, contentType, attachmentFilename);

this.addAttachment(attachmentFilename, dataSource);

}

}

protected DataSource createDataSource(final InputStreamSource inputStreamSource, final String contentType, final String name) {

return new DataSource() {

public InputStream getInputStream() throws IOException {

return inputStreamSource.getInputStream();

}

public OutputStream getOutputStream() {

throw new UnsupportedOperationException("Read-only javax.activation.DataSource");

}

public String getContentType() {

return contentType;

}

public String getName() {

return name;

}

};

}

}

邮件发送接口主要使用JavaMailSender

JavaMailSender接口源码

public interface JavaMailSender extends MailSender {

MimeMessage createMimeMessage();

MimeMessage createMimeMessage(InputStream var1) throws MailException;

void send(MimeMessage var1) throws MailException;

void send(MimeMessage... var1) throws MailException;

void send(MimeMessagePreparator var1) throws MailException;

void send(MimeMessagePreparator... var1) throws MailException;

}

JavaMailSender实现类为JavaMailSenderImpl,主要看邮件发送方法

JavaMailSenderImpl核心源码

public class JavaMailSenderImpl implements JavaMailSender {

public void send(SimpleMailMessage simpleMessage) throws MailException {

this.send(simpleMessage);

}

public void send(SimpleMailMessage... simpleMessages) throws MailException {

List mimeMessages = new ArrayList(simpleMessages.length);

SimpleMailMessage[] var3 = simpleMessages;

int var4 = simpleMessages.length;

for(int var5 = 0; var5 < var4; ++var5) {

SimpleMailMessage simpleMessage = var3[var5];

MimeMailMessage message = new MimeMailMessage(this.createMimeMessage());

simpleMessage.copyTo(message);

mimeMessages.add(message.getMimeMessage());

}

this.doSend((MimeMessage[])mimeMessages.toArray(new MimeMessage[0]), simpleMessages);

}

public void send(MimeMessage mimeMessage) throws MailException {

this.send(mimeMessage);

}

public void send(MimeMessage... mimeMessages) throws MailException {

this.doSend(mimeMessages, (Object[])null);

}

public void send(MimeMessagePreparator mimeMessagePreparator) throws MailException {

this.send(mimeMessagePreparator);

}

public void send(MimeMessagePreparator... mimeMessagePreparators) throws MailException {

try {

List mimeMessages = new ArrayList(mimeMessagePreparators.length);

MimeMessagePreparator[] var3 = mimeMessagePreparators;

int var4 = mimeMessagePreparators.length;

for(int var5 = 0; var5 < var4; ++var5) {

MimeMessagePreparator preparator = var3[var5];

MimeMessage mimeMessage = this.createMimeMessage();

preparator.prepare(mimeMessage);

mimeMessages.add(mimeMessage);

}

this.send((MimeMessage[])mimeMessages.toArray(new MimeMessage[0]));

} catch (MailException var8) {

throw var8;

} catch (MessagingException var9) {

throw new MailParseException(var9);

} catch (Exception var10) {

throw new MailPreparationException(var10);

}

}

public MimeMessage createMimeMessage() {

return new SmartMimeMessage(this.getSession(), this.getDefaultEncoding(), this.getDefaultFileTypeMap());

}

public MimeMessage createMimeMessage(InputStream contentStream) throws MailException {

try {

return new MimeMessage(this.getSession(), contentStream);

} catch (Exception var3) {

throw new MailParseException("Could not parse raw MIME content", var3);

}

}

......

}

SpringBoot实现邮件发送

YML配置

spring:

mail:

host: smtp.qq.com

username: 发送者邮箱

password: 邮箱授权码

default-encoding: UTF-8

spring.mail.host为邮箱服务商的smtp地址

spring.mail.username:发送者邮箱

spring.mail.password邮箱服务商授权码

使用SimpleMailMessage,设置发件人邮箱,收件人邮箱,主题和正文。

1.普通文本邮件发送

@Service

public class MailService {

@Value("${spring.mail.username}")

private String from;

@Autowired

JavaMailSender javaMailSender;

public void sendSimpleMail(String to,String subject,String content){

SimpleMailMessage simpleMailMessage=new SimpleMailMessage();

simpleMailMessage.setFrom(from);

simpleMailMessage.setTo(to);

simpleMailMessage.setSubject(subject);

simpleMailMessage.setText(content);

javaMailSender.send(simpleMailMessage);

}

}

单元测试

@Autowired

MailService mailService;

@Test

public void test() throws MessagingException {

mailService.sendSimpleMail("邮箱","主题","测试内容");

}

测试结果

f4ad23d43a03

image.png

MimeMessages为复杂邮件模板,支持文本、附件、HTML、图片等。

2.发送HTML邮件

创建MimeMessageHelper对象,处理MimeMessage的辅助类。

@Service

public class MailService {

@Value("${spring.mail.username}")

private String from;

@Autowired

JavaMailSender javaMailSender;

public void sendHtmlMail(String to,String subject,String content) throws MessagingException {

MimeMessage mimeMessage=javaMailSender.createMimeMessage();

MimeMessageHelper mimeMessageHelper=new MimeMessageHelper(mimeMessage,true);

mimeMessageHelper.setFrom(from);

mimeMessageHelper.setTo(to);

mimeMessageHelper.setSubject(subject);

mimeMessageHelper.setText(content,true);

javaMailSender.send(mimeMessage);

}

}

这里要注意的是发送HTML内容时需要设置html开启,默认为false

public void setText(String text, boolean html) throws MessagingException {

Assert.notNull(text, "Text must not be null");

Object partToUse;

if (this.isMultipart()) {

partToUse = this.getMainPart();

} else {

partToUse = this.mimeMessage;

}

if (html) {

this.setHtmlTextToMimePart((MimePart)partToUse, text);

} else {

this.setPlainTextToMimePart((MimePart)partToUse, text);

}

}

单元测试

@Autowired

MailService mailService;

@Test

public void test() throws MessagingException {

String content="\n"+

"

\n"+

"

hello world

\n"+

"\n"+

"";

mailService.sendHtmlMail("邮箱","主题",content);

}

测试结果

f4ad23d43a03

image.png

3.发送附件邮件

@Service

public class MailService {

@Value("${spring.mail.username}")

private String from;

@Autowired

JavaMailSender javaMailSender;

public void sendAttachmentMail(String to,String subject,String content,String filePath) throws MessagingException {

MimeMessage mimeMessage=javaMailSender.createMimeMessage();

MimeMessageHelper mimeMessageHelper=new MimeMessageHelper(mimeMessage,true);

mimeMessageHelper.setFrom(from);

mimeMessageHelper.setTo(to);

mimeMessageHelper.setSubject(subject);

mimeMessageHelper.setText(content,true);

FileSystemResource fileSystemResource=new FileSystemResource(new File(filePath));

String fileName=fileSystemResource.getFilename();

mimeMessageHelper.addAttachment(fileName,fileSystemResource);

javaMailSender.send(mimeMessage);

}

}

需要开启邮件附件

public MimeMessageHelper(MimeMessage mimeMessage, boolean multipart) throws MessagingException {

this(mimeMessage, multipart, (String)null);

}

addAttachment()方法是添加附件

public void addAttachment(String attachmentFilename, InputStreamSource inputStreamSource) throws MessagingException {

String contentType = this.getFileTypeMap().getContentType(attachmentFilename);

this.addAttachment(attachmentFilename, inputStreamSource, contentType);

}

单元测试

@Autowired

MailService mailService;

@Test

public void test() throws MessagingException {

mailService.sendAttachmentMail("邮箱","主题","测试内容","E:\\文件名");

}

测试结果

f4ad23d43a03

image.png

java发送s mime邮件_SpringBoot集成实现各种邮件发送相关推荐

  1. java token生成和验证_SpringBoot集成JWT生成token及校验方法过程解析

    GitHub源码地址:https://github.com/zeng-xian-guo/springboot_jwt_token.git 封装JTW生成token和校验方法 public class ...

  2. 邮件发送类_10 分钟实现 Spring Boot 发生邮件功能

    基础知识 什么是SMTP? 什么是IMAP? 什么是POP3? IMAP和POP3协议有什么不同呢? 进阶知识 什么是JavaMailSender和JavaMailSenderImpl? 如何通过Ja ...

  3. Java邮件服务学习之一:邮件服务概述

    java可以提供邮件服务:一般理解的邮件服务就是可以发送和接收邮件的客户端,另外就是使用java编写邮件服务端:两者区别在于客户端只负责给终端客户收发邮件,就相当于小区楼下的那一排排的铁皮邮箱盒,而邮 ...

  4. 持续集成:Jenkins邮件通知配置方法介绍

    Jenkins的邮件提醒功能主要通过Email Extension插件来实现,它是对Mailer Plugin的扩展,我在持续集成平台Jenkins配置方法介绍中简要介绍了Jenkins的邮件配置方法 ...

  5. Spring boot锦集(二):整合邮件发送的四种方法 | 纯文本的邮件、带有图片的邮件、带Html的邮件、带附件的邮件(很详细)

    前言 邮件发送,听着很神秘,然而对于Spring Boot来说,这个功能已被集成好,只需引入spring-boot-starter-mail依赖后,少量代码即可实现大部分邮件发送需求. 本文以异常教程 ...

  6. python搭建邮件服务器地址_python 配置邮件发送服务器发送邮件

    邮件发送脚本 #coding:utf-8 # #!/usr/bin/python import smtplib ,os from email.mime.text import MIMEText fro ...

  7. python发邮件图片太长显示不出来_小白入门,用python 发送定时邮件,将Dataframe转为邮件正文,链接显示为图片...

    在实际工作中,我们常常会遇到定时发送邮件的任务,基于我的实践,分享给大家,也许一篇文章写不完,就先列个目录. 本文想要解决的问题: 用python构造一封邮件,并设置定时发送出去.往往,这只是最低级的 ...

  8. python练习_邮件定时收取处理附件后发送结果

    """第一步定时执行 ok 第二步收取邮件 ok 第三步数据转换 ok 第四步邮件派发 ok""" -- coding: utf-8 -- ...

  9. PHP邮件功能无法完成电子邮件的发送

    本文翻译自:PHP mail function doesn't complete sending of e-mail <?php$name = $_POST['name'];$email = $ ...

最新文章

  1. 无监督学习之RBM和AutoEncoder
  2. 软件设计师备考知识05--设计模式
  3. Oracle 内存参数设置
  4. 5-struts2知识补充( 常用的struts2的标签,数据回显,防止重复提交)
  5. 解决python中join路径分隔符跨平台移植性
  6. C++头插法尾插法建立单链表,合并两个有序单链表
  7. Vue 模块化开发(构建项目常用工具)
  8. 苦等8个月!华为最令人期待的手机终于要来了:最快月底开卖
  9. GBK转unicode码查询表的改进
  10. ThinkPHP5最新URL访问:PATH_INFO和兼容模式
  11. 多维数组的索引与切片_SystemVerilog的那些数组
  12. Python安装库Could not find a version that satisfies the requirement requests (from versions: none)
  13. 我的世界正版租赁服务器,《我的世界》【PC版】租赁服务器正式版明日就要和大家见面啦~...
  14. 火线精英正在维护服务器吗,火线精英1月20日23:00更新维护公告
  15. dw怎么保存html格式,教你如何用Dreamweaver制作网页以及保存网页的方法--系统之家...
  16. 【智能优化算法-白鲸优化算法】基于白鲸优化算法求解单目标优化问题附matlab代码
  17. 结构体类型(struct)
  18. iOS真机测试详细步骤及图解
  19. RHCE认证考试成绩公布(转)
  20. 2020年日历_2020年日历表打印版下载|2020年日历表 打印版 下载 - 巴士下载站

热门文章

  1. 打车日记 - 谨慎的小哥哥
  2. amazon_亚马逊甚至不再那么方便
  3. PCB设计中屏蔽罩夹子的使用
  4. 匹配追踪和正交匹配追踪
  5. 芯片上电休眠或者JTAG禁用后怎么下载程序
  6. 文件搜索神器everything 你不知道的技巧总结
  7. 【二十六】springboot实现多线程事务处理
  8. matlab数字通信系统的仿真实验报告,数字通信系统的matlab仿真
  9. org.springframework.data.redis.serializer.SerializationException: Cannot serialize; nested exception
  10. 德国光量子计算机,新型量子光源:为光学量子计算机铺路!