JAVA实现发送邮件

工厂类

package com.mgk.tqyb.email;

import static com.mgk.tqyb.email.EmailEnum.*;

public class EmaiFactory {
    public static Email getEmai(EmailEnum emailEnum){
        Email email = null;
        switch (emailEnum)
        {
            case Text:
                email = new EmailText();
                System.out.println("文本");
            break;
            case Picture:
                System.out.println("图片大附件");
            break;
        }
        return email;
    }
}

 email类

package com.mgk.tqyb.email;

import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Properties;

public abstract class  Email {
    //发件人信息
    private String addresserMessage;
    //发件人邮箱
    private String getAddressEmail;
    //邮箱密码16位,这里从qq邮箱取的POP3/SMTP服务
    private String emailPassword;
    //收件人邮箱
    private String recipients;
    //邮件服务器
    private String emailServer;

    private String textData;
    private String emailTitle;

    public String getEmailTitle() {
        return emailTitle;
    }

    public void setEmailTitle(String emailTitle) {
        this.emailTitle = emailTitle;
    }

    public String getTextData() {
        return textData;
    }

    public void setTextData(String textData) {
        this.textData = textData;
    }

    public String getAddresserMessage() {
        return addresserMessage;
    }

    public void setAddresserMessage(String addresserMessage) {
        this.addresserMessage = addresserMessage;
    }

    public String getGetAddressEmail() {
        return getAddressEmail;
    }

    public void setGetAddressEmail(String getAddressEmail) {
        this.getAddressEmail = getAddressEmail;
    }

    public String getEmailPassword() {
        return emailPassword;
    }

    public void setEmailPassword(String emailPassword) {
        this.emailPassword = emailPassword;
    }

    public String getRecipients() {
        return recipients;
    }

    public void setRecipients(String recipients) {
        this.recipients = recipients;
    }

    public String getEmailServer() {
        return emailServer;
    }

    public void setEmailServer(String emailServer) {
        this.emailServer = emailServer;
    }

    //收件人接受的信息
    public abstract void   sendMessage(Properties properties) throws GeneralSecurityException, MessagingException;
    //加载邮件内容
    public abstract Properties loadEmail() throws IOException, GeneralSecurityException;
    //qq特性
    public void  setQQStyle(Properties properties) throws GeneralSecurityException {

    }

    //发送邮件
    public  void sendEmail() throws IOException, GeneralSecurityException, MessagingException {
        sendMessage( loadEmail());
    };

} 

枚举类

 

package com.mgk.tqyb.email;

public enum EmailEnum {
    Text,Picture;
}

 email发送文本类

package com.mgk.tqyb.email;


import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.*;
import java.security.GeneralSecurityException;
import java.util.Properties;

public class EmailText extends Email{
    private Properties properties = new Properties();
    @Override
    public Properties loadEmail() throws IOException, GeneralSecurityException {
        InputStream in = EmailText.class.getClassLoader().getResourceAsStream("email.properties");
        properties.load(in);
        setAddresserMessage(properties.get("emailAddresserMessage").toString());//发件人信息
        setGetAddressEmail(properties.get("emailAddresserEmail").toString());//发件人邮箱
        setEmailPassword(properties.get("emailPassword").toString());//发件人密码
        setRecipients(properties.get("emailRecipients").toString());//收件人邮箱
        setEmailServer(properties.get("emailServer").toString());//邮箱服务
        Properties emailProperties = new Properties();
        emailProperties.setProperty("mail.host",getEmailServer());
        emailProperties.setProperty("mail.transport.protocol","smtp");
        emailProperties.setProperty("mail.smtp.auth","true");
        setQQStyle(emailProperties);
        return  emailProperties;
    }
    @Override
    public void setQQStyle(Properties properties) throws GeneralSecurityException {
        if (getEmailServer().toString().indexOf("qq") != -1){
            //设置QQ特性设置SSL加密
            MailSSLSocketFactory msf = new MailSSLSocketFactory();
            msf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable", "true");
            properties.put("mail.smtp.ssl.socketFactory", msf);
        }
    }
    @Override
    public void sendMessage(Properties properties) throws GeneralSecurityException, MessagingException {
        //验证身份
        MyAuthenticator myAuthenticator = new MyAuthenticator();
        myAuthenticator.setUsername(getRecipients());//邮箱
        myAuthenticator.setPassword(getEmailPassword());//密码
        //创建Session 类包mail
        Session defaultInstance = Session.getDefaultInstance(properties,myAuthenticator);
        //是否Debug运行
        defaultInstance.setDebug(false);
        //获取Transport对象用来链接服务器
        Transport transport = defaultInstance.getTransport();
        //连接邮箱服务器
        transport.connect(getEmailServer(),getGetAddressEmail(),getEmailPassword());
        //创建邮件对象用来发送邮件
        MimeMessage mimeMessage = new MimeMessage(defaultInstance);
        //发送人
        mimeMessage.setFrom(new InternetAddress(getAddresserMessage()));
        //邮箱主题
        mimeMessage.setSubject(getEmailTitle());
        //接收人
        mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress(getRecipients()));
        //邮件内容
        mimeMessage.setContent(getTextData(),"text/html;charset=UTF-8");
        //发送邮件
        transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
        try {
        }
        finally {
            //连接必须得关闭
            transport.close();
        }
    }
    public static void main(String[] args) throws IOException, GeneralSecurityException, MessagingException {
        Email em = new EmailText();
        em.setEmailTitle("hhh");
        StringBuilder sbf = new StringBuilder();
        sbf.append("这是一段话");
        em.setTextData(sbf.toString());
        em.sendEmail();
    }
}

 验证器类

package com.mgk.tqyb.email;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class MyAuthenticator extends Authenticator {
    private String username;
    private String password;
    public MyAuthenticator(){

    }

    public MyAuthenticator(String username, String password) {
        this.username = username;
        this.password = password;
    }

    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password);
    }

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

email.properties文件

 

#邮件服务器
emailServer=smtp.qq.com
#收件人信息
emailRecipients=********@qq.com
#发件人密码
emailPassword=********
#发件人信息
emailAddresserMessage=********@qq.com
#发件人邮箱
emailAddresserEmail=********@qq.com

 获得授权码发件人16密码

 

进入设置,找到账户,下拉找到SMTP服务开启服务,即可获得

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值