javamail发信和收信机制(smtp、pop3、imap)

JSON 2016-08-22 14:59:29 12400

对于邮件的发送和邮件的收取,很多场景都用到过,那么下面来看一下 Java  对邮件的收发操作。

pop3收信模式

public void pop3Receive(){  
        try {  
            Properties props = System.getProperties();  
            props.setProperty("mail.pop3.host", "192.168.1.194");  
            props.setProperty("mail.pop3.port", "110");  
            props.setProperty("mail.store.protocol", "pop3");  
            props.setProperty("mail.debug", "true");  
            String username = "test@ext.com";  
            String password = "123456";  
            Session session = Session.getInstance(props);  
              
            Store store = (Store) session.getStore("pop3");  
            store.connect(username,password);  
            Folder folder = null;  
            folder =  (Folder) store.getFolder("INBOX");  
            if (folder.exists())  
                folder.open(2);  
            Message[] messages = folder.getMessages();  
            if(messages!=null&&messages.length>0){  
                for (Message message : messages) {  
                    System.out.println(message.getSubject());  
                }  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  

imap收信

public void imapReceive(){  
        try {  
            Properties props = System.getProperties();  
            props.setProperty("mail.imap.host", "192.168.1.194");  
            props.setProperty("mail.imap.port", "143");  
            props.setProperty("mail.store.protocol", "imap");  
            props.setProperty("mail.debug", "true");  
            String username = "test@ext.com";  
            String password = "123456";  
            Session session = Session.getInstance(props);  
              
            IMAPStore store = (IMAPStore) session.getStore("imap");  
            store.connect(username,password);  
            IMAPFolder folder = null;  
            folder =  (IMAPFolder) store.getFolder("INBOX");  
            if (folder.exists())  
                folder.open(2);  
            Message[] messages = folder.getMessages();  
            if(messages!=null&&messages.length>0){  
                for (Message message : messages) {  
                    System.out.println(message.getSubject());  
                }  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }

smtp 非加密方式发信

public void smtpSend(){  
        try {  
            Properties properties = new Properties();  
            properties.setProperty("mail.transport.protocol", "smtp");// 发送邮件协议  
            properties.setProperty("mail.smtp.auth", "true");// 需要验证  
            properties.setProperty("mail.smtp.port", "25");  
            properties.setProperty("mail.transport.protocol", "smtp");  
            properties.setProperty("mail.smtp.host", "192.168.1.194");  
            properties.setProperty("mail.debug", "true");//设置debug模式  
            final String username = "test@ext.com";  
            final String password = "123456";  
            // 后台输出邮件发送的过程  
            Session session = Session.getInstance(properties,  
                    new Authenticator() {  
                        protected PasswordAuthentication getPasswordAuthentication() {  
                            return new PasswordAuthentication(username,  
                                    password);  
                        }  
                    });  
            // 邮件信息  
            Message messgae = new MimeMessage(session);  
            messgae.setFrom(new InternetAddress("test@ext.com"));// 设置发送人  
            messgae.setText("X5O!P%@AP[4PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*");// 设置邮件内容  
            messgae.setSubject("测试postfix邮件发送");// 设置邮件主题  
            // 发送邮件  
            Transport tran = session.getTransport();  
            tran.connect("192.168.1.194", username, password);// 连接到新浪邮箱服务器  
            tran.sendMessage(messgae, new Address[] { new InternetAddress("postmaster@ext.com") });// 设置邮件接收人  
            tran.close();  
        } catch (Exception e) {  
        }  
    }  

smtp 加密方式发信

public void smtpSSLSend(){  
        try {  
            MailSSLSocketFactory sf = new MailSSLSocketFactory();  
            sf.setTrustAllHosts(true);  
            Properties properties = new Properties();  
            // 邮件发送协议  
            properties.setProperty("mail.transport.protocol", "smtp");  
            // SMTP邮件服务器   
            properties.setProperty("mail.smtp.host", "192.168.1.194");  
            // SMTP邮件服务器默认端口   
            properties.put("mail.smtp.socketFactory.port", 465);//发信端口  
             // 是否要求身份认证   
            properties.setProperty("mail.smtp.auth", "true");  
             // 是否启用调试模式  
            properties.setProperty("mail.debug", "true");//设置debug模式  
            properties.put("mail.smtp.ssl.enable", "true");//是否开启ssl  
            properties.put("mail.smtp.ssl.socketFactory", sf);  
            // 发送邮件协议  
            properties.setProperty("mail.smtp.auth", "true");// 需要验证  
            final String username = "test@ext.com";  
            final String password = "123456";  
              
            // 创建Session实例对象   
            Session session = Session.getDefaultInstance(properties,new Authenticator() {  
                protected PasswordAuthentication getPasswordAuthentication() {  
                    return new PasswordAuthentication(username,  
                            password);  
                }  
            });  
            // 创建MimeMessage实例对象   
            MimeMessage message = new MimeMessage(session);   
            // 设置发件人   
            message.setFrom(new InternetAddress("test@ext.com"));   
            // 设置邮件主题   
            message.setSubject("SSL认证测试javamail");   
            // 设置收件人   
            message.setRecipient(RecipientType.TO, new InternetAddress("postmaster@ext.com"));   
            // 设置发送时间   
            message.setSentDate(new Date());   
            // 设置纯文本内容为邮件正文   
            message.setText("abcdefghijklmnopqrstuvwxyz");  
            //回执  
            message.setHeader("Disposition-Notification-To", "test@ext.com");  
            //紧急  
            message.setHeader("X-Priority", "1");  
            // 保存并生成最终的邮件内容   
            message.saveChanges();   
            Transport.send(message);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
}  


版权所属:SO JSON在线解析

原文地址:https://www.sojson.com/blog/154.html

转载时必须以链接形式注明原始出处及本声明。

本文主题:

如果本文对你有帮助,那么请你赞助我,让我更有激情的写下去,帮助更多的人。

关于作者
一个低调而闷骚的男人。
相关文章
IE、Firefox对同一域名访问并,及解决优化方案
Shiro 权限控设计、权限控Demo、基于RBAC3
HTMLJSON如何互转
Httpclent 请求限,判断返回类型返回数据大小
HTTP/3 来了,您真的了解它么?
一些Node.js开工具、开包、框架等总结
Java 10正式布!增加109项新特性
for循环的 i++ ++i 的区别
js html5 canvas作多个小球碰撞的动画效果
阿里云腾讯云哪个好?
最新文章
HTML5 Canvas弧线教程 18
Java赋值运算符 73
XML内部实体和外部实体 201
Java面向对象编程概念 157
PHP回显语句 101
Linux—文件树 116
C语言while循环和do while循环 132
Python元组剖析 209
MySQL触发器教程 342
sql使用布尔运算符和关系运算符 258
最热文章
最新MyEclipse8.5注册码,有效期到2020年 (已经更新) 682317
苹果电脑Mac怎么恢复出厂系统?苹果系统怎么重装系统? 674722
免费天气API,全国天气 JSON API接口,可以获取五天的天气预报 602380
免费天气API,天气JSON API,不限次数获取十五天的天气预报 578213
Jackson 时间格式化,时间注解 @JsonFormat 用法、时差问题说明 552949
我为什么要选择RabbitMQ ,RabbitMQ简介,各种MQ选型对比 509407
Elasticsearch教程(四) elasticsearch head 插件安装和使用 480060
Jackson 美化输出JSON,优雅的输出JSON数据,格式化输出JSON数据... ... 264675
Java 信任所有SSL证书,HTTPS请求抛错,忽略证书请求完美解决 244332
Elasticsearch教程(一),全程直播(小白级别) 225624
支付扫码

所有赞助/开支都讲公开明细,用于网站维护:赞助名单查看

查看我的收藏

正在加载... ...