javaMail入门

利用java发送邮件,其实很方便。

1.首先下载jar包,http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-eeplat-419426.html#javamail-1.4.5-oth-JPR  官方网站,没得说,下载后解压,有个mail.jar,暂且只需要它。

2.入门程序:

 

(1).首先需要一个继承自Authenticator的类

 

package com.mail;

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

public class MyAutherticator extends Authenticator {
	private String username = "[email protected]";
	private String password = "xxx";

	public MyAutherticator() {
		super();
	}

	public MyAutherticator(String user, String pwd) {
		super();
		username = user;
		password = pwd;
	}

	public PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication(username, password);
	}
}

 

 (2).其次需要一个属性配置类

 

package com.mail;

import java.util.Properties;

public class MyProperties {
	private static String host = "smtp.qq.com";
	private static Properties properties = null;
	
	public static Properties getPro(){
		properties  = System.getProperties();// 获取系统环境
		properties.put("mail.smtp.host", host);
		properties.put("mail.smtp.auth", "true");
		
		return properties;
	}
}
 

(3).需要一个邮件发送类

 

package com.mail;

import java.util.Date;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MyMail {
	private String mail_head_name = "this is head of this mail";
	private String mail_head_value = "this is head of this mail";
	private String mail_to = "[email protected]";                         
	private String mail_from = "[email protected]";
	private String mail_subject = "this is the subject of this test mail";
	private String mail_body = "this is mail_body of this test mail";
	private String personalName = "我的邮件";
	
	public MimeMessage getMail(Session session)throws Exception{
		MimeMessage message = new MimeMessage(session);                     //邮件发送对象
		message.setContent("Hello", "text/plain");
		message.setSubject(mail_subject);			                        // 设置邮件主题
		message.setText(mail_body);                                         // 设置邮件内容
		message.setHeader(mail_head_name, mail_head_value);                 // 设置邮件标题
		message.setSentDate(new Date());                                    // 设置邮件发送时期
		Address address = new InternetAddress(mail_from, personalName);
		message.setFrom(address);                                           // 设置邮件发送者的地址
		Address toaddress = new InternetAddress(mail_to);
		message.addRecipient(Message.RecipientType.TO, toaddress);          // 设置邮件接收者的地址
		
		return message;
	}
}
 

 

(4).万事俱备,开始执行

 

package com.mail;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;

public class SendMail {
	
	public void sendMail() throws SendFailedException {
		try {
			Authenticator auth = new MyAutherticator();              //1--进行邮件服务用户认证
			Properties pros = MyProperties.getPro();                 //2--属性配置
			Session session = Session.getDefaultInstance(pros, auth);//3--设置session,和邮件服务器进行通讯
			MyMail mail = new MyMail();
			MimeMessage message = mail.getMail(session);             //4--设置一个邮件
			Transport.send(message);                                 //5--发送
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		SendMail mail = new SendMail();
		try {
			mail.sendMail();
		} catch (Exception e) {
			System.out.println(e);
		}
	}
}

 

 

 

后记:

1.发送邮件地址,以及密码,设置成自己的即可,接收邮件地址,自己看着办。

2.此程序只用来入门用,更多例子可以查看解压缩后的demo例子。但推荐使用javaMail框架。

你可能感兴趣的:(java,javamail)