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 

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

收件服务器POPpop.163.com995110
收件服务器IMAPimap.163.com993143
发件服务器SMTPsmtp.163.com465/99425

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值