java发送 email
public class EmailUtils implements IAction { private static Logger logger = Logger.getLogger(EmailUtils.class.getName()); private static EmailUtils instance; //邮件服务器 private static String EMAIL_HOST = "mail.host"; //邮件简单协议 private static String MAIL_TRANSPORT_PROTOCOL = "mail.transport.protocol"; //是否开启验证,必须能为true private static boolean MAIL_SMTP_AUTH_VALUE = true; //是否开启验证 private static String MAIL_SMTP_AUTH = "mail.smtp.auth"; private static String EAIL_FORMATTER = "text/html;charset=utf-8"; private static String MAIL_SMTP_PORT = "mail.smtp.port"; Map<String,String> map = null; List<String> receiverList; public static EmailUtils getInstance() throws BaseAppException { if(null == instance){ instance = new EmailUtils(); } return instance; } public EmailUtils() throws BaseAppException { logger.info("get email info begain....."); //初始化邮件系统参数系统 Base base = new Base(new DynamicDict().getConnection()); String sql = "SELECT PARA_SUB_CODE,PARA_VALUE FROM TWF_SYSTEMPARAMETER WHERE PARA_CATEGORY =15 ORDER BY SEQ ASC"; DynamicDict dict = base.query("get email info", "EMAIL", sql,new String[]{}); map = new HashMap<String,String>(); // properties = new Properties(); if(dict.getCount("EMAIL")>0){ for (int i = 0; i < dict.getCount("EMAIL"); i++) { Map<String,String> map_value = new HashMap<String,String>(); map_value = (Map<String, String>)dict.getValueByName("EMAIL",i); map.put(map_value.get("PARA_CODE"), map_value.get("PARA_VALUE")); } } //获取邮件的接受者 String rSql = "SELECT PARA_SUB_CODE,PARA_VALUE FROM TWF_SYSTEMPARAMETER WHERE PARA_CATEGORY =16 ORDER BY SEQ ASC"; //Base rBase = new Base(new DynamicDict().getConnection()); receiverList = new LinkedList<String>(); DynamicDict receiverDict = base.query("get email receiver", "EMAIL_RECEIVER", rSql,new String[]{}); if(receiverDict.getCount("EMAIL_RECEIVER")>0){ for (int i = 0; i < receiverDict.getCount("EMAIL_RECEIVER"); i++) { Map<String,String> map_value = new HashMap<String,String>(); map_value = (Map<String, String>)receiverDict.getValueByName("EMAIL_RECEIVER",i); receiverList.add(map_value.get("PARA_VALUE")); } } } @Override public int perform(DynamicDict dict) throws BaseAppException { //判断工单状态和等级 String level_order = dict.getString("WORK_ORDER_LEVEL"); String state_order = dict.getString("WORK_ORDER_STATE"); //String receiver = dict.getString("STAFF_EMAIL"); Properties prop=new Properties(); //邮件主题、内容、发送者、密码、工单等级、状态 String subject = "",content = "", sender = "",password = "" ,level = "", state = ""; prop.put(MAIL_SMTP_AUTH, MAIL_SMTP_AUTH_VALUE); logger.info("get email config info map :"+map); if(!CollectionUtils.isEmpty(map)){ //邮件服务器 prop.put(EMAIL_HOST, map.get("FOS_EMAIL_HOST")); //邮件简单协议 prop.put(MAIL_TRANSPORT_PROTOCOL, map.get("FOS_EMAIL_TRANSPORT_PROTOCOL")); //邮件端口 prop.put(MAIL_SMTP_PORT, map.get("FOS_EMAIL_PORT")); //密码(授权码非邮件用户登陆密码) password = map.get("FOS_EMAIL_SENDER_PASSWORD"); //邮件主题 subject = map.get("FOS_EMAIL_TITLE"); //邮件内容 content = map.get("FOS_EMAIL_CONTENT").replace("$(staffname)", dict.getString("STAFF_NAME")).replace("$(workorderno)", dict.getString("WORK_ORDER_NO")); //用户名 sender = map.get("FOS_EMAIL_SENDER_USERNAME"); //工单状态 state = map.get("FOS_EMAIL_ORDER_STATE"); //工单等级 level = map.get("FOS_EMAIL_ORDER_LEVEL"); } // if(level_order.equals(level) && state_order.equals(state)){ try { for (String receiver : receiverList) { sendEamil(prop, subject, content, sender, password, receiver); } } catch (MessagingException e) { logger.error("send email fail caused by :"+e); throw new BaseAppException("send email fail caused by :"+e); } //} return 0; } //发送邮件 public void sendEamil (Properties prop,String subject,String content,String sender,String password,String receiver) throws MessagingException{ System.out.println("prop..."+prop); //使用java发送邮件5步骤 //1.创建sesssion Session session=Session.getInstance(prop); //开启session的调试模式,可以查看当前邮件发送状态 session.setDebug(true); //2.通过session获取Transport对象(发送邮件的核心API) Transport ts=session.getTransport(); System.out.println(ts); //3.通过邮件用户名密码链接 ts.connect(sender, password); //4.创建邮件 Message msg=createSimpleMail(session,subject,content,sender,receiver) ; //5.发送电子邮件 ts.sendMessage(msg, msg.getAllRecipients()); } //創建一封郵件 public static MimeMessage createSimpleMail(Session session,String subject,String content,String sender,String receiver) throws AddressException,MessagingException{ //创建邮件对象 MimeMessage mm=new MimeMessage(session); //设置发件人 mm.setFrom(new InternetAddress(sender)); //设置收件人 mm.setRecipient(Message.RecipientType.TO, new InternetAddress(receiver)); // //设置抄送人 // mm.setRecipient(Message.RecipientType.CC, new InternetAddress("632132852@qq.com")); mm.setSubject(subject); mm.setContent(content, EAIL_FORMATTER); return mm; } public static void main(String[] args) throws MessagingException, BaseAppException { Properties localProperties = System.getProperties(); localProperties.setProperty("ZSMART_HOME", "F:\\zsmart"); DynamicDict dict = new DynamicDict(); dict.setValueByName("STAFF_NAME", "wudi"); dict.setValueByName("WORK_ORDER_NO", "123456"); //dict.setValueByName("STAFF_EMAIL", "wudi@redoornetwork.com"); //dict.setValueByName("WORK_ORDER_LEVEL", "A"); //dict.setValueByName("WORK_ORDER_STATE", "0"); System.out.println(FOSCommonCache.getInstance().getListByCode("FOS_EMAIL")); // EmailUtils.getInstance().perform(dict); } }
上述代码中夹杂了具体业务中的代码,没有用的可以自行删除,发送邮件比较简单。
还有就是需要mail.jar文件。 点此下载