jar 包: mail-1.4.jar
配置文件:list.properties、mail.properties
list.properties 内容如下:
list=aa@163.com,bb@163.com,cc@163.com
mail.properties内容如下:
mail.protocol=smtp
mail.port=465#mail.host=smtp.qq.com#pe
mail.host=smtp.exmail.qq.com#pe
mail.username=df_service@163.commail.from=df_service@13.commail.password=Pass@2016mail.sslFactory=javax.net.ssl.SSLSocketFactory
案例:
public static void sendMailCash(String orgId, String orgName, String cashName, String totalAmount,
String prewaringValue) throws MessagingException { Properties props = new Properties(); try { props.load(JavaMail.class.getClassLoader() .getResourceAsStream("mail.properties")); } catch (Exception e) { System.err.println("不能读取属性文件. " + "请确保properties在CLASSPATH指定的路径中"); }Properties properties = new Properties();
properties.setProperty("mail.smtp.auth", props.getProperty("mail.auth")); properties.setProperty("mail.transport.protocol", props.getProperty("mail.protocol")); properties.setProperty("mail.smtp.socketFactory.class", props.getProperty("mail.sslFactory")); properties.setProperty("mail.smtp.host", props.getProperty("mail.host")); properties.setProperty("mail.smtp.port", props.getProperty("mail.port")); Authenticator authenticator= new MyAuthenticator(props.getProperty("mail.username"),props.getProperty("mail.password")); Session session = Session .getInstance(properties, authenticator); session.setDebug(false); Message message = new MimeMessage(session); message.setFrom(new InternetAddress(props.getProperty("mail.from")));message.setSubject("ETS互联网支付-头寸预警通知");
message.setRecipients( Message.RecipientType.TO, InternetAddress.parse(ResourceBundle.getBundle("list") .getString("list").trim()));StringBuffer sb = new StringBuffer();
sb.append("ETS系统通知:\n\n\n"); SimpleDateFormat formatter = new SimpleDateFormat( "yyyy年MM月dd日HH时mm分ss秒 "); Date curDate = new Date(System.currentTimeMillis()); String strday = formatter.format(curDate); sb.append("系统于" + strday + "监测到渠道头寸余额预警,信息如下:\n"); sb.append("机构号:" + orgId + "\n"); sb.append("机构名称:" + orgName + "\n"); sb.append("头寸名称:" + cashName + "\n"); sb.append("头寸金额(元):" + totalAmount + "\n"); sb.append("预警值(元):" + prewaringValue + "\n"); sb.append("此头寸余额已达预警值,请知悉!谢谢!" + "\n"); sb.append("-------------------------------------\n"); sb.append("ETS交易控制系统\n"); sb.append("-------------------------------------\n\n\n\n"); sb.append("免责声明:此电子邮件(包括任何附件)可能包含机密信息,仅限邮件上列出的收件人查看。严禁未经授权的披露、传播或使用任何此封电子邮件中所包含的信息。如果您错误地收到这封电子邮件,请通知发件人,并立即删除它。"); message.setText(sb.toString());message.saveChanges();
Transport.send(message); }