使用Java 发送电子邮件

一个采用sun的类库的java发送邮件的demo

因为使用的是Gmail 所以有所不同 Gmail的端口是465 而其他邮箱一般是25 而且Gmail还要用到SSL的一些东西

package send.mail.test;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.net.ssl.SSLSocketFactory;
import java.security.Security;

public class TestSendEmail {

	public static void main(String[] args) {
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
		EmailBase base = new EmailBase("smtp.gmail.com", "namezhou#gmail.com",
				"********");
		base.setDebug(true);
		base.setPort(465);
		if (EmailSendManager.sendMail("namezhou#gmail.com", "title", "content",
				base))
			System.err.println("发送成功!");
		else
			System.err.println("发送失败");
	}

}

/**
 * 邮件发送功能实现
 */
class EmailSendManager {

	/**
	 * 发送邮件方法
	 * 
	 * @param toMail
	 *            邮件地址
	 * @param toTitle
	 *            标题名称
	 * @param toContent
	 *            邮件内容
	 * @return boolean 是否成功
	 */
	public static boolean sendMail(String toMail, String toTitle,
			String toContent, EmailBase base) {
		try {
			Properties props = base.getEmailProperties();
			// 根据属性新建一个邮件会话
			Session session = Session.getInstance(props);
			session.setDebug(base.isDebug());
			// 由邮件会话新建一个消息对象
			Message message = new MimeMessage(session);

			// 设置邮件
			InternetAddress from = new InternetAddress(base.getFromAddress());
			// 设置发件人
			message.setFrom(from);
			InternetAddress toWhere = new InternetAddress(toMail);
			// 设置收件人,并设置其接收类型为TO
			message.setRecipient(Message.RecipientType.TO, toWhere);
			// 设置主题
			message.setSubject(toTitle);
			// 设置信件内容
			message.setText(toContent);
			// 设置发信时间
			message.setSentDate(new Date());
			// 发送邮件并存储邮件信息
			message.saveChanges();

			Transport transport = session.getTransport("smtp");
			// 以smtp方式登录邮箱,第一个参数是发送邮件用的邮件服务器SMTP地址,第二个参数为用户名,第三个参数为密码
			transport.connect(base.getSmtp(), base.getFromAddress(), base
					.getPassword());
			// 发送邮件,其中第二个参数是所有已设好的收件人地址
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();
			return true;
		} catch (MessagingException e) {
			e.printStackTrace();
			return false;
		}
	}
}

/**
 * 把发邮件所需的信息封装成一个类
 * 
 * @author Ericzhou
 * 
 */
class EmailBase {
	public EmailBase(String smtp, String from, String password) {
		this.emailProperties = new Properties();
		this.smtp = smtp;
		this.fromAddress = from;
		this.password = password;
		this.emailProperties.put("mail.smtp.host", this.smtp);
		this.emailProperties.put("mail.smtp.port", this.port);
		this.emailProperties.put("mail.smtp.auth", this.auth);
		this.emailProperties.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		this.emailProperties.put("mail.smtp.socketFactory.fallback", "false");
		this.emailProperties.put("mail.smtp.socketFactory.port", "465");
	}

	private Properties emailProperties;
	private String smtp;
	private int port = 25;
	private String fromAddress;
	private boolean auth = true;
	private String password;
	private boolean debug = false;

	public boolean isDebug() {
		return debug;
	}

	public void setDebug(boolean debug) {
		this.debug = debug;
	}

	public Properties getEmailProperties() {
		return emailProperties;
	}

	public void setEmailProperties(Properties emailProperties) {
		this.emailProperties = emailProperties;
	}

	public String getSmtp() {
		return smtp;
	}

	public void setSmtp(String smtp) {
		this.smtp = smtp;
		this.emailProperties.setProperty("mail.smtp.host", this.smtp);
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
		this.emailProperties.setProperty("mail.smtp.port", String
				.valueOf(this.port));
	}

	public String getFromAddress() {
		return fromAddress;
	}

	public void setFromAddress(String fromAddress) {
		this.fromAddress = fromAddress;
	}

	public boolean isAuth() {
		return auth;
	}

	public void setAuth(boolean auth) {
		this.auth = auth;
		this.emailProperties.setProperty("mail.smtp.auth", String
				.valueOf(this.auth));
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
		this.emailProperties.setProperty("", this.password);
	}

}

 

你可能感兴趣的:(java,.net,Security,sun,Gmail)