博客
关于我
初学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/

你可能感兴趣的文章
MySQL中使用IN()查询到底走不走索引?
查看>>
Mysql中使用存储过程插入decimal和时间数据递增的模拟数据
查看>>
MySql中关于geometry类型的数据_空的时候如何插入处理_需用null_空字符串插入会报错_Cannot get geometry object from dat---MySql工作笔记003
查看>>
mysql中出现Incorrect DECIMAL value: '0' for column '' at row -1错误解决方案
查看>>
mysql中出现Unit mysql.service could not be found 的解决方法
查看>>
mysql中出现update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在 dpkg: 处理软件包 mysql-server-8.0的解决方法(全)
查看>>
Mysql中各类锁的机制图文详细解析(全)
查看>>
MySQL中地理位置数据扩展geometry的使用心得
查看>>
Mysql中存储引擎简介、修改、查询、选择
查看>>
Mysql中存储过程、存储函数、自定义函数、变量、流程控制语句、光标/游标、定义条件和处理程序的使用示例
查看>>
mysql中实现rownum,对结果进行排序
查看>>
mysql中对于数据库的基本操作
查看>>
Mysql中常用函数的使用示例
查看>>
MySql中怎样使用case-when实现判断查询结果返回
查看>>
Mysql中怎样使用update更新某列的数据减去指定值
查看>>
Mysql中怎样设置指定ip远程访问连接
查看>>
mysql中数据表的基本操作很难嘛,由这个实验来带你从头走一遍
查看>>
Mysql中文乱码问题完美解决方案
查看>>
mysql中的 +号 和 CONCAT(str1,str2,...)
查看>>
Mysql中的 IFNULL 函数的详解
查看>>