简单的邮件发送mail.jar
2014-08-27 18:02
197 查看
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.properties
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
Tool.java
public class Tools { public static String subString(String content,String start){ String subConetent=content.substring(content.indexOf(start)+1, content.length()); return subConetent; } public static String subberString(String content,String end){ String subConetent=content.substring(0,content.indexOf(end)); return subConetent; } }
调用方法:
MailSender.sendMailByAsynchronous("123@126.com","topic:"+a.getAskTitle(), "content:"+a.getAskContent()+"/n请你及时回复");
jar包:mai.jar
相关文章推荐
- [邮件]简单MAIL发送类
- 使用javamail.jar进行邮件发送包含代理设置
- 利用mail.jar发送邮件(简单版)
- Linux系统下使用mail发送一封简单的Internet邮件【以及验证邮件是否发送成功sendmail -bp,必须是root用户才可以使用此命令查看邮件消息队列中的内容】
- JAVA 使用JavaMail发送邮件(mail.jar和activtion.jar)
- 用mail.jar发送邮件(正解)
- 基于java mail.jar邮件发送,最详细代码和讲解
- java-邮件发送-mail.jar
- 基于JAVA-mail的JAVA邮件发送:简单邮件发送
- asp.net内置对象Mail的实例介绍---自制简单的邮件发送网站
- JavaMail邮件学习01之发送简单邮件
- Java使用javax.mail.jar发送邮件并同意发送附件
- JavaMail 发送邮件【需要Mail.jar】
- jodd mail技术实现简单发送email邮件
- System.web.mail和System.Net.mail简单的邮件发送
- Java使用javax.mail.jar发送邮件并允许发送附件
- 用commons-mail.jar发送邮件
- 最简单的mail发送邮件配置
- 一个简单的基于System.Web.Mail的ASP.Net邮件发送程序
- Spring mail 邮件发送的简单实现