Email就是电子邮件。电子邮件的应用已经有几十年的历史了,我们熟悉的邮箱地址比如aaaa22222@163.com,邮件软件比如Outlook、网易闪电邮、Foxmail都是用来收发邮件的。当然,使用Java程序也可以收发电子邮件。
传统的邮件就是通过邮局投递,然后从一个邮局到另一个邮局,最终到达用户的邮箱。电子邮件的发送也是类似的,
java Email首先需要导一个包:javax.mail-1.6.2.jar
yan
可以自己创建一个工具类,便于每次的使用,预防每一次都需要重复很多东西!类似于这样:
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
//工具类
public class JavaMailUtils {
private JavaMailUtils() {
}
public static Session createSession() {
//邮箱账号信息
String userName = "19584XXX@qq.com";//邮件发送账号
String password = "cyhajwlbkXXxxX";//账号授权密码
//SMTP服务连接器连接信息
Properties props = new Properties();
props.put("mail.smtp.host","smtp.qq.com");//SMTP主机名
props.put("mail.smtp.post","25");//主机端口号
props.put("mail.smtp.auth","true");//是否需要用户认证
props.put("mail.smtp.starttls.enable","true");//启用TLS加密
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
return session;
}
}
提示:密码必须是授权密码。。。授权密码可以在邮件设置中找到并打开!
接下来演示一则最基本的文字邮件发送。
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
public class Demo06 {
public static void main(String[] args) {
try {
//1.创建Session会话
Session session = JavaMailUtils.createSession();
//创建邮箱对象
MimeMessage message = new MimeMessage(session);
message.setSubject("聘请函!");
message.setText("恭喜您被猿究院录取,工资详谈,手机号:13347xxxxx`4");
message.setFrom(new InternetAddress("1808xxx2494@163.com"));
message.setRecipient(RecipientType.TO, new InternetAddress("2232266959@qq.com"));
Transport.send(message);
System.out.println("1");
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
以及邮件加入附件照片的发送!!!
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.activation.DataHandler;
import javax.mail.BodyPart;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
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;
import javax.mail.util.ByteArrayDataSource;
import org.apache.xmlbeans.impl.store.Path;
public class Demo07 {
public static void main(String[] args) {
//1.创建Session会话
try {
Session session = JavaMailUtils.createSession();
//2.创建MimeMessage邮件对象
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("1958411xxx@qq.com"));//发件人
message.setRecipient(RecipientType.TO, new InternetAddress("7755xxxxx6@qq.com"));//收件人
message.setRecipients(RecipientType.CC, new InternetAddress[] {new InternetAddress("7755xxxxx6@qq.com")});
message.setSubject("测试邮件");
//邮箱仅包含正文
//message.setText("sssss");
//邮件包含正文也包含附件
//正文
BodyPart textPart = new MimeBodyPart();
//textPart.setContent("太阳<p>晒屁屁<p>了","text/html;harset=utf-8");
StringBuilder contentText = new StringBuilder();
contentText.append("<h3>触不可及<h3>");
contentText.append("<p>程47与七个小矮人的故事<p>");
contentText.append("<img scr=\"cid:cbkj\"/>");
textPart.setContent(contentText.toString(),"text/html;charset=utf-8");
//附件
BodyPart imagePart = new MimeBodyPart();
//filePart.setFileName("李云龙阁下");
//上传附件
imagePart.setDataHandler(
new DataHandler(
new ByteArrayDataSource(
Files.readAllBytes(Paths.get("E:\\test\\test\\DESKTOP-ILBR8QI.jpg")),
"application/octet-stream")));
imagePart.setHeader("Content-ID","cbkj");//图片的内容id
//将正文+附件组装成Multipart对象
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(textPart);
multipart.addBodyPart(imagePart);
//将Multipart对象放入邮件
message.setContent(multipart);
//3.发送邮件
Transport.send(message);
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
其中发送内容中可以包含超文本,但必须要在代码中说明文字中含有超文本,不然无法识别!
同时我们可以在控制台看到JavaMail打印的调试信息,在这里我就不演示了。。。
如果有收获,记得一键三连支持二锅头哦!!