import javax.mail.*; import javax.mail.internet.*; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Properties; public class SendEmailUtil { /**服务邮箱账号**/ public static String serviceEmail = "**服务邮箱账号**"; /**服务邮箱授权码**/ public static String password = "**服务邮箱授权码**"; /**发件人**/ public static String sender = "系统管理员"; /**主题**/ public static String subject = "系统通知"; /**邮箱类型**/ private static String mailServerName = "163/qq/..."; /**系统属性**/ private Properties properties; /** 邮件会话对象**/ private Session session; /** MIME邮件对象**/ private MimeMessage msg; /** Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象**/ private Multipart multipart; /**正文内容对象**/ private BodyPart bp; /**对象实例**/ private static SendEmailUtil instance = null; public SendEmailUtil() { properties = System.getProperties(); properties.put("mail.transport.protocol", "smtp"); properties.put("mail.smtp.host", "smtp." + mailServerName + ".com"); properties.put("mail.smtp.port", "25"); properties.put("mail.smtp.auth", "true"); // 创建验证器 Authenticator authenticator = new Authenticator() { @Override public PasswordAuthentication getPasswordAuthentication() { // 密码验证(授权码) return new PasswordAuthentication(serviceEmail, password); } }; // 建立默认会话 session = Session.getDefaultInstance(properties); } public static SendEmailUtil getInstance() { if (instance == null) { instance = new SendEmailUtil(); } return instance; } /** * 发送邮件 * * @param addressee 收件人邮箱 * @param text 正文 * @return */ public boolean sendMail(String addressee, String text) { boolean success = true; try { // 建立信息 msg = new MimeMessage(session); multipart = new MimeMultipart(); // 自定义发件人昵称 String nikeName = MimeUtility.encodeText(sender); // 设置发件人 msg.setFrom(new InternetAddress(serviceEmail, nikeName)); // 设置收件人 if (addressee != null) { InternetAddress[] iaToList = new InternetAddress().parse(addressee); msg.setRecipients(Message.RecipientType.TO, iaToList); } // 设置抄送人 // InternetAddress[] parse = new InternetAddress().parse(copyto); // msg.setRecipients(Message.RecipientType.CC,parse); // 设置发送日期 msg.setSentDate(new Date()); Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); String DateSuffix = dateFormat.format(date); // 设置主题 msg.setSubject(subject + DateSuffix); // 设置正文 bp = new MimeBodyPart(); bp.setText(text); multipart.addBodyPart(bp); // 获取图片的流 // URL url = new URL(fileUrl); // HttpURLConnection conn = (HttpURLConnection)url.openConnection(); // conn.setRequestMethod("GET"); // conn.setConnectTimeout(5 * 1000); // InputStream inStream = conn.getInputStream();//通过输入流获取图片数据 // 设置附件 // bp = new MimeBodyPart(); // bp.setDataHandler(new DataHandler(new ByteArrayDataSource(inStream,"application/x-jpg"))); // String fileName = String.valueOf(System.currentTimeMillis())+ ".jpg"; // bp.setFileName(MimeUtility.encodeText(fileName, "UTF-8", "B")); // multipart.addBodyPart(bp); msg.setContent(multipart); msg.saveChanges(); // 邮件服务器进行验证 Transport tran = session.getTransport("smtp"); // 配置发送者的邮箱和密码 tran.connect("smtp." + mailServerName + ".com", serviceEmail, password); // 发送 tran.sendMessage(msg, msg.getAllRecipients()); // 邮件连接关闭 tran.close(); System.out.println("邮件发送成功!收件人:"+addressee); } catch (Exception e) { e.printStackTrace(); success = false; } return success; } public static void main(String[] args) throws InterruptedException { SendEmailUtil emailUtil = SendEmailUtil.getInstance(); while (true) { emailUtil.sendMail("**接收邮箱地址**", "this is a testing message , please don`t answer the e-mail."); } } }