package cn.itcast.sender;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
public class MimeMessageHelperTest {
public static void sendMail() throws MessagingException
{
//这种方式还是不能发送给qq邮箱
//这是一个发送邮件的类
JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
//这是设置发送邮件的服务器,一旦指定,那么下面设置的发送者就必须是这种类型的邮箱,像这里是163邮箱服务器,那么下面的
//发送者就必须要是163邮箱
senderImpl.setHost("smtp.163.com");
//这是设置邮箱的验证
Properties properties = new Properties();
properties.setProperty("mail.smtp.auth", "true");
senderImpl.setJavaMailProperties(properties);
//这是邮箱的用户名
senderImpl.setUsername("ch469299503");
//这是邮箱密码
senderImpl.setPassword("CH13410084766");
//这是通过senderImp得到邮箱的信息
MimeMessage mimeMessage = senderImpl.createMimeMessage();
//这是通过封装mimeMessage
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
//这是设置邮件的主题
helper.setSubject("这就是爱");
//这是设置邮件的发送者
helper.setFrom(new InternetAddress("ch469299503@163.com"));
//这是设置邮件的接受者ch900915caohuan@163.com
helper.setTo(new InternetAddress("469299503@qq.com"));
//这是邮箱的内容
helper.setText("这就是内容");
//这是发送邮件
senderImpl.send(mimeMessage);
}
public static void main(String[] args) {
try {
sendMail();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}