MimeMessage message = null; final Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.socketFactory.fallback", "true"); props.put("mail.smtp.host", host); props.put("mail.smtp.port", port); props.put("mail.user", user); props.put("mail.password", pw); Authenticator authenticator = new Authenticator() { protected PasswordAuthentication getPasswordAuthentication(){ String userName = props.getProperty("mail.user"); String password = props.getProperty("mail.password"); return new PasswordAuthentication(userName, password); } }; Session mailSession = Session.getInstance(props, authenticator); message = new MimeMessage(mailSession); message.setFrom(new InternetAddress("郵箱地址", "郵箱名稱", "UTF-8")); //一、是否須要回執--非必須 message.setHeader("Disposition-Notification-To", "<gyli1992@qq.com>"); //收件人地址 message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("gyoyinli1992@qq.com", "user_to", "UTF-8")); //主題 message.setSubject("test02"); //內容 message.setContent("<a href='taoquan.tlong.top' style='color:#ccc'>淘券網</a>", "text/html;charset=UTF-8"); //顯示發送時間 message.setSentDate(new Date()); //保存更改 message.saveChanges(); //輸出eml文件 OutputStream out = new FileOutputStream("D://myemail.eml"); message.writeTo(out); out.flush(); out.close(); //發送郵件 Transport.send(message); System.out.println("郵件發送成功");