博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
邮件发送1
阅读量:5767 次
发布时间:2019-06-18

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

package com.tarena.netctoss.test;import java.util.Properties;import javax.mail.Address;import javax.mail.Flags;import javax.mail.Folder;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.NoSuchProviderException;import javax.mail.Session;import javax.mail.Store;import javax.mail.Transport;import javax.mail.internet.AddressException;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;public class TestMail {    public static void main(String[] args)    {            TestMail.sendMail();//            //TestMail.receiveMail();//            TestMail.deleteMail();    }        /**     * send mail     */    public static void sendMail()    {        String host = "smtp.163.com";//邮件服务器        String from = "snail_zhou@163.com";//发件人地址        String to = "972661036@qq.com";//接受地址(必须支持pop3协议)        String userName = "snail_zhou";//发件人邮件名称        String pwd = "*************";//发件人邮件密码        Properties props = new Properties();        props.put("mail.smtp.host", host);        props.put("mail.smtp.auth", "true");                Session session = Session.getDefaultInstance(props);        session.setDebug(true);                MimeMessage msg = new MimeMessage(session);        try {            msg.setFrom(new InternetAddress(from));            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));//发送            msg.setSubject("我的测试...........");//邮件主题            msg.setText("测试内容。。。。。。。");//邮件内容            msg.saveChanges();                        Transport transport = session.getTransport("smtp");            transport.connect(host, userName, pwd);//邮件服务器验证            transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));            System.out.println("send ok...........................");        } catch (AddressException e) {            e.printStackTrace();        } catch (MessagingException e) {            e.printStackTrace();        }    }        /**     * receive mail     */    public static void receiveMail()    {        String host = "pop3.sina.com";        String userName = "xingui5624";        String passWord = "******";                Properties props = new Properties();        Session session = Session.getDefaultInstance(props);        session.setDebug(true);        try {            System.out.println("receive...............................");            Store store = session.getStore("pop3");            store.connect(host, userName,passWord);//验证            Folder folder = store.getFolder("INBOX");//取得收件文件夹            folder.open(Folder.READ_WRITE);            Message msg[] = folder.getMessages();            System.out.println("邮件个数:" + msg.length);                        for(int i=0; i
0) from.append(";"); from.append(address[j].toString()); } System.out.println(message.getMessageNumber()); System.out.println("来自:" + from.toString()); System.out.println("大小:" + message.getSize()); System.out.println("主题:" + message.getSubject()); System.out.println("时间::" + message.getSentDate()); System.out.println("==================================================="); } folder.close(true);//设置关闭 store.close(); System.out.println("receive over............................"); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } } /** * delete mail */ public static void deleteMail() { String host = "pop3.sina.com"; String userName = "xingui5624"; String passWord = "******"; Properties props = new Properties(); //Properties props = System.getProperties();这种方法创建 Porperties 同上 Session session = Session.getDefaultInstance(props); session.setDebug(true); try { System.out.println("begin delete ..........."); Store store = session.getStore("pop3"); store.connect(host, userName, passWord);//验证邮箱 Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_WRITE);//设置我读写方式打开 int countOfAll = folder.getMessageCount();//取得邮件个数 int unReadCount = folder.getUnreadMessageCount();//已读个数 int newOfCount = folder.getNewMessageCount();//未读个数 System.out.println("总个数:" +countOfAll); System.out.println("已读个数:" +unReadCount); System.out.println("未读个数:" +newOfCount); for(int i=1; i<=countOfAll; i++) { Message message = folder.getMessage(i); message.setFlag(Flags.Flag.DELETED, true);//设置已删除状态为true if(message.isSet(Flags.Flag.DELETED)) System.out.println("已经删除第"+i+"邮件。。。。。。。。。"); } folder.close(true); store.close(); System.out.println("delete ok................................."); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } } /** * reply mail */ public static void replyMail() { //test } }

 

转载于:https://www.cnblogs.com/zhoushengbing/p/3251289.html

你可能感兴趣的文章
如何给Windows Server 2012 R2 添加“磁盘清理”选项
查看>>
js中如何以最简单的方式将数组元素添加到对象中
查看>>
Linux makefile 教程 非常详细,且易懂
查看>>
获取短信
查看>>
MQTT简介
查看>>
Django文档学习
查看>>
i submit a bug for springmvc
查看>>
[zz] fstab mtab
查看>>
2011-04-18 python 文件copy 之道 大全 (转)
查看>>
poj2007
查看>>
C++ 一串数字三位一节,用逗号隔开表示
查看>>
sizeof和mallo
查看>>
[Linux] 进程通信-消息队列
查看>>
UPNP实现的基本原理
查看>>
Vertical Menu ver4
查看>>
comet
查看>>
Ext.grid.Panel表格分页
查看>>
rtp rtcp rtsp
查看>>
Eclipse 中Alt+/快捷键失效的解决办法。
查看>>
【反传销】春节一个短暂误入传销和脱身的真实故事以及对技术的思考
查看>>