java实现邮件发送

邮件发送,如今已经是很普遍了,随便去哪个网站注册一个账号,可能都会用到邮箱接收验证码,那么今天这篇文章将介绍一下:

java如何实现邮件发送。

这里讲的邮件发送是指通过代理进行邮件发送,即通过代理邮件服务器进行邮件地发送,所以,想要发送邮件,我们必须得有个自己的邮箱号。

我用的是新浪的邮箱,登录自己的新浪邮箱后,开启客户端代理服务,具体操作如下:

设置   ==>   客户端pop/imap/smtp   ==>   IMAP4/SMTP服务   ==>  开启

将SMTP服务器地址:smtp.sina.cn 拷贝下来,待会儿要用到。

java实现邮件发送

 

邮箱的设置完成,接下来直接上代码,想要让代码编译通过,我们还需要导一个包:javax.mail,我用的是1.6.0版本的,大家可以在网上找到,下面是核心类代码:

package com.yc;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Mail {
	// 发送者邮箱
	private String myEmailAccount = "";
	// 发件人昵称
	private String sender = "";
	// 发送者邮箱的密码
	private String myEmailpassword = "";
	// 使用的邮箱官方网站
	private String myEmailSMTPhost = "";
	// 收件人
	private String receiveMailAccount = "";
	// 邮件主题
	private String theme = "";
	// 邮件内容
	private String contents = "";

	/**
	 * 无参构造方法
	 */
	public Mail() {
		super();
	}

	/**
	 * 构造方法
	 * 
	 * @param myEmailAccount
	 * @param sender
	 * @param myEmailpassword
	 * @param myEmailSMTPhost
	 * @param receiveMailAccount
	 * @param theme
	 * @param contents
	 */
	public Mail(String myEmailAccount, String sender, String myEmailpassword, String myEmailSMTPhost,
			String receiveMailAccount, String theme, String contents) {
		this.myEmailAccount = myEmailAccount;
		this.sender = sender;
		this.myEmailpassword = myEmailpassword;
		this.myEmailSMTPhost = myEmailSMTPhost;
		this.receiveMailAccount = receiveMailAccount;
		this.theme = theme;
		this.contents = contents;
	}

	// 下面是 get set 方法

	public String getMyEmailAccount() {
		return myEmailAccount;
	}

	public void setMyEmailAccount(String myEmailAccount) {
		this.myEmailAccount = myEmailAccount;
	}

	public String getSender() {
		return sender;
	}

	public void setSender(String sender) {
		this.sender = sender;
	}

	public String getMyEmailpassword() {
		return myEmailpassword;
	}

	public void setMyEmailpassword(String myEmailpassword) {
		this.myEmailpassword = myEmailpassword;
	}

	public String getMyEmailSMTPhost() {
		return myEmailSMTPhost;
	}

	public void setMyEmailSMTPhost(String myEmailSMTPhost) {
		this.myEmailSMTPhost = myEmailSMTPhost;
	}

	public String getReceiveMailAccount() {
		return receiveMailAccount;
	}

	public void setReceiveMailAccount(String receiveMailAccount) {
		this.receiveMailAccount = receiveMailAccount;
	}

	public String getTheme() {
		return theme;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

	public String getContents() {
		return contents;
	}

	public void setContents(String contents) {
		this.contents = contents;
	}

	public void service() throws MessagingException, UnsupportedEncodingException {
		// 1.创建参数设置,用于连接邮件服务器
		Properties props = new Properties();
		props.setProperty("mail.transport.protocol", "smtp");// 使用协议
		props.setProperty("mail.smtp.host", myEmailSMTPhost);
		props.setProperty("mail.smtp.auth", "true");
		// 2.根据配置创建会话对象
		Session session = Session.getInstance(props);
		session.setDebug(true);
		// 3.创建一封信
		MimeMessage message = createMessage(session);
		// 4.根据Session 获取邮件传送对象
		Transport transport = session.getTransport();
		// 5.获取连接
		transport.connect(myEmailAccount, myEmailpassword);
		// 6.发送邮件
		transport.sendMessage(message, message.getAllRecipients());
		// 7.关闭连接
		transport.close();
	}

	private MimeMessage createMessage(Session session) throws UnsupportedEncodingException, MessagingException {
		// 1、创建一封邮件
		MimeMessage message = new MimeMessage(session);
		// 2、From:发件人
		message.setFrom(new InternetAddress(myEmailAccount, sender, "UTF-8"));
		// 3、To:收件人
		message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMailAccount, "XXX", "UTF-8"));
		// 4、Subject:邮件主题
		message.setSubject(theme, "UTF-8");
		// 5、Content:邮件正文
		message.setContent(contents, "text/html;charset=UTF-8");
		// 6、设置发件时间
		message.setSentDate(new Date());
		// 7、保存设置
		message.saveChanges();
		return message;
	}

}

上面这个核心类里面的注释我都写得比较的清晰了,相信大家可以看得很明白,下面我们写一个测试类

java实现邮件发送

运行测试类,没有报错的话,我们的接收者邮箱就会收到我们发的邮件,我将邮件发送到了我的QQ邮箱,如下图:

java实现邮件发送

这样,我们便用java实现了邮件地发送

注意:如果运行没报错,接收者邮箱却没有接收到邮件的情况,可以去接收者邮箱的垃圾箱看看,有可能被当做垃圾邮件处理了。

OK,java实现邮件发送的经验分享到此告一段落,谢谢大家的参阅。

文章属原创,如需引用,请注明出处。