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("邮箱发送失败");
		}
	}
}

  

猜你喜欢

转载自www.cnblogs.com/shundong106/p/9791371.html