java邮件发送

java邮件发送需要2个依赖包

1.javax.mail.jar:http://www.oracle.com/technetwork/java/javamail/index-138643.html

2.activation.jar:http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-java-plat-419418.html#jaf-1.1.1-fcs-oth-JPR

注意:QQ邮箱需要SSL验证

在QQ邮箱设置——》账户——》开启服务——》生成授权码



文件结构如下:



代码如下:

public class JavaMail {
	// 收件人电子邮箱(请自行修改)
	private String to = "1404510094@qq.com";
	// 发件人电子邮箱(请自行修改)
	private String from = "chengbg1997@qq.com";
	// 指定发送邮件的主机
	private String host = "smtp.qq.com";
	// 端口号(这是QQSMTP服务的默认端口)
	private int port = 465;
	// 用户名(邮箱账号)
	private String userName = "xxxxxxxxxx@qq.com";
	// SSL授权码(在QQ邮箱中获取)
	private String password = "xxxxxxxxxxxxxxxxx";

	/**
	 * 发送文本邮件
	 * 
	 */
	public void sendTextMail() throws GeneralSecurityException, MessagingException {
		Properties properties = System.getProperties();
		//SSL证书授权
		MailSSLSocketFactory ssl = new MailSSLSocketFactory();
		ssl.setTrustAllHosts(true);
		properties.put("mail.smtp.ssl.enable", "true");
		properties.put("mail.smtp.ssl.socketFactory", ssl);
		properties.put("mail.smtp.host", host);
		properties.put("mail.smtp.port", port);
		properties.put("mail.smtp.auth", "true");

		// 根据邮件会话属性和密码验证器构造一个发送邮件的session
		Session session = Session.getDefaultInstance(properties, new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(userName, password);
			}
		});
		// 开启debug模式
		session.setDebug(true);
		// 根据session创建一个邮件消息
		Message mailMessage = new MimeMessage(session);
		// 设置邮件消息的发送者
		mailMessage.setFrom(new InternetAddress(userName));
		// 创建邮件的接收者地址,并设置到邮件消息中
		mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
		// 设置邮件消息的主题
		mailMessage.setSubject("邮件测试");
		// 设置邮件消息发送的时间
		mailMessage.setSentDate(new Date());
		// 设置邮件消息的主要内容
		mailMessage.setText("测试内容");
		// 发送邮件
		Transport.send(mailMessage);
	}
	
	/**
	 * 发送HTML邮件
	 * 
	 */
	public void sendHtmlMail() throws GeneralSecurityException, MessagingException {
		Properties properties = System.getProperties();
		MailSSLSocketFactory ssl = new MailSSLSocketFactory();
		ssl.setTrustAllHosts(true);
		properties.put("mail.smtp.ssl.enable", "true");
		properties.put("mail.smtp.ssl.socketFactory", ssl);
		properties.put("mail.smtp.host", host);
		properties.put("mail.smtp.port", port);
		properties.put("mail.smtp.auth", "true");

		// 根据邮件会话属性和密码验证器构造一个发送邮件的session
		Session session = Session.getDefaultInstance(properties, new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(userName, password);
			}
		});
		// 开启debug模式
		session.setDebug(true);
		// 根据session创建一个邮件消息
		Message mailMessage = new MimeMessage(session);
		// 设置邮件消息的发送者
		mailMessage.setFrom(new InternetAddress(userName));
		// 创建邮件的接收者地址,并设置到邮件消息中
		mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
		// 设置邮件消息的主题
		mailMessage.setSubject("邮件测试");
		// 设置邮件消息发送的时间
		mailMessage.setSentDate(new Date());
		// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
		Multipart mainPart = new MimeMultipart();
		// 创建一个包含HTML内容的MimeBodyPart
		BodyPart html = new MimeBodyPart();
		// 设置HTML内容
		html.setContent("<html><body><a href='http://blog.csdn.net/qq1404510094/article/details/79646707'/>java邮件发送实现</a></body></html>",
				"text/html; charset=utf-8");
		mainPart.addBodyPart(html);
		// 将MiniMultipart对象设置为邮件内容
		mailMessage.setContent(mainPart);
		// 发送邮件
		Transport.send(mailMessage);
	}
	
	
	/**
	 * 发送附件邮件
	 * 
	 */
	public void sendAttachmentMail() throws GeneralSecurityException, MessagingException, UnsupportedEncodingException {
		Properties properties = System.getProperties();
		MailSSLSocketFactory ssl = new MailSSLSocketFactory();
		ssl.setTrustAllHosts(true);
		properties.put("mail.smtp.ssl.enable", "true");
		properties.put("mail.smtp.ssl.socketFactory", ssl);
		properties.put("mail.smtp.host", host);
		properties.put("mail.smtp.port", port);
		properties.put("mail.smtp.auth", "true");

		// 根据邮件会话属性和密码验证器构造一个发送邮件的session
		Session session = Session.getDefaultInstance(properties, new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(userName, password);
			}
		});
		// 开启debug模式
		session.setDebug(true);
		// 根据session创建一个邮件消息
		Message mailMessage = new MimeMessage(session);
		// 设置邮件消息的发送者
		mailMessage.setFrom(new InternetAddress(userName));
		// 创建邮件的接收者地址,并设置到邮件消息中
		mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
		// 设置邮件消息的主题
		mailMessage.setSubject("邮件测试");
		// 设置邮件消息发送的时间
		mailMessage.setSentDate(new Date());
		// MiniMultipart类是一个容器类,包含一个或多个MimeBodyPart类型的对象,mixed为附件模式
		MimeMultipart multipart = new MimeMultipart("mixed");

		// 创建一个包含附件内容的MimeBodyPart
		MimeBodyPart imageBodyPart = new MimeBodyPart();
		// 获取文件路径
		String filePath = JavaMail.class.getResource("/images/img.jpg").getPath();
		File file = new File(filePath);
		DataSource dataSource = new FileDataSource(file);
		// 设置文件
		imageBodyPart.setDataHandler(new DataHandler(dataSource));
		// 设置文件名
		imageBodyPart.setFileName(MimeUtility.encodeWord(file.getName()));
		multipart.addBodyPart(imageBodyPart);
		mailMessage.setContent(multipart);
		// 发送邮件
		Transport.send(mailMessage);
	}
	
	
	/**
	 * 发送内嵌邮件+附件
	 * 
	 */
	public void sendRelatedMail() throws GeneralSecurityException, MessagingException, UnsupportedEncodingException {
		Properties properties = System.getProperties();
		MailSSLSocketFactory ssl = new MailSSLSocketFactory();
		ssl.setTrustAllHosts(true);
		properties.put("mail.smtp.ssl.enable", "true");
		properties.put("mail.smtp.ssl.socketFactory", ssl);
		properties.put("mail.smtp.host", host);
		properties.put("mail.smtp.port", port);
		properties.put("mail.smtp.auth", "true");
		
		// 根据邮件会话属性和密码验证器构造一个发送邮件的session
		Session session = Session.getDefaultInstance(properties, new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(userName, password);
			}
		});
		// 开启debug模式
		session.setDebug(true);
		// 根据session创建一个邮件消息
		Message mailMessage = new MimeMessage(session);
		// 设置邮件消息的发送者
		mailMessage.setFrom(new InternetAddress(userName));
		// 创建邮件的接收者地址,并设置到邮件消息中
		mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
		// 设置邮件消息的主题
		mailMessage.setSubject("邮件测试");
		// 设置邮件消息发送的时间
		mailMessage.setSentDate(new Date());
		
		// 创建一个包含附件内容的MimeBodyPart
		MimeBodyPart imageBodyPart = new MimeBodyPart();
		// 获取文件路径
		String filePath = JavaMail.class.getResource("/images/img.jpg").getPath();
		File file = new File(filePath);
		DataSource dataSource = new FileDataSource(file);
		// 设置文件
		imageBodyPart.setDataHandler(new DataHandler(dataSource));
		// 设置文件名
		imageBodyPart.setFileName(MimeUtility.encodeWord(file.getName()));
		
		// 创建一个包含图片MimeBodyPart
		MimeBodyPart image = new MimeBodyPart();
		image.setDataHandler(new DataHandler(dataSource));
		// 设置图片ID
		image.setContentID("headImg");
		
		// 创建一个包含HTML内容的MimeBodyPart
		MimeBodyPart html = new MimeBodyPart();
		// 设置HTML内容
		html.setContent("<html><body><a href='http://blog.csdn.net/qq1404510094/article/details/79646707'/>java邮件发送实现</a></body></html>",
				"text/html; charset=utf-8");
		
		// 创建一个包含文字和图片内容的MimeBodyPart
		MimeBodyPart text = new MimeBodyPart();
		//内嵌文字和图片
		text.setContent("我的头像:<img src='cid:headImg'>", "text/html;charset=UTF-8");
		
		// MiniMultipart类是一个容器类,包含一个或多个MimeBodyPart类型的对象
		MimeMultipart multipart = new MimeMultipart("related");
		// 添加内容
		multipart.addBodyPart(text);
		multipart.addBodyPart(image);
		multipart.addBodyPart(html);
		
		MimeBodyPart content = new MimeBodyPart();
		//设置主体内容
		content.setContent(multipart);
		
		// 创建MiniMultipart包含整个资源
		MimeMultipart message = new MimeMultipart("mixed");
		//添加主体内容
		message.addBodyPart(content);
		//添加图片附件
		message.addBodyPart(imageBodyPart);
		
		// 将MiniMultipart对象设置为邮件内容
		mailMessage.setContent(message);
		// 发送邮件
		Transport.send(mailMessage);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值