host?和port?获取
微软Outlook邮箱
- POP3: outlook.office365.com,使用TLS,端口号995
- IMAP: outlook.office365.com,使用TLS,端口号993
- SMTP: smtp.office365.com ,使用 STARTTLS,端口号587
- 账户名:您的outlook邮箱账户名
- 密码:您的outlook邮箱密码
- 电子邮件地址:邮箱的完整邮件地址
public static void mailProperties(String from, String to, String subject, String msgBody) {
Properties properties = System.getProperties(); //获取系统属性,主要用于设置邮件相关的参数。
//设置邮件传输服务器,由于本次是发送邮件操作,所需我们需要配置smtp协议,按outlook官方同步邮件的要求,依次配置协议地址,端口号和加密方法
properties.setProperty("mail.smtp.host", host);//发件人邮箱服务地址
properties.setProperty("mail.smtp.port", portData);//ssl端口
properties.setProperty("mail.debug", "true"); // 开启debug调试
properties.setProperty("mail.transport.protocol", "smtp");//使用协议
properties.setProperty("mail.smtp.starttls.enable", "true");// outlook邮箱需要加上
properties.setProperty("mail.smtp.auth", "true");//用户验证并返回Session,开启用户验证,设置发送邮箱的账号密码。
// properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.smtp.socketFactory.fallback", "false");
properties.setProperty("mail.smtp.socketFactory.port", portData);
// properties.setProperty("mail.smtp.user", name);
// properties.setProperty("mail.smtp.password", pwd);
MailAuthenticator mailAuthenticator = new MailAuthenticator(name, pwd);
// Session session1 = Session.getDefaultInstance(properties);//是获得一个默认的共享session
Session session1 = Session.getInstance(properties, mailAuthenticator); //1、创建session
session1.setDebug(true);
try {
MimeMessage message = new MimeMessage(session1); //4、创建邮件
message.addHeader("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");
message.setFrom(new InternetAddress(from)); //发件人
try {
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to, "你好,收到一封邮件", "UTF-8"));//收件人//MimeMessage.RecipientType.TO:发送、CC:抄送BCC:密送
// message.addRecipients(Message.RecipientType.TO, to);//收件人
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
message.setSubject(subject, "UTF-8");
message.setContent(msgBody, "text/html;charset=UTF-8");
message.setSentDate(new Date());
message.setText("测试邮件发送成功");
message.saveChanges();
// Transport transport = session1.getTransport(); //2、通过session得到transport对象
Transport transport = session1.getTransport("smtp");
// transport.connect(name,pwd);
transport.connect();//3、使用邮箱的用户名和密码连上邮件服务器
transport.sendMessage(message, message.getAllRecipients());//5、发送邮件
transport.close();
System.out.println("发送成功!");
// Multipart multipart = new MimeMultipart();//添加附件
// BodyPart filePart = new MimeBodyPart();
// DataSource source= new FileDataSource(File_Path);//附件存放路径
// filePart.setDataHandler(new DataHandler(source));
// filePart.setFileName(source.getName());
// multipart.addBodyPart(filePart);//3.添加文本内容
// MimeBodyPart textPart = new MimeBodyPart();
// textPart.setText("测试邮件发送成功");
// multipart.addBodyPart(textPart);//4.绑定消息对象
// message.setContent(multipart);//5.发送邮件
// Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
public static class MailAuthenticator extends Authenticator {
private String strUser;
private String strPwd;
public MailAuthenticator() {
super();
}
@Override
public PasswordAuthentication getPasswordAuthentication() {
String username = this.strUser;
String password = this.strPwd;
if ((username != null) && (username.length() > 0) && (password != null) && (password.length() > 0)) {
return new PasswordAuthentication(username, password);
}
return null;
}
public MailAuthenticator(String user, String password) {
this.strUser = user;
this.strPwd = password;
}
}
运行:
new MailUtil().mailProperties(from, from, "Subject", "Message");
|