JAVA使用 STMP 发送带有附件的邮件 免费下载commons-lang3-3.8.1.jar activation.jar email.jar

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.commons.lang3.StringUtils;

import com.sun.mail.util.MailSSLSocketFactory;

/**
 * @author lzw
 * @Date 2019年2月20日
 */
public class EmailDemo2 {

    private static final String protocol = "smtp";// 协议
    private static final String host = "smtp.163.com";// 服务器地址
    private static final String port = "465";// 端口
    private static final String from = "********";// 发件人用户名
    private static final String password = "******";// 发件人密码 授权码
    
    // 权限认证  可以不做权限认证
    static class MyAuthenricator extends Authenticator {
        String u = null;
        String p = null;
        public MyAuthenricator(String u, String p) {
            this.u = u;
            this.p = p;
        }
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(u, p);
        }
    }
    /**
     * 
     * @param receive 收件人
     * @param subject 主体
     * @param msg     邮件正文
     * @param filename      附件地址
     * @return true/false 发送成功
     */
    public static Boolean sendEmail(String receive, String subject, String msg, String filename){
        if (StringUtils.isAllEmpty(receive)) {
            return false;
        }
        
         Properties prop = new Properties();
        //协议
        prop.setProperty("mail.transport.protocol", protocol);
        //服务器
        prop.setProperty("mail.smtp.host", host);
        //端口
        prop.setProperty("mail.smtp.port", port);
        //使用smtp身份验证
        prop.setProperty("mail.smtp.auth", "true");
        //开启安全协议 使用SSL,企业邮箱必需!
        MailSSLSocketFactory mailSSLSocketFactory = null;
        try {
            mailSSLSocketFactory = new MailSSLSocketFactory();
            mailSSLSocketFactory.setTrustAllHosts(true);
        } catch (GeneralSecurityException e1) {
            e1.printStackTrace();
        }
        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", mailSSLSocketFactory);
 
        Session session = Session.getDefaultInstance(prop, new MyAuthenricator(from, password));
        session.setDebug(true);
        MimeMessage mimeMessage = new MimeMessage(session);
        try {
            mimeMessage.setFrom(new InternetAddress(from));
            //收件人
            mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(receive));
            //主题
            mimeMessage.setSubject(subject);
            //时间
            mimeMessage.setSentDate(new Date());
            //容器类,可以包含多个MimeBodyPart对象
            Multipart mp = new MimeMultipart();
 
            //MimeBodyPart可以包装文本,图片,附件
            MimeBodyPart body = new MimeBodyPart();
            //HTML正文
            body.setContent(msg, "text/html; charset=UTF-8");
            mp.addBodyPart(body);
 
            //添加附件
            if(StringUtils.isNotBlank(filename)){
                body = new MimeBodyPart();
                body.attachFile(filename);
                mp.addBodyPart(body);
            }
            //设置邮件内容
            mimeMessage.setContent(mp);
            mimeMessage.saveChanges();
            Transport.send(mimeMessage);
            // 发送成功
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
 
    public static void main(String[] args) {
        sendEmail("收件箱","主题","正文","附件位置");
    }
}
 

需要导入三个jar包:

commons-lang3-3.8.1.jar

activation.jar

email.jar   需要最新的版本才能有MailSSLSocketFactory这个类

免费获取jar地址

链接: https://pan.baidu.com/s/1DcxmMtTNqiuQRQx9tg1YNw 提取码: 2km4 

常用的服务器对应的端口号:

收件服务器 POP pop.163.com 995 110
收件服务器 IMAP imap.163.com 993 143
发件服务器 SMTP smtp.163.com 465/994 25

猜你喜欢

转载自blog.csdn.net/weixin_42228950/article/details/87854490
今日推荐