java mail发送、接收邮件

java mail接收邮件

1、引入java mail依赖


    org.eclipse.angus
    angus-mail
    2.0.2

2、编写代码

注意:下述代码中的服务器地址,用户名均是博主自己搭建的邮件服务器中的信息,请对应修改为自己的信息,邮件服务器搭建可以参考 

Apache James邮件服务器搭建(windows)_Steven-Russell的博客-CSDN博客Apache James邮件服务器搭建(linux)_Steven-Russell的博客-CSDN博客

package com.wd;

import jakarta.mail.*;
import jakarta.mail.search.FlagTerm;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

public class ReceiverPop {

    public static void main(String[] args) throws Exception {
        Properties props = new Properties();
        props.put("mail.pop3.host", "192.168.133.135");
        props.put("mail.pop3.port", 110);
        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("[email protected]", "123456");
            }
        });
        Store store = session.getStore("pop3");
        store.connect();
        Folder folder = store.getFolder("INBOX");
        folder.open(Folder.READ_WRITE);
        FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false); //false代表未读,true代表已读
        Message[] messages = folder.search(ft);
        Message lastMessage = messages[messages.length - 1];
        System.out.println(new FormatMess(lastMessage));
    }

    private static class FormatMess {

        private final String subject;

        private final String content;

        private final String from;

        private final Date sendDate;

        public FormatMess(Message message) throws Exception {
            this.subject = message.getSubject();
            this.content = String.valueOf(message.getContent());
            this.sendDate = message.getSentDate();
            Address[] messageFrom = message.getFrom();
            List messageFromList = new ArrayList<>();
            for (Address address : messageFrom) {
                messageFromList.add(address.toString());
            }
            this.from = String.join(",", messageFromList);
        }

        @Override
        public String toString() {
            return "=======start========" + System.lineSeparator()
                    + "sendDate=" + sendDate + System.lineSeparator()
                    + "from=" + from + System.lineSeparator()
                    + "subject=" + subject + System.lineSeparator()
                    + "content=" + content + System.lineSeparator()
                    + "=======end========";
        }
    }
}

3、发送邮件,可以参考如下链接使用curl命令进行Apache James邮件服务器搭建(linux)_Steven-Russell的博客-CSDN博客

curl -ikv --url 'smtps://192.168.133.135:465' --mail-from '[email protected]' --mail-rcpt '[email protected]' --upload-file /root/email.txt --user '[email protected]:123456'

编辑文件/root/email.txt

From: steven
To: russell
Subject: st.com test email

// 注意空行

test email info.

4、接收邮件,可以发现已经接收到了邮件信息

java mail发送、接收邮件_第1张图片

java-mail发送邮件:

1、引入java mail依赖


    org.eclipse.angus
    angus-mail
    2.0.2

2、编写代码

package com.wd;

import jakarta.mail.Message;
import jakarta.mail.Session;
import jakarta.mail.Transport;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;

import java.util.Properties;

public class SendMail {

    public static void main(String[] args) throws Exception {
        Properties properties = new Properties();
        properties.put("mail.transport.protocol", "smtp");
        properties.put("mail.smtp.host", "192.168.133.135");
        properties.put("mail.smtp.port", 465);
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.ssl.enable", true);
//        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.debug", "true");

        Session session = Session.getInstance(properties);
        // 获取邮件对象
        Message message = new MimeMessage(session);
        //设置发件人邮箱地址
        message.setFrom(new InternetAddress("[email protected]"));
        //设置收件人地址
        message.setRecipients(Message.RecipientType.TO, new InternetAddress[] { new InternetAddress("[email protected]") });
        //设置邮件标题
        message.setSubject("这是第一封Java邮件");
        //设置邮件内容
        message.setText("内容为: 这是第一封java发送来的邮件。");
        //得到邮差对象
        try (Transport transport = session.getTransport();){
            //连接自己的邮箱账户
            transport.connect("[email protected]", "123456");//密码为刚才得到的授权码
            //发送邮件
            transport.sendMessage(message, message.getAllRecipients());
        }
    }

}

你可能感兴趣的:(java,开发语言)