使用 JavaMail 實現郵件發送與收取

已使用 163 郵箱測試經過,且支持 SSL 鏈接。 html

發送郵件

示例:Jack 發送一封郵件給 Rose。 java

public class SendMail {

    public static void main(String[] args) {
        boolean isSSL = true;
        String host = "smtp.163.com";
        int port = 465;
        String from = "jack@163.com";
        String to = "rose@163.com";
        boolean isAuth = true;
        final String username = "jack@163.com";
        final String password = "jack";

        Properties props = new Properties();
        props.put("mail.smtp.ssl.enable", isSSL);
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", port);
        props.put("mail.smtp.auth", isAuth);

        Session session = Session.getDefaultInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

        try {
            Message message = new MimeMessage(session);

            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject("主題");
            message.setText("內容");

            Transport.send(message);
        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }

        System.out.println("發送完畢!");
    }
}

收取郵件

示例:Rose 收取最近一封郵件。 apache

import java.util.Date;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;

public class FetchMail {

    public static void main(String[] args) {
        String protocol = "pop3";
        boolean isSSL = true;
        String host = "pop.163.com";
        int port = 995;
        String username = "rose@163.com";
        String password = "rose";

        Properties props = new Properties();
        props.put("mail.pop3.ssl.enable", isSSL);
        props.put("mail.pop3.host", host);
        props.put("mail.pop3.port", port);

        Session session = Session.getDefaultInstance(props);

        Store store = null;
        Folder folder = null;
        try {
            store = session.getStore(protocol);
            store.connect(username, password);

            folder = store.getFolder("INBOX");
            folder.open(Folder.READ_ONLY);

            int size = folder.getMessageCount();
            Message message = folder.getMessage(size);

            String from = message.getFrom()[0].toString();
            String subject = message.getSubject();
            Date date = message.getSentDate();

            System.out.println("From: " + from);
            System.out.println("Subject: " + subject);
            System.out.println("Date: " + date);
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } finally {
            try {
                if (folder != null) {
                    folder.close(false);
                }
                if (store != null) {
                    store.close();
                }
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }

        System.out.println("接收完畢!");
    }
}

經常使用郵件協議 服務器

發送郵件:SMTP session

收取郵件:POP三、IMAP ide

經常使用郵件配置項

配置項 說明
mail.xxx.ssl.enable
是否支持 SSL 鏈接
mail.xxx.host
郵件服務器主機名
mail.xxx.port
郵件服務器端口號
mail.xxx.auth
是否進行身份認證


說明:xxx 表示協議名稱,例如:smtp、pop3 等。 測試

默認端口號


SMTP
POP3
IMAP
普通方式 25 110 143
SSL 方式 465 995 993

使用 Apache Commons Email 發送郵件

import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

public class SendMail {

    public static void main(String[] args) {
        boolean isSSL = true;
        String host = "smtp.163.com";
        int port = 465;
        String from = "jack@163.com";
        String to = "rose@163.com";
        String username = "jack@163.com";
        String password = "jack";

        try {
            Email email = new SimpleEmail();
            email.setSSLOnConnect(isSSL);
            email.setHostName(host);
            email.setSmtpPort(port);
            email.setAuthentication(username, password);
            email.setFrom(from);
            email.addTo(to);
            email.setSubject("主題");
            email.setMsg("內容");
            email.send();
        } catch (EmailException e) {
            e.printStackTrace();
        }

        System.out.println("發送完畢!");
    }
}

參考:http://commons.apache.org/proper/commons-email/userguide.html ui

spa

相關文章
相關標籤/搜索