如何通过javamail来发送邮件(SSL加密)??这里做一个小测试
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class JavaSendEmail {
public static void main(String[] args) throws MessagingException {
Properties properties = new Properties();
properties.put("mail.transport.protocol","smtp");//协议
properties.put("mail.smtp.host","IP地址");
properties.put("mail.smtp.port",465);//端口号
properties.put("mail.smtp.auth","true");
properties.put("mail.smtp.ssl.enable","true");//设置ssl安全连接
properties.put("mail.debug","true");//显示debug信息
//会话对象
Session session = Session.getInstance(properties);
//获取邮件对象
Message message = new MimeMessage(session);
//设置发件人邮箱地址
message.setFrom(new InternetAddress("kaibo_cg@kaibo.com"));
//设置收件人邮箱地址
message.setRecipients(Message.RecipientType.TO,
new InternetAddress[]{
new InternetAddress("收件人的邮箱@"),
});
//设置邮件标题
message.setSubject("发给我自己");
//设置邮件内容
message.setText("testtesttesttesttest");
//得到邮箱对象
Transport transport = session.getTransport();
transport.connect("账号@","密码");
//发送邮件
transport.sendMessage(message,message.getAllRecipients());
//关闭邮件对象
transport.close();
}
}