java操作email发送邮件

简单记录一下java如何群发邮箱,首先需要下载一个发送邮件的jar包(email.jar)

下面是一个xml配置

<?xml version="1.0" encoding="UTF-8"?>
<main>
	<config>  
		<protocol>smtp</protocol>
		<host>smtp.163.com</host>
		<port>25</port>
		<email_user_name>java_xx@163.com</email_user_name>
		<email_pass_word>xxx</email_pass_word>
	</config>
</main>

接下来是java代码写法

package com.task.utils;

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

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class EmailUtil {
	private static EmailUtil emailUtil= null;
	
	private static String protocol;
	private static String host;
	private static int port;
	private static String myEmail;
	private static String myEmailPwd;
	private static Element element;
	private EmailUtil(){
		System.out.println("初始化邮件发送配置……");
		SAXReader saxReader = new SAXReader();
		try {
			String path = this.getClass().getResource("/")+"email.config.xml";
			Document document = saxReader.read(path);
			this.element = document.getRootElement();
			
			for (Iterator i = element.elementIterator("config"); i.hasNext();) {  
	            Element foo = (Element) i.next();  
	            this.protocol = foo.elementText("protocol");  
	            this.host = foo.elementText("host");  
	            this.port = Integer.parseInt(foo.elementText("port"));  
	            this.myEmail = foo.elementText("email_user_name");
	            this.myEmailPwd = foo.elementText("email_pass_word");
	        } 
			System.out.println(protocol);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static Session getSession(){
		if(null==emailUtil){
			emailUtil = new EmailUtil();
		}
		Properties props = new Properties();
		props.put("mail.transport.protocol", protocol);
		props.put("mail.smtp.host", host);  
        props.put("mail.smtp.port", port);  
        props.put("mail.smtp.auth", true);
        Session session = Session.getDefaultInstance(props,new Authenticator() {
			@Override
        	protected PasswordAuthentication getPasswordAuthentication(){
        		return new PasswordAuthentication(myEmail, myEmailPwd);  
        	}
        
        });
        
        return session;
	}
	
	public static void send(String[] emailSendAddress,String content,String subject)throws Exception{
		Session session = getSession();
		//Instantiate a message
		Message mimeMessage = new MimeMessage(session);
		//set message attributes
		try {
			mimeMessage.setFrom(new InternetAddress(myEmail));
			InternetAddress[] address = new InternetAddress[emailSendAddress.length];
			for(int i=0;i<emailSendAddress.length;i++){
				address[i] = new InternetAddress(emailSendAddress[i]);
				System.out.println(address[i]);
			}
			mimeMessage.setRecipients(Message.RecipientType.TO, address);
			mimeMessage.setSubject(subject);
			mimeMessage.setSentDate(new Date());	
			mimeMessage.setContent(content,"text/html;charset=utf-8");
			System.out.println(content);
			
			//send the message
			Transport.send(mimeMessage); 
			
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
	public static void main(String arg0[]) throws Exception{
		String[] str= {"3829835@qq.com","8362076@qq.com"};
		send(str, "发送人xxx,测试群发邮箱", "测试");
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值