java mail

1.需要的jar :mail.jar,activation.jarhtml

2.QQ郵箱設置:設置 -賬戶-開啓服務:java

3.注意點: 若用QQ郵箱,有發送次數限制的,發了一次後,就停了。建議用企業郵箱。session

4.javamaildemo點擊打開連接
this

5.相關代碼:server驗證與處理代碼spa

package com.jack.mail;

import java.util.Properties;

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

public class MailVConstructor {
	private Properties props = null;
	private Authenticator authenticator=null; 
	private Session session = null;
	private MimeMessage message = null;
	public  Properties createProperties(String host ){
		Properties p = System.getProperties();;
		p.put("mail.smtp.localhost", "localhost");
		p.put("mail.smtp.auth", "true");/*打開驗證*/
		p.put("mail.smtp.host", host);
		return p;
	}
	 
	public Authenticator crateAuthenticator(String from ,String passWord){
		Authenticator authenticator = new MailAuthenticator(from, passWord);
		return authenticator;
	}
	 
	public Session createSession(Properties props,Authenticator authenticator){
		Session session = Session.getDefaultInstance(props, authenticator);
		return session;
	}
	
	public MimeMessage createMessage(Session session){
		MimeMessage message = new MimeMessage(session);
		return message;
	}
	
	
	/**
	 * 
	 * @param host 發送郵件的server
	 * @param from 發送郵件的地址 ,如 :xxxxxxxx@qq.com
	 * @param pwd  發送郵件的(用戶)password   *************
	 * @param to   接收郵件的地址
	 * @param subject 郵件的標題
	 * @param text 郵件的內容
	 */
  
	public void sendMail(String host,String from,String pwd,String to,String subject,String text){
		props= createProperties(host);
		authenticator = crateAuthenticator(from,pwd);
		session = createSession(props,authenticator);
		message = createMessage(session);
		try {
			message.setFrom(new InternetAddress(from));
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));	
			message.setSubject(subject);/*郵件主題*/
			message.setText(text);/*郵件內容*/
			// 發送message
			Transport.send(message);
			System.out.println("已成功發送給 :"+to);
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}

	}
}

package com.jack.mail;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class MailAuthenticator extends Authenticator {
	String authenName; // username
	String authenPass; // password

	public MailAuthenticator(String authenName, String authenPass) {
		super();
		this.authenName = authenName;
		this.authenPass = authenPass;
	}

	/* 若server需要身份認證,Sission會本身主動調用這種方法 */
	public PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication(authenName, authenPass);
	}
}
測試類

package com.jack.mail;

public class TestMail {
	public static void main(String args[]) {
		String subject = "測試標題";
		String text = "郵件內容";
		MailVConstructor mc = new MailVConstructor();
		mc.sendMail("smtp.exmail.qq.com", "jacklei2@baletu.com", "leixuan1225", "15618222119@qq.com", subject, text);
	}
}
結果:

相關文章
相關標籤/搜索