javaMail实现发送邮件

邮件发送协议为:smtp

接收协议为:pop3

所以邮件必须满足这个两个协议才能进行接收和发送

代码如下

package com.wem.Dome;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
 * Created by Administrator on 2016-08-18.
 */
public class SendMail {
    private String userName;//用户名
    private String password; // 邮箱密码或者口令
    private String to;  //目标邮箱
    private String subject; //主题
    private String content; //内容
    private List<String> attachmentsList; //附件
    private String smtpType; //smtp类型

    public SendMail(String userName,String password,String to,String subject,String content,List<String> attachmentsList,String smtpType){
        this.userName =userName;
        this.password = password;
        this.to = to;
        this.subject = subject;
        this.content = content;
        this.attachmentsList = attachmentsList;
        this.smtpType = smtpType;
    }

    public void send(){
        Properties props = new Properties();
        props.put("mail.smtp.host", smtpType);
        props.put("mail.smtp.auth", "true");
        //QQ邮箱端口号
        if("smtp.qq.com".equals(smtpType)) {
            props.put("mail.smtp.port", "587");
        }
        //创建Session对象
        Session session = Session.getInstance(props
                //以匿名内部类的形式创建登录服务器的认证对象
                //password 就我目前测试而言,QQ邮箱和163邮箱,密码都为口令,并不是登录密码或者邮箱密码
                , new Authenticator() {
                    public PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(userName, password);
                    }
                });
        try {
            //构造MimeMessage并设置相关属性值
            MimeMessage msg = new MimeMessage(session);
            //设置发件人
            msg.setFrom(new InternetAddress(userName));
            //设置收件人
            InternetAddress[] addresses = {new InternetAddress(to)};
            msg.setRecipients(Message.RecipientType.TO, addresses);
            //设置邮件主题     防止乱码
            try {
                msg.setSubject(MimeUtility.encodeText(subject, MimeUtility.mimeCharset("gb2312"), null));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            //构造Multipart
            Multipart mp = new MimeMultipart();
            //向Multipart添加正文
            MimeBodyPart mbpContent = new MimeBodyPart();
            //mbpContent.setText(content);
            mbpContent.setContent(content, "text/html;charset=gb2312");
            //将BodyPart添加到MultiPart中
            mp.addBodyPart(mbpContent);
            //向Multipart添加附件
            //遍历附件列表,将所有文件添加到邮件消息里
            for (String efile : attachmentsList) {
                MimeBodyPart mbpFile = new MimeBodyPart();
                //以文件名创建FileDataSource对象
                FileDataSource fds = new FileDataSource(efile);
                //处理附件
                mbpFile.setDataHandler(new DataHandler(fds));
                try {
                    if (fds.getName() != null && !("".equals(fds.getName()))) {
                        // 解决中文附件名乱码问题
                        mbpFile.setFileName(MimeUtility.encodeText(new String(fds.getName().getBytes(), "utf-8"), "utf-8", null));
                    } else {
                        mbpFile.setFileName(fds.getName());
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                //将BodyPart添加到MultiPart中
                mp.addBodyPart(mbpFile);
            }
            //清空附件列表
            attachmentsList.clear();
            //向Multipart添加MimeMessage
            msg.setContent(mp, "text/html;charset=utf-8");
            //设置发送日期
            msg.setSentDate(new Date());
            //发送邮件
            Transport.send(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
测试邮件发送
public static void main(String[] arge) throws Exception {
         //用户名
        String userName = "xxxxxxxxxxxx@163.com";
        // 邮箱密码或者口令
        String password = "xxxxxxxxxxxxxxxxx";
        //主题
        String subject = "测试";
        //目标邮箱
        String to = "xxxx@163.com";
        //内容
        String content = "发送成功";
        List arrayList = new ArrayList();
        arrayList.add("D:\\1111.png");
        String stmpType = "smtp.163.com";
        SendMail sendMail = new SendMail(userName,password,to,subject,content,arrayList,stmpType);
        sendMail.send();
    }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值