一、 开启SMTP服务
1.基本都在邮箱设置里,开启后会获得神秘代码,后面有用。
2.记得添加依赖,或者自己添加jar包。
<dependency><groupId>javax.mail</groupId><artifactId>mail</artifactId><version>1.5.0-b01</version>
</dependency>
二、发送新浪邮箱带附件
public static void sinaMail() throws GeneralSecurityException {// 收件人电子邮箱String to = "XXXXXXXXX@qq.com"; //也可以的// 发件人电子邮箱String from = "XXXXXXXXX@sina.com";// 获取系统属性Properties properties = new Properties();//发送邮件协议properties.setProperty("mail.transport.protocol", "SMTP");// 设置邮件服务器properties.setProperty("mail.smtp.host", "smtp.sina.com");// 设置邮件服务器是否需要登录认证properties.setProperty("mail.smtp.auth", "true");// 验证账号及密码,密码需要是第三方授权码Authenticator auth = new Authenticator() {public PasswordAuthentication getPasswordAuthentication(){return new PasswordAuthentication("XXXXXXXXX@sina.com", "ed0dc82fcd7cea3f");}};// 获取默认session对象Session session = Session.getInstance(properties,auth);try{// 创建默认的 MimeMessage 对象MimeMessage message = new MimeMessage(session);// Set From: 头部头字段message.setFrom(new InternetAddress(from));// Set To: 邮件接收人message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));// Set Subject: 主题名称message.setSubject("This is the Subject Line!");// 创建消息部分BodyPart messageBodyPart = new MimeBodyPart();// 消息内容messageBodyPart.setText("TEST/TEST");// 创建多重消息Multipart multipart = new MimeMultipart();// 设置文本消息部分multipart.addBodyPart(messageBodyPart);// 附件部分messageBodyPart = new MimeBodyPart();//把文件,添加到附件1中//数据源DataSource source = new FileDataSource(new File("D:/1.pptx"));//设置第一个附件的数据messageBodyPart.setDataHandler(new DataHandler(source));//设置附件的文件名messageBodyPart.setFileName("1.pptx");multipart.addBodyPart(messageBodyPart);message.setContent(multipart);// 发送消息Transport.send(message);System.out.println("Sent message successfully....");}catch (MessagingException mex) {mex.printStackTrace();}
}
三、发送QQ邮箱带附件
public static void qqMail() throws GeneralSecurityException {// 收件人电子邮箱String to = "XXXXXXXXX@qq.com"; //也可以的// 发件人电子邮箱String from = "XXXXXXXXX@qq.com";// 获取系统属性Properties properties = new Properties();//发送邮件协议properties.setProperty("mail.transport.protocol", "SMTP");// 设置邮件服务器properties.setProperty("mail.host", "smtp.qq.com");// 设置邮件服务器端口properties.setProperty("mail.smtp.socketFactory.port", "587");//如果是阿里云服务器properties.put("mail.smtp.port", "587");// 设置邮件服务器是否需要登录认证properties.setProperty("mail.smtp.auth", "true");// 验证账号及密码,密码需要是第三方授权码Authenticator auth = new Authenticator() {public PasswordAuthentication getPasswordAuthentication(){return new PasswordAuthentication("XXXXXXXXX@qq.com", "urzgdvdyflesbjhh");}};// 获取默认session对象Session session = Session.getInstance(properties,auth);try{// 创建默认的 MimeMessage 对象MimeMessage message = new MimeMessage(session);// Set From: 头部头字段message.setFrom(new InternetAddress(from));// Set To: 邮件接收人message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));// Set Subject: 主题名称message.setSubject("This is the Subject Line!");// 创建消息部分BodyPart messageBodyPart = new MimeBodyPart();// 消息内容messageBodyPart.setText("TEST/TEST");// 创建多重消息Multipart multipart = new MimeMultipart();// 设置文本消息部分multipart.addBodyPart(messageBodyPart);// 附件部分messageBodyPart = new MimeBodyPart();//把文件,添加到附件1中//数据源DataSource source = new FileDataSource(new File("D:/1.pptx"));//设置第一个附件的数据messageBodyPart.setDataHandler(new DataHandler(source));//设置附件的文件名messageBodyPart.setFileName("1.pptx");multipart.addBodyPart(messageBodyPart);message.setContent(multipart);// 发送消息Transport.send(message);System.out.println("Sent message successfully....");}catch (MessagingException mex) {mex.printStackTrace();}
}
四、效果
五、补充
1.邮件内容换行
// 设置文本消息部分
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart,"text/html;charset=UTF-8");
用下面这个换行
<br>