首页 > 代码库 > 简单的邮件发送mail.jar
简单的邮件发送mail.jar
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.commail.smtp.qq.mailServer=smtp.qq.commail.smtp.sina.mailServer=smtp.sina.com.cnmail.username=asdmail.password=123456mail.from=asd@163.com
简单的邮件发送mail.jar
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。