比JavaMail使用方法要直接,jar包只需要在JavaMail的基础上添加commons-email-1.4.jar即可。
代码:
// QQ邮箱发送邮件 SimpleEmail simpleEmail = new SimpleEmail(); simpleEmail.setHostName("smtp.qq.com"); simpleEmail.setAuthentication("3******8", "g*****ai"); simpleEmail.setSmtpPort(587); simpleEmail.setFrom("3********@qq.com", "y****", "UTF-8"); simpleEmail.addTo(email, name); simpleEmail.setSubject("title"); simpleEmail.setMsg("content"); simpleEmail.send(); System.out.println("发送邮件成功!");
// 网易邮箱发送邮件 SimpleEmail simpleEmail = new SimpleEmail(); simpleEmail.setHostName("smtp.163.com"); simpleEmail.setAuthentication("y*******@163.com", "y********"); simpleEmail.setSmtpPort(25); simpleEmail.setFrom("y*******@163.com", "y*", "UTF-8"); simpleEmail.addTo("3**********@qq.com", "c**************"); simpleEmail.setSubject("网易邮箱测试"); simpleEmail.setMsg("网易邮箱测试网易邮箱测试网易邮箱测试"); simpleEmail.send(); System.out.println("发送成功");
// 发送带附件的邮件 // 附件 EmailAttachment attachment = new EmailAttachment(); attachment.setPath("D:\\testEXC.xlsx"); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription("excel文件"); attachment.setName("MyExcel.xlsx"); // 邮件 MultiPartEmail simpleEmail = new MultiPartEmail(); simpleEmail.setHostName("smtp.163.com"); simpleEmail.setAuthentication("y***********@163.com", "y********"); simpleEmail.setSmtpPort(25); simpleEmail.setFrom("y*********@163.com", "y*", "UTF-8"); simpleEmail.addTo("3**************@qq.com", "c***********"); simpleEmail.setSubject("带附件"); simpleEmail.setMsg("带附件的邮件测试"); simpleEmail.attach(attachment); simpleEmail.send(); System.out.println("发送成功");
时间: 2024-10-13 09:31:49