欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java mail

程序员文章站 2022-07-13 16:44:47
...

一、java Mail 简单案例  学习笔记

基于Maven前期准备(这里使用1.4.7为例)

<dependency>
	<groupId>javax.mail</groupId>
	<artifactId>mail</artifactId>
	<version>1.4.7</version>
</dependency>

 

1、发送邮件代码如下所示

package com.xxxxx.mp2.utils;

import java.io.File;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
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;


public class SendEmail {
	
    public static void main(String[] args) {
          new SendEmail().sendEmails("xxxxxs@xxx.com");
    }
 
    /**
      * 
      * @param email 接收方 邮件地址
      */
    public void  sendEmails(String email){
	try {
		//定义电子邮件
		MimeMessage msg = nakeMail("标题","内容",new InternetAddress(email));
		//发送电子邮件
		Transport.send(msg);
	} catch (Exception e) {
		e.printStackTrace();
	}
    }

	/**
	 * 
	 * @param title 邮件标题
	 * @param content 邮件内容
	 * @param email to EmailAddress 邮件接收方 数组
	 * @return
	 */
	private  MimeMessage nakeMail(String title,String content,InternetAddress ... email) throws  Exception {
	    //定义发送邮件的属性信息
		Properties prop = new Properties();
	    //使用发送邮件的服务器 常见的有 smtp.163.com    smtp.21cn.com smtp.sina.com.cn smtp.sohu.com smtp.126.com
	    prop.put("mail.smtp.host", "smtp.xxxxxx.com");
	    //通知邮件服务器为此次访问授权
	    prop.put("mail.smtp.auth", "true");
	    //创建会话 java 详见: http://haolloyin.blog.51cto.com/1177454/354585
	    Session session = Session.getDefaultInstance(prop, new Authenticator(){
	    	 @Override
	         protected PasswordAuthentication getPasswordAuthentication() {
	    		 //form email Address and password
	             return new PasswordAuthentication("邮箱发送方用户名","邮箱密码");
	         }
	    });
	    //设置为调试会话   ----在运行过程中会将发送邮件的实时日志打印到控制台
	    session.setDebug(true);
	    //电子邮件汇总信息
	    MimeMessage message = new MimeMessage(session);
	    //邮件发送方
	    message.setFrom(new InternetAddress("邮箱用户名"));
	    
	    message.setRecipients(Message.RecipientType.TO, email);
	    //邮件标题
	    message.setSubject(title);
	    //邮件内容
	    message.setText(content, null, "html");
	    //查找附件是否存在
	    File file = new File("附件地址");
	    if(file.exists()){
	    	 //邮件正文对象-定义一个空的邮件正文对象
	    	 MimeMultipart allPart = new MimeMultipart("mixed"); 
	    	 //定义附件存放容器对象--定义一个空的附件存放容器
	         MimeBodyPart attachmentPart1 = new MimeBodyPart();
	         MimeBodyPart attachmentPart2 = new MimeBodyPart();
	         //读取附件信息
	         FileDataSource fds1 = new FileDataSource("附件地址");
	         FileDataSource fds2 = new FileDataSource("附件地址");
	         //将附件信息存入到空容器中
	         attachmentPart1.setDataHandler(new DataHandler(fds1));
	         attachmentPart2.setDataHandler(new DataHandler(fds2));
	         //附件名称
	         attachmentPart1.setFileName(fds1.getName());  
	         attachmentPart2.setFileName(fds2.getName());  
	         //将附件容器对象存放到邮件正文中
	         allPart.addBodyPart(attachmentPart1);  
	         allPart.addBodyPart(attachmentPart2);  
	         //将新的正文叠加到电子邮件中
	         message.setContent(allPart);
	         //将修改同步到电子邮件中--刷新同步
	         message.saveChanges();  
	    }
	    return message;
	}

}

 

 

2、常遇问题

1)、未加授权,以下授权代码未加

 prop.put("mail.smtp.auth", "true");

错误内容如下:

javax.mail.MessagingException: Can't send command to SMTP host;
  nested exception is:
 java.net.SocketException: Software caused connection abort: socket write error

2)、邮件内容包含链接地址时,为个别邮箱发送邮件时,需要在邮件内容或邮件标题中加上"邮件验证"四个字,否则个别邮箱始终无法收到邮件(qq)。