Jodd Mail的使用

来源:
三产
最后修订:
2016年07月03日 00:00:00
 878

转载请标明出处:拿客 coderknock.com

Jodd Mail的使用

import jodd.mail.Email;
import jodd.mail.SendMailSession;
import jodd.mail.SmtpServer;

/**
 * 拿客
 * 网站:www.coderknock.com
 * QQ群:21373...

转载请标明出处:拿客 coderknock.com

Jodd Mail的使用

import jodd.mail.Email;
import jodd.mail.SendMailSession;
import jodd.mail.SmtpServer;

/**
 * 拿客
 * 网站:www.coderknock.com
 * QQ群:213732117
 * 三产 创建于 2016年07月02日 22:56:47。
 */
public class EmailTest {
    public static void main(String[] args) {
        SmtpServer smtpServer = SmtpServer.create("smtp.163.com")//邮箱的stmp服务器地址
                .authenticateWith("xxx@xx.xx", "*******************");
        Email emailHtml = Email.create()
                //from必须与上面的邮箱地址相同否则报错:553 Mail from must equal authorized user
                .from("xxx@xx.xx")//发件人
                .to("yyy@yy.yyy")//收件人
                .subject("您好")//主题
                .addHtml("<b>您好</b>123");//HTML格式的内容

        Email emailText = Email.create()
                .from("xxx@xx.xx")
                .to("yyy@yy.yyy")
                .subject("Hello!")
                .addText("一段文本信息...");
        SendMailSession session = smtpServer.createSession();
        session.open();
        session.sendMail(emailHtml);
        session.sendMail(emailText);
        session.close();
    }
}

上面只是一个简单的例子,如果有报错请留意是不是您使用的邮箱服务的垃圾邮件拦截(我用163测试的时候会有这种情况),Jodd Mail还有很多简洁的特性:

  Email email = Email.create()
        .from("...@jodd.org")
        .to("adr1@jodd.org")//发送多人
        .to("adr2@jodd.org")
        .cc("xxx@bar.com")//抄送多人
        .cc("zzz@bar.com")
        .subject("Hello HTML!")
        .addHtml("<b>HTML</b> message")
        .priority(PRIORITY_HIGHEST);//设置邮件的优先级

   Email email = Email.create()
        .from("infoxxxx@jodd.org")
        .to("igorxxxxxx@gmail.com")
        .subject("test6")
        .addText("Hello!")
        .addHtml(
            "<html><META http-equiv=Content-Type content=\"text/html; charset=utf-8\">"+
            "<body><h1>Hey!</h1><img src='cid:c.png'><h2>Hay!</h2></body></html>")
        .embed(attachment().bytes(new File("/c.png")))//添加附件
        .attach(attachment().file("/b.jpg"));//添加附件

接收邮件

  Pop3Server popServer = new Pop3Server("pop3.jodd.org",
            new SimpleAuthenticator("username", "password"));
    ReceiveMailSession session = popServer.createSession();
    session.open();
    //收到的邮件数量
    System.out.println(session.getMessageCount());
    //接收邮件
    ReceivedEmail[] emails = session.receiveEmailAndMarkSeen();
    if (emails != null) {
        for (ReceivedEmail email : emails) {
            System.out.println("\n\n===[" + email.getMessageNumber() + "]===");

            //发送人等相关信息
            Printf.out("%0x", email.getFlags());
            System.out.println("FROM:" + email.getFrom());
            System.out.println("TO:" + email.getTo()[0]);
            System.out.println("SUBJECT:" + email.getSubject());
            System.out.println("PRIORITY:" + email.getPriority());
            System.out.println("SENT DATE:" + email.getSentDate());
            System.out.println("RECEIVED DATE: " + email.getReceiveDate());

            //邮件的相关信息
            List messages = email.getAllMessages();
            for (EmailMessage msg : messages) {
                System.out.println("------");
                System.out.println(msg.getEncoding());
                System.out.println(msg.getMimeType());
                System.out.println(msg.getContent());
            }

            //附件的相关信息
            List<EmailAttachment> attachments = email.getAttachments();
            if (attachments != null) {
                System.out.println("+++++");
                for (EmailAttachment attachment : attachments) {
                    System.out.println("name: " + attachment.getName());
                    System.out.println("cid: " + attachment.getContentId());
                    System.out.println("size: " + attachment.getSize());
                    attachment.writeToFile(
                        new File("d:\\", attachment.getName()));
                }
            }
        }
    }
    session.close();

接收邮件时还可以添加过滤:

  filter()
        .from("from")
        .to("to")
        .or()
        .not()
        .subject("subject")
        .from("from2");