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

你可能感兴趣的文章
NAS、SAN和DAS的区别
查看>>
NAS个人云存储服务器搭建
查看>>
NAS服务器有哪些优势
查看>>
NAT PAT故障排除实战指南:从原理到技巧的深度探索
查看>>
nat 网卡间数据包转发_你是不是从来没有了解过光纤网卡,它跟普通网卡有什么区别?...
查看>>
NAT-DDNS内网穿透技术,快解析DDNS的优势
查看>>
NAT-DDNS内网穿透技术,快解析DDNS的优势
查看>>
NAT-DDNS内网穿透技术,解决动态域名解析难题
查看>>
natapp搭建外网服务器
查看>>
NativePHP:使用PHP构建跨平台桌面应用的新框架
查看>>
nativescript(angular2)——ListView组件
查看>>
NativeWindow_01
查看>>
Native方式运行Fabric(非Docker方式)
查看>>
Nature | 电子学“超构器件”, 从零基础到精通,收藏这篇就够了!
查看>>
Nature和Science同时报道,新疆出土四千年前遗骸完成DNA测序,证实并非移民而是土著...
查看>>
Nature封面:只低一毫米,时间也会变慢!叶军团队首次在毫米尺度验证广义相对论...
查看>>
Nat、端口映射、内网穿透有什么区别?
查看>>
Nat、端口映射、内网穿透有什么区别?
查看>>
nat打洞原理和实现
查看>>
NAT技术
查看>>