博客
关于我
初学java javamail总结
阅读量:722 次
发布时间:2019-03-21

本文共 4365 字,大约阅读时间需要 14 分钟。

邮件服务器配置与JavaMail的应用引导

在实际开发中,需要配置常用的邮件协议POP3和IMAP,同时确保相关服务已开启。例如,腾讯邮箱用户需要进入账号设置打开相应服务。配置完成后,可以通过JavaMail来实现邮件的发送和接收操作。

关于JavaMail依赖项的集成,可以通过Maven等工具进行配置。推荐使用如下的依赖版本:

javax.mail
mail
1.4.7

接下来,可以参考如下的代码示例进行邮件操作。该示例主要包含邮件的发送和接收功能,同时也能提取附件文件。请注意处理附件时可添加异常捕捉机制,以应对可能出现的文件读取错误。

生 wrongful code:

package test;import java.io.*;import javax.activation.DataHandler;import javax.activation.FileDataSource;import javax.mail.BodyPart;import javax.mail.Folder;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.Multipart;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMultipart;public class SimpleSendReceiveMail {    // 以下内容需根据实际需求进行设置    private static String username = "你的邮箱地址";    private static String password = "你的邮箱密码";    private static String sendHost = "smtp.qq.com";    private static String receiveHost = "imap.qq.com";    public static void main(String[] args) {        try {            // 初始化邮件会话            Session session = Session.getInstance(getMailProperties());            session.setDebug(true);            // 获取存储对象并连接接收服务器            Store store = session.getStore("imap");            store.connect(receiveHost, new Authenticator() {                @Override                public PasswordAuthentication getPasswordAuthentication() {                    return new PasswordAuthentication(username, password);                }            });            // 发送测试邮件            sendEmail(session, "发送测试邮件");            // 接收邮件            receiveEmail(store);        } catch (MessagingException e) {            System.out.println("邮件操作遇到异常:" + e.getMessage());            e.printStackTrace();        }    }    private static Properties getMailProperties() {        Properties props = new Properties();        props.put("mail.smtp.host", sendHost);        props.put("mail.smtp.port", "465");        props.put("mail.imap.host", receiveHost);        props.put("mail.imap.port", "993");        props.put("mail.debug", "true");        props.put("mail.store_protocol", "imap");        props.put("mail.transport_protocol", "smtp");        return props;    }    private static void sendEmail(Session session) throws MessagingException {        MimeMessage msg = new MimeMessage(session);        InternetAddress from = new InternetAddress("发送方邮箱");        InternetAddress to = new InternetAddress("收信方邮箱");        msg.setFrom(from);        msg.setRecipient(Message.RecipientType.TO, to);        msg.setSubject("邮件测试主题");        msg.setText("邮件内容");        Transport.send(msg, session.getTransport("smtp"));    }    private static void receiveEmail(Store store) throws Exception {        Folder inbox = store.getFolder("INBOX");        inbox.open(Folder.READ_ONLY);        Message[] msgs = inbox.getMessages();        for (int i = 0; i < msgs.length; i++) {            Message msg = msgs[i];            System.out.println("接收的邮件:" + msg.getSubject());            System.out.println("发送者:" + msg.getFrom().getAddress());            System.out.println("接收时间:" + msg.getReceivedDate());                        // 下载附件(如有附件)            if (msg.isMimeType("multipart/*")) {                Multipart mp = (Multipart) msg.getContent();                for (int j = 0; j < mp.getCount(); j++) {                    Part part = mp.getBodyPart(j);                    if (part.getDisposition() == Part.ATTACHMENT) {                        try {                            OutputStream os = new FileOutputStream("附件名称" + (j + 1) + ".txt");                            InputStream is = (InputStream) part.getInputStream();                            byte[] buffer = new byte[1024];                            int count = 0;                            while ((count = is.read(buffer)) != -1) {                                os.write(buffer, 0, count);                            }                            os.close();                            is.close();                        } catch (Exception e) {                            System.out.println("下载附件失败:" + e.getMessage());                        }                    }                }            }        }        inbox.close(false);    }}

以上示例代码可根据实际需求进行修改和扩展。在实际应用中,建议在生产环境中使用try-with-resources语句,并对日志进行适当的处理和错误收集。对于大规模邮件接收或发送操作,建议增加并发控制,避免被邮件服务器认为是垃圾邮件发送。此外,注意 umístence》

转载地址:http://edjgz.baihongyu.com/

你可能感兴趣的文章
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>