java邮箱工具类

java邮箱工具类

1.首先导入所有需要的maven坐标

<dependency>
            <groupId>javax.mail</groupId>
            <artifactId>javax.mail-api</artifactId>
            <version>1.5.6</version>
 </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.5.6</version>
        </dependency>

2.然后我们需要编写一个邮箱的工具类,具体代码如下

import javax.activation.DataHandler;

import javax.activation.FileDataSource;

import javax.mail.*;

import javax.mail.internet.*;

import java.io.File;

import java.util.Properties;


public class MailUtils {
    
    
    //邮件传送协议,不同的邮箱传送协议也不一样,这里采用QQ传送协议
    private static String smtp_host = "smtp.qq.com";
    //需要发送的邮箱号码,这里填上的邮箱
    private static String username = "********@qq.com";
    //这里不是邮箱密码,需要开通POP3/SMTP服务,会有个授权码。
    private static String password = "*****";
    //参数1,需要发送的邮箱,参数2邮箱标题,参数3邮箱内容。
    public static void sendSimpleMail(String to, String subject, String msg) {
    
    

        Properties props = new Properties();

        props.setProperty("mail.host", smtp_host);

        props.setProperty("mail.transport.protocol", "smtp");

        props.setProperty("mail.smtp.auth", "true");

// 创建验证器

        Authenticator auth = new Authenticator() {
    
    

            public PasswordAuthentication getPasswordAuthentication() {
    
    

                // 密码验证

                return new PasswordAuthentication(username,passowrd);// 邮箱的授权码

            }

        };


        try {
    
    

// 1.创建会话

            Session session = Session.getInstance(props, auth);

// 开启Session的debug模式,这样就可以查看到程序发送Email的运行状态

            session.setDebug(true);

// 2.获取传输对象

            Transport transport = session.getTransport();

// 3.设置连接

            transport.connect(smtp_host, username, password);

// 4.创建邮件

            Message message = new MimeMessage(session);

            message.setFrom(new InternetAddress(username));//发送人

            message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));//接收人

            message.setSubject(subject);//标题


            message.setContent(msg, "text/html;charset=utf-8");//内容

// 5.发送邮件

            transport.sendMessage(message, message.getAllRecipients());

        } catch (Exception e) {
    
    

            e.printStackTrace();

            throw new RuntimeException("邮件发送失败");

        }

    }


//发送带有附件的邮件

    public static void sendAttachmentMail(String to, String subject, String msg, File file) {
    
    

        Properties props = new Properties();

        props.setProperty("mail.host", smtp_host);

        props.setProperty("mail.transport.protocol", "smtp");

        props.setProperty("mail.smtp.auth", "true");


// 创建验证器

        Authenticator auth = new Authenticator() {
    
    

            public PasswordAuthentication getPasswordAuthentication() {
    
    

                // 密码验证

                return new PasswordAuthentication(username, password);// 邮箱的授权码

            }

        };


        try {
    
    

// 1.创建会话

            Session session = Session.getInstance(props, auth);

// 开启Session的debug模式,这样就可以查看到程序发送Email的运行状态

            session.setDebug(true);

// 2.获取传输对象

            Transport transport = session.getTransport();

// 3.设置连接

            transport.connect(smtp_host,username, password);

// 4.创建邮件

// 邮件头

            Message message = new MimeMessage(session);

            message.setFrom(new InternetAddress(username));

            message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));

//message.addRecipients(MimeMessage.RecipientType.CC, InternetAddress.parse(username));

            message.setSubject(subject);


// 邮件体(发送的字符串内容+附件)

            Multipart multipart = new MimeMultipart();

// 信息(发送的字符串内容)

            BodyPart content = new MimeBodyPart();

            content.setContent(msg, "text/html;charset=utf-8");

// 添加信息

            multipart.addBodyPart(content);


// 附件

            BodyPart attachment = new MimeBodyPart();

//附件对象----->路径------>附件转换为DataSource源------>DataSource源封装到附件对象中

            String filePath = file.getPath();

            FileDataSource fileDataSource = new FileDataSource(filePath);

            attachment.setDataHandler(new DataHandler(fileDataSource));


//设置接收到 的附件名

            String filename = fileDataSource.getName();

            attachment.setFileName(MimeUtility.encodeText(filename));


// 添加附件

            multipart.addBodyPart(attachment);

// 5.发送邮件

            message.setContent(multipart);

            transport.sendMessage(message, message.getAllRecipients());

        } catch (Exception e) {
    
    

            e.printStackTrace();

            throw new RuntimeException("邮件发送失败");

        }

    }

猜你喜欢

转载自blog.csdn.net/zhangzhenkeai/article/details/109270466
今日推荐