linux使用mail以及使用java发送邮件
目录
1.安装
2.配置
3.使用
4.使用java发送邮件
1.安装
yum -y install mailx sendmail
2.配置
#未加密的发送方式通过25端口,会被公有云封掉.
cat >>/etc/mail.rc <<EOF
set from=xxx@163.com
set smtp=smtp.163.com
set smtp-auth-user=xxx@163.com
set smtp-auth-password=授权码
set smtp-auth=login
EOF#加密的方式465端口
cat >>/etc/mail.rc <<EOF
set nss-config-dir=/etc/pki/nssdb/ #加密方式配置
set smtp-user-starttls #加密方式配置
set ssl-verify=ignore #加密方式配置
set from=xxx@163.com #配置发件人
set smtp=smtps://smtp.163.com:465 #配置使用qq邮箱发送邮件,不加密方式参考上面
set smtp-auth-user=xxx@163.com #邮箱名
set smtp-auth-password=授权码 #授权码
set smtp-auth=login #认证形式
EOF
3.使用
mail -s xxx@163.com yyy@163.com </etc/hosts
xxx@163.com是发送方
yyy@163.com是接收方
4.使用java发送邮件
package pers.wwz.oom.oomone.utils;import javax.activation.DataHandler;
import javax.mail.*;
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 java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;/* 发送邮件* 需要依赖* <dependency>* <groupId>com.sun.mail</groupId>* <artifactId>javax.mail</artifactId>* <version>1.6.2</version>* </dependency>*/
public class MailUtils {/* 发件人*/public static final String SEND_FROM="xxx@163.com";/* 收件人*/public static final String SEND_TO="yyy@163.com";/* 抄送人*/public static final String SEND_CC="zzz@163.com";/* 邮件用户名*/public static final String MAIL_USERNAME="xxx@163.com";/* 邮件授权码(不是登录密码)*/public static final String MAIL_AUTH_PASSWORD="authPassword";public static void main(String[] args) throws MessagingException, IOException {sendSimpleMail();sendHtmlMail();sendFileMail();sendContentTagMail();}/* 创建session* @return session*/private static Session createSession() {// SMTP服务器地址String smtp = "smtp.163.com";// 邮箱账号和密码(授权密码)String userName = MAIL_USERNAME;String password = MAIL_AUTH_PASSWORD;// SMTP服务器的连接信息Properties props = new Properties();props.put("mail.smtp.host", smtp); // SMTP主机号props.put("mail.smtp.port", "25"); // 主机端口号props.put("mail.smtp.auth", "true"); // 是否需要认证props.put("mail.smtp.starttls.enable", "true"); // 启用TLS加密// 创建Session// 参数1:SMTP服务器的连接信息// 参数2:用户认证对象(Authenticator接口的匿名实现类)Session session = Session.getInstance(props, new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(userName, password);}});// 开启调试模式session.setDebug(true);return session;}/* 发送邮件* @throws MessagingException*/private static void sendSimpleMail() throws MessagingException {// 1.创建sessionSession session = createSession();// 2.创建邮件对象(Message抽象类的子类对象)MimeMessage msg = new MimeMessage(session); // 传入sessionmsg.setFrom(new InternetAddress(SEND_FROM)); // 发件人msg.setRecipient(Message.RecipientType.TO, new InternetAddress(SEND_TO)); // 收件人msg.setRecipient(Message.RecipientType.CC, new InternetAddress(SEND_CC)); // 抄送人msg.setSubject("Simple-这是一封来自好友的邮件","utf-8"); // 标题msg.setText("Simple-愿世界和平!","utf-8"); // 正文// 3.发送Transport.send(msg);}/* 发送html邮件* @throws MessagingException*/private static void sendHtmlMail() throws MessagingException {try {// 1.创建sessionSession session = createSession();// 2.创建邮件对象(Message抽象类的子类对象)MimeMessage msg = new MimeMessage(session); // 传入sessionmsg.setFrom(new InternetAddress(SEND_FROM)); // 发件人msg.setRecipient(Message.RecipientType.TO, new InternetAddress(SEND_TO)); // 收件人msg.setRecipient(Message.RecipientType.CC, new InternetAddress(SEND_CC)); // 抄送人msg.setSubject("html-这是一封来自好友的邮件","utf-8"); // 标题// 邮件正文中包含有“html”标签(控制文本的格式)msg.setText("<b>世界</b>和平!","utf-8","html"); // 正文// 3.发送Transport.send(msg);} catch (MessagingException e) {e.printStackTrace();}}/* 发送附件邮件* @throws MessagingException*/private static void sendFileMail() throws MessagingException, IOException {try {// 1.创建sessionSession session = createSession();// 2.创建邮件对象(Message抽象类的子类对象)MimeMessage msg = new MimeMessage(session); // 传入sessionmsg.setFrom(new InternetAddress(SEND_FROM)); // 发件人msg.setRecipient(Message.RecipientType.TO, new InternetAddress(SEND_TO)); // 收件人msg.setRecipient(Message.RecipientType.CC, new InternetAddress(SEND_CC)); // 抄送人msg.setSubject("file-这是一封来自好友的邮件","utf-8"); // 标题// 3.邮件内容"复合"对象Multipart multipart = new MimeMultipart();// 正文BodyPart textPart = new MimeBodyPart();// 参数1:正文内容// 参数2:内容类型;字符编码集textPart.setContent("file-<b>世界</b>和平!!!", "text/html;charset=utf-8");// 附件BodyPart imagePart = new MimeBodyPart();imagePart.setFileName("look.jpg"); // 设置附件文件的显示名称// 数据处理对象(读取附件文件从本地磁盘进行读取)imagePart.setDataHandler(new DataHandler(new ByteArrayDataSource(Files.readAllBytes(Paths.get("D:\\\\tmp\\\\test.png")),"application/octet-stream")));// 添加至邮件内容multipart.addBodyPart(textPart); // 添加正文multipart.addBodyPart(imagePart); // 添加附件// 设置邮件内容msg.setContent(multipart);// 3.发送Transport.send(msg);} catch (MessagingException e) {e.printStackTrace();}}/* 发送正文嵌套标签邮件* @throws MessagingException*/private static void sendContentTagMail() throws MessagingException, IOException {try {// 1.创建sessionSession session = createSession();// 2.创建邮件对象(Message抽象类的子类对象)MimeMessage msg = new MimeMessage(session); // 传入sessionmsg.setFrom(new InternetAddress(SEND_FROM)); // 发件人msg.setRecipient(Message.RecipientType.TO, new InternetAddress(SEND_TO)); // 收件人msg.setRecipient(Message.RecipientType.CC, new InternetAddress(SEND_CC)); // 抄送人msg.setSubject("ContentTag-爱永不凋零!","utf-8");// 邮件正文部分BodyPart textBodyPart = new MimeBodyPart();StringBuilder body = new StringBuilder();body.append("ContentTag-<h1>世界和平</h1>");body.append("ContentTag-<img src=\\"cid:jue\\"/>"); // 通过内容ID引用附件图片textBodyPart.setContent(body.toString(), "text/html;charset=utf-8");// 邮件附件部分BodyPart imageBodyPart = new MimeBodyPart();imageBodyPart.setFileName("son.jpg"); // 读取名称imageBodyPart.setDataHandler( // 读取附件内容new DataHandler(new ByteArrayDataSource(Files.readAllBytes(Paths.get("D:\\\\tmp\\\\test.png")),"application/octet-stream")));imageBodyPart.setHeader("Content-ID", "<jue>");// 组合正文+附件Multipart multipart = new MimeMultipart();multipart.addBodyPart(textBodyPart); // 添加正文部分multipart.addBodyPart(imageBodyPart); // 添加附件部分// 设置邮件内容msg.setContent(multipart);// 3.发送Transport.send(msg);} catch (MessagingException e) {e.printStackTrace();}}}
参考:用Java实现发送邮件——Java Email_java 发送邮件_小赵不脱发的博客-CSDN博客
参考:https://www.cnblogs.com/roak/p/16261702.html