Jmail收发邮件

Jmail 接收邮件:
import javax.mail.PasswordAuthentication;
import javax.mail.Authenticator;
import java.util.*;
import javax.mail.*;
import java.io.*;
public class ReceiveMail {
    public ReceiveMail() {
    }
    public static void main(String[] args) throws Exception {
        ReceiveMail receivemail = new ReceiveMail();
        receivemail.receive();
    }
    public void receive() throws Exception {
        Properties prop = new Properties();
        prop.setProperty("mail.pop3.host", "pop3.sina.com");
        prop.setProperty("mail.pop3.auth", "true");
        MyAuthenticator auth = new MyAuthenticator();
        Session session = Session.getDefaultInstance(prop, auth);
        Store store = session.getStore("pop3");
        store.connect("pop3.sina.com", "[email protected]", "邮箱的密码");
        Folder defaultFolder = store.getDefaultFolder();
        Folder folder = defaultFolder.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);
        Message[] message = folder.getMessages();
        for (int i = 0; i < message.length; i++) {
            System.out.println(
                    "-------------------第" + i + " 封邮件------------");
            Message msg = message;
            System.out.println("邮件标题:" + msg.getSubject());
            System.out.println("邮件正文:" + msg.getContent());
            if (msg.getContent() instanceof Multipart) {
                Multipart mp = (Multipart) msg.getContent();
                for (int t = 0; t < mp.getCount(); t++) {
                    BodyPart part = mp.getBodyPart(t);
                    String fileName = part.getFileName();
                    if(fileName==null)
                    {
                        System.out.println(part.getContent());
                    }else
                    {
                        InputStream in=part.getInputStream();
                        byte[] date = new byte[in.available()];
                        in.read(date);
                        FileOutputStream out = new FileOutputStream("c://"+fileName);
                        out.write(date);
                        System.out.println("文件 "+fileName +"保存在c://");
                    }
                }
            }
        }
    }
    public class MyAuthenticator extends Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("wpabbs", "邮箱的密码");
        }
    }
}

 

Jmail带附件的邮件发送:
import javax.mail.*;
import java.util.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendMailAddFile {
    public SendMailAddFile() {
    }
    public static void main(String[] args) throws Exception {
        SendMailAddFile send = new SendMailAddFile();
        send.sendFile();
    }
    public void sendFile() throws Exception {
        Properties prop = new Properties();
        prop.setProperty("mail.smtp.host","smtp.sina.com");
        prop.setProperty("mail.smtp.auth","true");
        MyAuthenticator authenticator = new MyAuthenticator();
        Session session = Session.getInstance(prop,authenticator);
        MimeMessage message = new MimeMessage(session);
        Address from = new InternetAddress("[email protected]"); //你的邮箱
        Address to = new InternetAddress("要发送的Email");   //她的邮箱
        message.setFrom(from);
        message.setRecipient(Message.RecipientType.TO,to);
        message.setSubject("o(∩_∩)o...哈哈");
        Multipart mpart = new MimeMultipart();
        MimeBodyPart body = new MimeBodyPart();
        body.setText("测试带附件的邮件发送情况");
        mpart.addBodyPart(body);
        body = new MimeBodyPart();
        DataSource ds = new FileDataSource("c://1.jpg");
        DataHandler dh = new DataHandler(ds);
        body.setDataHandler(dh);
        body.setFileName("readme.jpg");
        mpart.addBodyPart(body);
        message.setContent(mpart);
        Transport trans = session.getTransport("smtp");
        trans.connect("smtp.sina.com","用户名","密码");  //你的用户名及密码
        trans.send(message,message.getAllRecipients());
        trans.close();
        System.out.println("send ok!");
    }
    public class MyAuthenticator extends Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("用户名","密码");  //你的用户名及密码
        }
    }
}



你可能感兴趣的:(mail)