使用本站提供的下载链接:
下载完成后将包导入项目的 lib 中。
JSP 通过发送邮件正文+附件:
163邮箱增加了图片点字验证,验证处理需要单独再处理该案例才可用,其他流程OK。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| <%--163邮箱发送带有附件的邮件到qq邮箱--%>
<%@ page import="java.util.*" %> <%@ page import="javax.mail.*" %> <%@ page import="javax.mail.internet.*" %> <%@ page import="javax.activation.*" %> <% String result; String to = "username1@qq.com";
String from = "username2@163.com"; String pwd = "yourpassword"; String usr = "Jerry";
Properties properties = new Properties();
try { properties.setProperty("mail.smtp.auth", "true"); properties.setProperty("mail.transport.protocol", "smtp"); properties.setProperty("mail.smtp.host", "smtp.163.com"); properties.setProperty("mail.smtp.port", "25");
Session mailSession = Session.getDefaultInstance(properties);
Message message = new MimeMessage(mailSession); Transport transport = mailSession.getTransport();
message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject("This is the Subject Line!");
BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText("This is message body"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart(); String filename = "E:\\JavaNote\\00_Tools\\04_Web\\javaMail_jar\\mail.jar"; DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart); message.setContent(multipart); message.setSentDate(new Date());
transport.connect(usr, pwd); transport.sendMessage(message, message.getAllRecipients());
result = "Sent message successfully...."; } catch (Exception e) { e.printStackTrace(); result = "Error: unable to send message...."; } %> <html> <head> <title>Send Email using JSP</title> </head> <body> <div style="text-align: center;"> <h1>Send Email using JSP</h1> </div> <p align="center"> <% out.println("Result: " + result + "\n"); %> </p> </body> </html>
|