1. 程式人生 > >JavaMail傳送郵箱

JavaMail傳送郵箱

package utils;

import java.security.GeneralSecurityException;
import java.util.Properties;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sun.mail.util.MailSSLSocketFactory;

public class JavaEmail {
	private static Scanner in;
	//測試
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("請輸入收件郵箱:");
		in = new Scanner(System.in);
		String rs = in.nextLine();
		while(!isEmail(rs)){
			System.err.println("你輸入的郵箱不正確!!請重新輸入正確的郵箱地址:");
			rs = in.nextLine();
		}
		sendMail(rs);
	}
    //判斷Email合法性
    public static boolean isEmail(String email) {
        if (email == null)
            return false;
        String rule = "[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?";
        Pattern pattern;
        Matcher matcher;
        pattern = Pattern.compile(rule);
        matcher = pattern.matcher(email);
        if (matcher.matches())
            return true;
        else
            return false;
    }
 /**
     * 傳送郵件工具..
     * @param email
     * @param code
     */
    public static void sendMail(String email){
		//收信人的email
		String to = email;
//		//郵件傳送的email
		String from = "
[email protected]
"; Properties prop = new Properties(); // 開啟debug除錯,以便在控制檯檢視 prop.setProperty("mail.debug", "true"); // 設定郵件伺服器主機名 prop.setProperty("mail.host", "smtp.qq.com"); // 傳送伺服器需要身份驗證 prop.setProperty("mail.smtp.auth", "true"); // 傳送郵件協議名稱 prop.setProperty("mail.transport.protocol", "smtp"); // 開啟SSL加密,否則會失敗 MailSSLSocketFactory sf = null; try { sf = new MailSSLSocketFactory(); } catch (GeneralSecurityException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } sf.setTrustAllHosts(true); prop.put("mail.smtp.ssl.enable", "true"); prop.put("mail.smtp.ssl.socketFactory", sf); Session session = Session.getDefaultInstance(prop,new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { //發件人郵件使用者名稱、密碼 return new PasswordAuthentication(from, "**********");//後面的字元是授權碼,用qq密碼反正我是失敗了(用自己的,別用我的,這個號是我瞎編的,為了。。。。) } }); try { //建立郵件物件 MimeMessage message = new MimeMessage(session); //設定自定義發件人暱稱 String mailName=""; try { mailName=javax.mail.internet.MimeUtility.encodeText("克里斯部落格驗證"); } catch (Exception e) { e.printStackTrace(); } //設定發件者 message.setFrom(new InternetAddress(mailName+"<"+from+">")); //設定收件者 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); //設定郵件主題 message.setSubject("歡迎你註冊吳順東個人部落格!"); //設定郵件的正文 message.setText("這裡是正文"); //傳送郵件 Transport.send(message); System.err.println("郵箱傳送成功...."); } catch (AddressException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("郵箱傳送失敗"); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("郵箱傳送失敗"); } } }