Java发送邮件已经测试成功的版本

首先maven项目下引入依赖

<dependency>
	<groupId>javax.mail</groupId>
	<artifactId>mail</artifactId>
	<version>1.4.7</version>
</dependency>

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context-support</artifactId>
</dependency>

1.添加mail配置文件
在src/main/resources路径下创建mail.properties

#服务器(填自己发件箱的服务器地址)
mailHost=xxx.xxx.xxx
#端口号 
mailPort=465
#邮箱账号(改成自己的,此为发送邮件方的账号)
mailUsername=xxxxxx
#邮箱密码
mailPassword=xxxxx
#时间延迟
mailTimeout=30000

2.引入配置文件类

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class MailConfig {
	public static final String PROPERTIES_DEFAULT = "mail.properties";
    public static String host;
    public static Integer port;
    public static String userName;
    public static String passWord;
    public static String emailForm;
    public static String timeout;
    public static String personal;
    public static Properties properties;

    static {
        init();
    }

    private static void init(){
        properties = new Properties();
        try {
            //获取配置文件内容
            InputStream inputStream = MailConfig.class.getClassLoader().getResourceAsStream(PROPERTIES_DEFAULT);
            properties.load(inputStream);
            inputStream.close();
            //给属性赋值
            host = properties.getProperty("mailHost");
            port = Integer.parseInt(properties.getProperty("mailPort"));
            userName = properties.getProperty("mailUsername");
            passWord = properties.getProperty("mailPassword");
            emailForm = properties.getProperty("mailUsername");
            timeout = properties.getProperty("mailTimeout");
            //发送人
            personal = "冰箱客诉后台";
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3.创建MailUtils工具以及测试

import com.uboxol.cloud.mermaid.cfg.MailConfig;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

@Component
public class MailUtils {
    private static final String HOST = MailConfig.host;
    private static final Integer PORT = MailConfig.port;
    private static final String USERNAME = MailConfig.userName;
    private static final String PASSWORD = MailConfig.passWord;
    private static final String EMAILFROM = MailConfig.emailForm;
    private static final String TIMEOUT = MailConfig.timeout;
    private static final String PERSONAL = MailConfig.personal;
    private static JavaMailSenderImpl mailSender = createMailsender();
    /**
     * 配置好的工具
     *
     * @return
     */
    private static JavaMailSenderImpl createMailsender() {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost(HOST);
        sender.setPort(PORT);
        sender.setUsername(USERNAME);
        sender.setPassword(PASSWORD);
        sender.setDefaultEncoding("UTF-8");
        Properties p = new Properties();
        p.setProperty("mail.smtp.timeout", TIMEOUT);
        p.setProperty("mail.smtp.auth", "true");
        p.setProperty("mail.smtp.ssl.enable", "true");//加这句ssl的话,对应端口465,不加这句的话端口对应25,不能共存
        sender.setJavaMailProperties(p);
        return sender;
    }

    /**
     * 发送邮件
     *
     * @param to 接收人
     * @param subject 主题
     * @param html 发送内容
     *
     * @throws MessagingException
     * @throws UnsupportedEncodingException
     */
    public static void sendMail(String to, String subject, String html) throws MessagingException, UnsupportedEncodingException {

        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //设置编码
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        mimeMessageHelper.setFrom(EMAILFROM, PERSONAL);
        mimeMessageHelper.setTo(to);
        mimeMessageHelper.setSubject(subject);
        mimeMessageHelper.setText(html, true);
        mailSender.send(mimeMessage);

    }

    /**
     * 发送带附件的邮件
     *
     * @param to 接受人
     * @param subject 主题
     * @param html 发送内容
     * @param filePath 附件路径
     *
     * @throws MessagingException 异常
     * @throws UnsupportedEncodingException 异常
     */
    public static void sendAttachmentMail(String to, String subject, String html, String filePath) throws MessagingException, UnsupportedEncodingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        // 设置utf-8或GBK编码,否则邮件会有乱码
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        messageHelper.setFrom(EMAILFROM, PERSONAL);
        messageHelper.setTo(to);
        messageHelper.setSubject(subject);
        messageHelper.setText(html, true);
        FileSystemResource file = new FileSystemResource(new File(filePath));
        String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
        messageHelper.addAttachment(fileName, file);
        mailSender.send(mimeMessage);
    }

    public static void main(String[] args) {
        try {
            //这里的邮箱是接受者的邮箱,和配置的邮箱不一样
            // 发送普通文本邮件
            sendMail("huyifan@ubox.cn", "邮件发送测试", "<a href='https://www.baidu.com' >百度一下</a>");
            // 发送带附件的邮件
            sendAttachmentMail("huyifan@ubox.cn", "邮件发送测试", "<a href='https://www.baidu.com' >百度一下</a>", "D:\\excel\\示例文件.xlsx");
        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值