package top.imooc.mail;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import top.imooc.message.AttachmentMessage;
import top.imooc.message.HtmlMessage;
public class MailClientSend {
private Session session;
private Transport transport;
private String username="[email protected]";
private String password="xiojdebh";这个密码不是QQ密码或者邮箱密码,要验证的。自己百度
private String smtpServer="smtp.qq.com";
public void init() throws NoSuchProviderException {
//设置属性
Properties props=new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.class", "com.sun.mail.stmp,SMTPtransport");
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.host", smtpServer);
final String smtpPort = "465";
props.put("mail.smtp.port", smtpPort);
props.put("mail.smtp.auth", "true");//SMTP服务器需要核实身份
//创建session对象
session=Session.getDefaultInstance(props,new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
session.setDebug(true); //输出跟踪日志
//创建transport对象
transport=session.getTransport();
}
public void sendMessage() throws MessagingException, FileNotFoundException, IOException {
//Message msg=TextMessage.generate();
//打印Html邮件
//Message msg=HtmlMessage.generate();
Message msg=AttachmentMessage.generate();
transport.connect();
transport.sendMessage(msg, msg.getAllRecipients());
System.out.println("打印邮件成功发送");
}
public void close() throws MessagingException {
transport.close();
}
public static void main(String[] args) throws Exception {
MailClientSend send=new MailClientSend();
send.init();
send.sendMessage();
send.close();
}
}
package top.imooc.message;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class AttachmentMessage {
public static MimeMessage generate() throws AddressException, MessagingException, FileNotFoundException, IOException {
String from="[email protected]";//发件人邮箱地址
String to="[email protected]"; //收件人邮箱地址
String subject="多附件邮件";
String body=""
+"欢迎大家访问我们家的网站
";
//创建Session实例对象
Session session=Session.getInstance(new Properties());
//MimeMessage对象
MimeMessage message=new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject(subject);
message.setContent(body, "text/html;charset=gb2312");
//创建各个代表邮件附件的正文和附件MimeBodyPart对象
MimeBodyPart contentPart=createContent(body);
MimeBodyPart attachPart1=createAttachment("E:\\SSMProject\\20190423151417.jpg");
MimeBodyPart attachPart2=createAttachment("E:\\SSMProject\\redis.ppt");
//创建用于组合邮件正文和附件的MimeMu
MimeMultipart allMultipart=new MimeMultipart("mixed");
allMultipart.addBodyPart(contentPart);
allMultipart.addBodyPart(attachPart1);
allMultipart.addBodyPart(attachPart2);
//设置整个邮件最终组合为allMultipart
message.setContent(allMultipart);
message.writeTo(new FileOutputStream("E:\\SSMProject\\attachment.docx"));
message.saveChanges();
return message;
}
public static MimeBodyPart createContent(String body) throws MessagingException {
MimeBodyPart htmlBodyPart=new MimeBodyPart();
htmlBodyPart.setContent(body, "text/html;charset=gb2312");
return htmlBodyPart;
}
public static MimeBodyPart createAttachment(String filename) throws MessagingException {
//创建并保存福建的MimeBodyPart对象,并加入附件内容和相应的信息
MimeBodyPart attachPart=new MimeBodyPart();
FileDataSource fds=new FileDataSource(filename);
attachPart.setDataHandler(new DataHandler(fds));
attachPart.setFileName(fds.getName());
return attachPart;
}
}
package top.imooc.message;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class HtmlMessage {
public static MimeMessage generate() throws AddressException, MessagingException {
String from="[email protected]";//发件人邮箱地址
String to="[email protected]"; //收件人邮箱地址
String subject="Html邮件";
String body=""
+"欢迎大家访问我们家的网站
";
//创建Session实例对象
Session session=Session.getInstance(new Properties());
//创建MimeMessage对象
MimeMessage message=new MimeMessage(session);
//设置发件人
message.setFrom(new InternetAddress(from));
//设置收件人
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
//设置主题
message.setSubject(subject);
//设置发送日期
message.setSentDate(new Date());
//设置邮件正文内容
message.setContent(body, "text/html;charset=gb2312");
message.saveChanges();
return message;
}
}
package top.imooc.message;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class TextMessage {
public static MimeMessage generate() throws AddressException, MessagingException, FileNotFoundException, IOException {
String from="[email protected]";//发件人邮箱地址
String to="[email protected]"; //收件人邮箱地址
String subject="TextMessage";//邮件主题
String body="您好这是一封测试邮件";
//创建session对象
Session session=Session.getInstance(new Properties());
//创建MimeMessage
MimeMessage message=new MimeMessage(session);
//设置发信人
message.setFrom(new InternetAddress(from));
//设置收信人
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
//设置发送日期
message.setSentDate(new Date());
//设置邮件主题
message.setSubject(subject);
//设置邮件正文
message.setText(body);
//保存信息并生成邮件
message.saveChanges();
//可以把邮件也到本地文件中
message.writeTo(new FileOutputStream("E:\\SSMProject\\message.docx"));
return message;
}
}