Java 腾讯邮箱发送邮件工具类(单人+多人)

邮箱实体类

public class EmailMailInfo {
    //邮箱服务器 如smtp.163.com
    private String host ;
    //用户邮箱 如**@163
    private String formName ;
    //用户授权码 不是用户名密码 可以自行查看相关邮件服务器怎么查看
    private String formPassword ;
    //消息回复邮箱
    private String replayAddress ;
    //发送地址
    private String toAddress ;
    //发送主题
    private String subject ;
    //发送内容
    private String content ;
    //端口号
    private String port;
    //多人邮件
    private InternetAddress[] emailMany;

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getHost() {
        return host;
    }
    public void setHost(String host) {
        this.host = host;
    }
    public String getFormName() {
        return formName;
    }
    public void setFormName(String formName) {
        this.formName = formName;
    }
    public String getFormPassword() {
        return formPassword;
    }
    public void setFormPassword(String formPassword) {
        this.formPassword = formPassword;
    }
    public String getReplayAddress() {
        return replayAddress;
    }
    public void setReplayAddress(String replayAddress) {
        this.replayAddress = replayAddress;
    }
    public String getToAddress() {
        return toAddress;
    }
    public void setToAddress(String toAddress) {
        this.toAddress = toAddress;
    }
    public String getSubject() {
        return subject;
    }
    public void setSubject(String subject) {
        this.subject = subject;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }

    public InternetAddress[] getEmailMany() {
        return emailMany;
    }

    public void setEmailMany(InternetAddress[] emailMany) {
        this.emailMany = emailMany;
    }
}

具体业务实现

	/**
     * 多个账号邮箱的公共方法
     * @param emailList
     * @return
     */
    private static InternetAddress[] addressMails(List emailList) throws AddressException {
        List list = new ArrayList();//不能使用string类型的类型,这样只能发送一个收件人
        //多个接收账号
        for (int i = 0; i < emailList.size(); i++) {
            list.add(new InternetAddress(emailList.get(i).toString()));
        }
        return (InternetAddress[]) list.toArray(new InternetAddress[list.size()]);
    }

	/**
     * 发送多人邮件
     * @throws Exception
     */
    private void SendMoreEmail() throws Exception {
        List<String> noticeList = new ArrayList<>();
        noticeList.add("163@163.com");
        noticeList.add("164@163.com");
        EmailMailInfo info = new EmailMailInfo();
        info.setSubject("主题");
        info.setContent("这里是邮件内容啊啊啊啊啊啊啊啊啊啊啊啊啊");
        info.setEmailMany(addressMails(noticeList));//多人邮件
        EmailSendUtil.sendHtmlMail(info);
    }

	/**
	 *  单人邮箱
	 */
	public static void main(String[] args) {
			String email = "1578855@qq.com";
			String title = "用户";
			String content = "您好:我是邮箱";
			EmailMailInfo info = new EmailMailInfo();
			info.setToAddress(email);
			info.setSubject(title);
			info.setContent(content);
			try {
				EmailSendUtil.sendHtmlMail(info);
			} catch (Exception e) {
				System.out.print("'" + title + "'的邮件发送失败!");
				e.printStackTrace();
			}
		}

发送邮件工具类

import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
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 com.sun.mail.util.MailSSLSocketFactory;


public class EmailSendUtil {
    private final static String host = "smtp.exmail.qq.com"; //腾讯企业邮的服务器
    private final static String formName = "邮箱";//发送人邮箱
    private final static String password = "密码"; //授权码

    private final static String replayAddress = "邮箱"; //发送人邮箱
    private final static String port = "465"; //端口号
    //private final static String port = "587"; //你的邮箱


    public static void sendHtmlMail(EmailMailInfo info)throws Exception{
        info.setHost(host);
        info.setFormName(formName);
        info.setFormPassword(password);   //网易邮箱的授权码~不一定是密码
        info.setReplayAddress(replayAddress);
        info.setPort(port);
        Message message = getMessage(info);
        // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
        Multipart mainPart = new MimeMultipart();
        // 创建一个包含HTML内容的MimeBodyPart
        BodyPart html = new MimeBodyPart();
        // 设置HTML内容
        html.setContent(info.getContent(), "text/html; charset=utf-8");
        mainPart.addBodyPart(html);
        // 将MiniMultipart对象设置为邮件内容
        message.setContent(mainPart);
        Transport.send(message);
    }

    public static void sendTextMail(EmailMailInfo info) throws Exception {

        info.setHost(host);
        info.setFormName(formName);
        info.setFormPassword(password);   //网易邮箱的授权码~不一定是密码
        info.setReplayAddress(replayAddress);
        Message message = getMessage(info);
        //消息发送的内容
        message.setText(info.getContent());

        Transport.send(message);
    }

    private static Message getMessage(EmailMailInfo info) throws Exception{
        final Properties p = System.getProperties() ;
        p.setProperty("mail.smtp.host", info.getHost());
        p.setProperty("mail.smtp.auth", "true");
        p.setProperty("mail.smtp.user", info.getFormName());
        p.setProperty("mail.smtp.pass", info.getFormPassword());
        p.setProperty("mail.smtp.port", info.getPort());//设置端口
        p.setProperty("mail.transport.protocol", "smtps");//

        //开启安全协议
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            p.put("mail.smtp.ssl.enable", "true");
            p.put("mail.smtp.ssl.socketFactory", sf);
        } catch (GeneralSecurityException e1) {
            e1.printStackTrace();
        }

        // 根据邮件会话属性和密码验证器构造一个发送邮件的session
        Session session = Session.getDefaultInstance(p, new Authenticator(){
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(p.getProperty("mail.smtp.user"),p.getProperty("mail.smtp.pass"));
            }
        });
        session.setDebug(false);
        Message message = new MimeMessage(session);
        //消息发送的主题
        message.setSubject(info.getSubject());
        //接受消息的人
        //message.setReplyTo(InternetAddress.parse(info.getReplayAddress()));
        //消息的发送者
        message.setFrom(new InternetAddress(p.getProperty("mail.smtp.user"),"系统"));
        // 创建邮件的接收者地址,并设置到邮件消息中
        //单人接收邮件
        //message.setRecipient(Message.RecipientType.TO,new InternetAddress(info.getToAddress()));
        //多人接收邮件
        message.setRecipients(Message.RecipientType.TO, info.getEmailMany());
        // 消息发送的时间
        message.setSentDate(new Date());
        return message ;
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值