引入bean
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.163.com"/>
<property name="port" value="465"/>
<property name="defaultEncoding" value="utf-8"/>
<property name="username" value="mail@163.com"/>
<property name="password" value="your password"/>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.sendpartial">true</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.socketFactory.port">465</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
</props>
</property>
</bean>
java实现
import java.io.File;
import java.util.List;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
public class MailUtil {
private static final LoggerLOGGER = LoggerFactory.getLogger(MailUtil.class);
@Autowired
JavaMailSenderImpl mailSender;
public void sendSimpleMail(String title, String content, String address) {
SimpleMailMessage mail =new SimpleMailMessage();
mail.setFrom(mailSender.getUsername());
mail.setTo(address);
mail.setSubject(title);
mail.setText(content);
mailSender.send(mail);
}
public StringsendHtmlMailWithVelocity(final String title, final String content, final String[] receiver, final String[] ccReceiver, final List picList) {
MimeMessagePreparator messagePreparator =new MimeMessagePreparator() {
@Override
public void prepare(MimeMessage mimeMessage)throws Exception {
MimeMessageHelper message =new MimeMessageHelper(mimeMessage, true, "UTF-8");
message.setFrom(new InternetAddress(mailSender.getUsername(), "显示别名", "UTF-8"));
message.setTo(receiver);
message.setCc(ccReceiver);
}
message.setSubject(title);
message.setText(content, true);
for (int i =0; i
//从本地文件夹中获取所需图片
FileSystemResource res =new FileSystemResource(new File(picList.get(i)));
message.addInline(String.valueOf(i), res);
}
}
};
try {
mailSender.send(messagePreparator);
}catch (Exception e) {
return e.getMessage();
}
return "success";
}
public StringsendHtmlMail(final String title, final String content, final String [] receiver, final String[] ccReceiver) {
MimeMessagePreparator messagePreparator =new MimeMessagePreparator() {
@Override
public void prepare(MimeMessage mimeMessage)throws Exception {
MimeMessageHelper message =new MimeMessageHelper(mimeMessage, true, "UTF-8");
message.setFrom(new InternetAddress(mailSender.getUsername(), "显示别名", "UTF-8"));
message.setTo(receiver);
message.setCc(ccReceiver);
}
message.setSubject(title);
message.setText(content, true);
}
};
try {
mailSender.send(messagePreparator);
}catch (Exception e) {
return e.getMessage();
}
return "success";
}
}