1. 程式人生 > >Java郵件發送

Java郵件發送

net 參數 import username .get 驗證 text imp 連接

需要導入的jar包:mail.jar和activation.jar

587端口:

package mail;

import java.util.Properties;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class MailUtilsQQ587 {

    // 收件人,郵件主題及郵件內容通過參數形式傳遞
    public static void sendMail(String emailAddr,String emailSub,String emailMsg) throws MessagingException {

        // 創建Properties 類用於記錄郵箱的一些屬性
        Properties props = new Properties();

        // 表示SMTP發送郵件,必須進行身份驗證
        props.put("mail.smtp.auth", "true");

        //SMTP服務器
        props.put("mail.smtp.host", "smtp.qq.com");

        //端口號,QQ郵箱給出了兩個端口,465與587
        props.put("mail.smtp.port", 587);

        // 郵箱賬號
        props.put("mail.user", "[email protected]");

        // 郵箱授權碼
        props.put("mail.password", "xxxxxxxxxxxxxxxx");


        // 構建授權信息,用於進行SMTP進行身份驗證
        Authenticator authenticator = new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                String userName = props.getProperty("mail.user");
                String password = props.getProperty("mail.password");
                return new PasswordAuthentication(userName, password);
            }
        };

        // 使用環境屬性和授權信息,創建郵件會話
        Session mailSession = Session.getInstance(props, authenticator);

        // 郵件消息
        MimeMessage message = new MimeMessage(mailSession);

        // 發件人
        InternetAddress form = new InternetAddress(props.getProperty("mail.user"));
        message.setFrom(form);

        // 收件人
        InternetAddress to = new InternetAddress(emailAddr);
        message.setRecipient(MimeMessage.RecipientType.TO, to);

        // 郵件標題
        message.setSubject(emailSub);

        // 郵件內容體
        message.setContent(emailMsg, "text/html;charset=UTF-8");

        // 發送郵件
        Transport.send(message);
    }
}

465端口:

package mail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class MailUtilsQQ465 {
    public static void senMail(String emailAddr, String emailSub,String emailMsg) throws MessagingException {

        Properties properties = new Properties();
        properties.put("mail.transport.protocol", "smtp"); // 連接協議
        properties.put("mail.smtp.host", "smtp.qq.com"); // 主機名
        properties.put("mail.smtp.port", 465);  // 端口號
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.ssl.enable", "true");  // 設置是否使用ssl安全連接 (一般都使用)
        properties.put("mail.debug", "true"); // 設置是否顯示debug信息 true 會在控制臺顯示相關信息

        // 得到回話對象
        Session session = Session.getInstance(properties);

        // 獲取郵件對象
        Message message = new MimeMessage(session);

        // 設置發件人郵箱地址
        message.setFrom(new InternetAddress("[email protected]"));

        // 設置收件人地址
        message.setRecipients( RecipientType.TO, new InternetAddress[] { new InternetAddress(emailAddr) });

        // 設置郵件標題
        message.setSubject(emailSub);

        // 設置郵件內容
        message.setContent(emailMsg,"text/html;Charset=UTF-8");

        // 得到郵差對象
        Transport transport = session.getTransport();

        // 連接郵箱賬戶,密碼為郵箱的授權碼
        transport.connect("[email protected]", "xxxxxxxxxxxxxxxx");

        // 發送郵件
        transport.sendMessage(message, message.getAllRecipients());
    }
}

  

Java郵件發送