如何使用Jmail收发邮件

转载自:http://blog.csdn.net/wpabbs/article/details/2458006


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", "wpabbs@sina.com", "邮箱的密码");
        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.163.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("retror@163.com"); // 你的邮箱
		Address to = new InternetAddress("retror@163.com"); // 她的邮箱
		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("E://01.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.163.com", "邮箱名", "邮箱密码"); // 你的用户名及密码
		trans.send(message, message.getAllRecipients());
		trans.close();
		System.out.println("send ok!");
	}

	public class MyAuthenticator extends Authenticator {
		public PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication("邮箱名", "密码"); // 你的用户名及密码
		}
	}
}
效果:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值