标签:style blog color os java 使用 io ar 文件
package com.future.portal.util; import java.io.IOException; import java.util.Date; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.NoSuchProviderException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import org.apache.log4j.Logger; public class MailSender { final static Logger logger = Logger.getLogger(MailSender.class); /** * 发送简单邮件 * @param str_from:发件人地址 * @param str_to:收件人地址 * @param str_title:邮件标题 * @param str_content:邮件正文 * @param emailPwd:邮件密码 */ @SuppressWarnings("finally") public static void send(String str_to, String str_title,String str_content){ boolean flog=false; logger.info("sending... To: " + str_to + " Mail Title: " + str_title + " fileAttachment: " + str_content); Properties props = new Properties(); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); try { props.load(classLoader.getResourceAsStream("mail.properties")); //使用property读取文件 } catch (IOException e) { e.printStackTrace(); } String str_from = props.getProperty("mail.from"); String emailPwd = props.getProperty("mail.password");
final String mailServer_163 = props.getProperty("mail.smtp.163.mailServer"); final String mailServer_qq = props.getProperty("mail.smtp.qq.mailServer"); final String mailServer_Sina = props.getProperty("mail.smtp.sina.mailServer");
String mailServer = ""; String emailType = Tools.subString(str_from, "@"); //截取@前 String eName = Tools.subberString(str_from,"@");
if (emailType.trim() != "" && emailType.equals("qq.com")) { mailServer = mailServer_qq; } if (emailType.trim() != "" && emailType.equals("163.com")) { mailServer = mailServer_163; } if (emailType.trim() != "" && emailType.equals("sina.com.cn")) { mailServer = mailServer_Sina; } if (emailType.trim() != "" && emailType.equals("sina.com")) { mailServer = mailServer_Sina; } // 存储发送邮件服务器的信息 props.put("mail.smtp.host", mailServer); // 同时通过验证 props.put("mail.smtp.auth", "true"); // 根据属性新建一个邮件会话 Session s = Session.getInstance(props); s.setDebug(true); // 有他会打印一些调试信息。 // 由邮件会话新建一个消息对象 MimeMessage message = new MimeMessage(s); // 设置邮件 InternetAddress from = null; try { from = new InternetAddress(str_from); } catch (AddressException e) { // TODO Auto-generated catch block e.printStackTrace(); } // pukeyouxintest2@163.com try { message.setFrom(from); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 设置发件人的地址 // //设置收件人,并设置其接收类型为TO InternetAddress to = null; try { to = new InternetAddress(str_to); } catch (AddressException e) { e.printStackTrace(); } try { message.setRecipient(Message.RecipientType.TO, to); // 设置标题 message.setSubject(str_title); // java学习 // 设置信件内容 // message.setText(str_content); //发送文本邮件 //你好吗? message.setContent(str_content, "text/html;charset=gbk"); // 发送HTML邮件 // 设置发信时间 message.setSentDate(new Date()); // 存储邮件信息 message.saveChanges(); } catch (MessagingException e) { e.printStackTrace(); } // 发送邮件 Transport transport = null; try { transport = s.getTransport("smtp"); } catch (NoSuchProviderException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 以smtp方式登录邮箱,第一个参数是发送邮件用的邮件服务器SMTP地址,第二个参数为用户名,第三个参数为密码 try { transport.connect(mailServer, eName, emailPwd) ; } catch (MessagingException e) { e.printStackTrace(); } // 发送邮件,其中第二个参数是所有已设好的收件人地址 try { transport.sendMessage(message, message.getAllRecipients()); } catch (MessagingException e) { e.printStackTrace(); } try { transport.close(); } catch (MessagingException e) { e.printStackTrace(); } } /** * 异步发送邮件的方法*/ public static void sendMailByAsynchronous(final String str_to, final String str_title, final String str_content) throws Exception{ new Thread(new Runnable() { public void run() { try { MailSender.send(str_to, str_title, str_content); } catch (Exception ex) { logger.error("mail sender error To: " + str_to + " Mail Title: " + str_title + " fileAttachment: " + str_content, ex); } } }).start(); } }
mail.smtp.163.mailServer=smtp.163.com mail.smtp.qq.mailServer=smtp.qq.com mail.smtp.sina.mailServer=smtp.sina.com.cn mail.username=asd mail.password=123456 mail.from=asd@163.com
标签:style blog color os java 使用 io ar 文件
原文地址:http://www.cnblogs.com/estellez/p/3940052.html