java多线程    Java入门    vsftp    ftp    linux配置    centos    FRP教程    HBase    Html5缓存    webp    zabbix    分布式    neo4j图数据库    

javamail发送邮件

JavaMail可以到http://www.oracle.com/technetwork/java/javamail/index-138643.html进行下载

package com.javaer.mail;

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

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

class MyAuthenticatorX extends Authenticator {
	String userName = null;
	String password = null;

	public MyAuthenticatorX() {
	}

	public MyAuthenticatorX(String username, String password) {
		this.userName = username;
		this.password = password;
	}

	protected PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication(userName, password);
	}
}

public class SendMail {

	/**
	 * @param args
	 * @throws MessagingException
	 */
	public static void main(String[] args) throws MessagingException {
		// TODO Auto-generated method stub
		String userName = "gxxxx@163.com";
		String password = "xxxxxxxn";
		String host = "smtp.163.com";
		String to = "275702228@qq.com";
		String port = "25";

		Properties p = new Properties();
		p.setProperty("mail.smtp.host", host);
		p.put("mail.smtp.port", port);
		p.put("mail.smtp.auth", "true");

		MyAuthenticatorX authenticator = null;

		// 如果需要身份认证,则创建一个密码验证器
		authenticator = new MyAuthenticatorX(userName, password);

		// 根据邮件会话属性和密码验证器构造一个发送邮件的session
		Session sendMailSession = Session.getDefaultInstance(p, authenticator);

		System.out.println(sendMailSession);
		try {
			// 根据session创建一个邮件消息
			Message mailMessage = new MimeMessage(sendMailSession);
			// 创建邮件发送者地址
			Address from = new InternetAddress(userName);
			// 设置邮件消息的发送者
			mailMessage.setFrom(from);
			// 创建邮件的接收者地址,并设置到邮件消息中
			Address to1 = new InternetAddress(to);
			mailMessage.setRecipient(Message.RecipientType.TO, to1);
			// 设置邮件消息的主题
			mailMessage.setSubject("一份普通的java邮件发送");
			// 设置邮件消息发送的时间
			mailMessage.setSentDate(new Date());
			// 设置邮件消息的主要内容
			mailMessage
					.setText("欢迎访问月小升的技术博客");

			// -------此部分删除即为普通文本发送------------------------------------------------------------------------------
			// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
			Multipart mainPart = new MimeMultipart();
			// 创建一个包含HTML内容的MimeBodyPart
			BodyPart html = new MimeBodyPart();
			// 设置HTML内容
			html.setContent("欢迎访问月小升的技术博客",
					"text/html; charset=utf-8");
			mainPart.addBodyPart(html);
			// 将MiniMultipart对象设置为邮件内容
			mailMessage.setContent(mainPart);
			// -------------------------------------------------------------------------------------

			// 发送邮件
			Transport.send(mailMessage);

		} catch (MessagingException ex) {
			ex.printStackTrace();
		}

	}

}

如果采用163邮件,发送人和验证登陆的账户必须一致。


This entry was posted in JAVA and tagged , . Bookmark the permalink.
月小升QQ 2651044202, 技术交流QQ群 178491360
首发地址:月小升博客https://java-er.com/blog/javamail-java-send-mail/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
您的评论是我写作的动力.

Leave a Reply