1. 程式人生 > >qq和163發郵件工具類

qq和163發郵件工具類

jar下載:mail.jar

package email;

import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class SendEmail {
    
    public static void main(String[] args) throws Exception  {
    	
        String email_receive_qq = "[email protected]";	    //收件人
        String email_send_qq = "
[email protected]
"; //發件人 String email_receive_163 = "[email protected]"; //收件人 String email_send_163 = "[email protected]"; //發件人 //主機名 String host_qq = "smtp.qq.com"; String host_163 = "smtp.163.com"; //授權碼 String pwd_qq = "dthflvqsezbtfjib"; String pwd_163 = "123456abcdef"; //郵件內容 String content = "111"; sendMail(email_send_163,email_receive_163,content,host_163,pwd_163); sendMail(email_send_qq,email_receive_qq,content,host_qq,pwd_qq); } private static void sendMail(String email_send,String email_receive,String content,String host,String pwd) throws MessagingException, AddressException, NoSuchProviderException { Properties properties = new Properties(); properties.put("mail.transport.protocol", "smtp");// 連線協議 properties.put("mail.smtp.host",host); // 主機名 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); // 設定發件人暱稱 String nick=""; try { nick=javax.mail.internet.MimeUtility.encodeText("發件人暱稱"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } message.setFrom(new InternetAddress(nick+" <"+email_send+">")); // 設定收件人郵箱地址 //message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("
[email protected]
"),new InternetAddress("[email protected]"),new InternetAddress("[email protected]")}); message.setRecipient(Message.RecipientType.TO,new InternetAddress(email_receive));//一個收件人 // 設定郵件標題 message.setSubject("郵件標題"); // 設定郵件內容及格式 message.setContent(content,"text/html;charset=utf-8"); // 得到郵差物件 Transport transport = session.getTransport(); // 連線自己的郵箱賬戶 transport.connect(email_send, pwd);// 密碼為郵箱開通的stmp服務後得到的客戶端授權碼 // 傳送郵件 transport.sendMessage(message, message.getAllRecipients()); transport.close(); } }