JSP Invia Mail
Sebbene l'uso di JSP per la funzione di invio del messaggio è semplice, ma richiede JavaMail API, e la necessità di installare JavaBean Activation Framework.
- È possibile scaricare l'ultima versione di Java del JavaMail , aprire la parte destra della pagina c'è un link Download, cliccaci sopra per scaricare.
- È possibile scaricare l'ultima versione di Java (versione 1.1.1) JAF .
È inoltre possibile utilizzare il sito per fornire link per il download:
Scaricare e decomprimere il file nella directory principale, si vedrà una serie di pacchetto jar. I pacchetti mail.jar e pacchetto activation.jar aggiunto variabile CLASSPATH.
Invia una mail semplice
Questo esempio mostra come dalla vostra macchina per inviare un messaggio semplice. Si assume che localhost è già collegato alla rete e la possibilità di inviare un messaggio. Nel frattempo, una volta confermare pacchetti mail.jar e pacchetto activation.jar è stato aggiunto alla variabile CLASSPATH.
<%@ page import="java.io.*,java.util.*,javax.mail.*"%> <%@ page import="javax.mail.internet.*,javax.activation.*"%> <%@ page import="javax.servlet.http.*,javax.servlet.*" %> <% String result; // 收件人的电子邮件 String to = "[email protected]"; // 发件人的电子邮件 String from = "[email protected]"; // 假设你是从本地主机发送电子邮件 String host = "localhost"; // 获取系统属性对象 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("mail.smtp.host", host); // 获取默认的Session对象。 Session mailSession = Session.getDefaultInstance(properties); try{ // 创建一个默认的MimeMessage对象。 MimeMessage message = new MimeMessage(mailSession); // 设置 From: 头部的header字段 message.setFrom(new InternetAddress(from)); // 设置 To: 头部的header字段 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 设置 Subject: header字段 message.setSubject("This is the Subject Line!"); // 现在设置的实际消息 message.setText("This is actual message"); // 发送消息 Transport.send(message); result = "Sent message successfully...."; }catch (MessagingException mex) { mex.printStackTrace(); result = "Error: unable to send message...."; } %> <html> <head> <title>Send Email using JSP</title> </head> <body> <center> <h1>Send Email using JSP</h1> </center> <p align="center"> <% out.println("Result: " + result + "\n"); %> </p> </body> </html>
Ora visitare il sito http: // localhost: 8080 / SendEmail.jsp, invierà un messaggio a [email protected] e visualizza i seguenti risultati:
Send Email using JSP Result: Sent message successfully....
Se si desidera inviare un messaggio a molte persone, i metodi elencati di seguito possono essere utilizzati per specificare più di un indirizzo e-mail:
void addRecipients(Message.RecipientType type, Address[] addresses) throws MessagingException
Descrizione dei parametri sono i seguenti:
- Tipo: Questo valore sarà impostato a TO, CC o BCC. rappresentanti CC una copia, BCC per copia in bianco l'esempio utilizzato nel programma è quello.
- indirizzi: Questo indirizzo e-mail è un array, metodo quando è richiesto l'indirizzo e-mail specificato InternetAddress ().
Invia una e-mail HTML
Questo esempio invia un semplice messaggi HTML. Si presuppone che sia già collegato alla rete di localhost e la possibilità di inviare messaggi. Nel frattempo, una volta confermare pacchetti mail.jar e pacchetto activation.jar è stato aggiunto alla variabile CLASSPATH.
Questo esempio è molto simile a quella dell'esempio precedente, ma in questo caso si utilizza setContent () metodo, il "text / html" come secondo parametro passato per indicare che il messaggio contiene il contenuto HTML.
<%@ page import="java.io.*,java.util.*,javax.mail.*"%> <%@ page import="javax.mail.internet.*,javax.activation.*"%> <%@ page import="javax.servlet.http.*,javax.servlet.*" %> <% String result; // 收件人的电子邮件 String to = "[email protected]"; // 发件人的电子邮件 String from = "[email protected]"; // 假设你是从本地主机发送电子邮件 String host = "localhost"; // 获取系统属性对象 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("mail.smtp.host", host); // 获取默认的Session对象。 Session mailSession = Session.getDefaultInstance(properties); try{ // 创建一个默认的MimeMessage对象。 MimeMessage message = new MimeMessage(mailSession); // 设置 From: 头部的header字段 message.setFrom(new InternetAddress(from)); // 设置 To: 头部的header字段 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 设置 Subject: header字段 message.setSubject("This is the Subject Line!"); // 设置 HTML消息 message.setContent("<h1>This is actual message</h1>", "text/html" ); // 发送消息 Transport.send(message); result = "Sent message successfully...."; }catch (MessagingException mex) { mex.printStackTrace(); result = "Error: unable to send message...."; } %> <html> <head> <title>Send HTML Email using JSP</title> </head> <body> <center> <h1>Send Email using JSP</h1> </center> <p align="center"> <% out.println("Result: " + result + "\n"); %> </p> </body> </html>
Ora si può provare a utilizzare più di file JSP per inviare HTML messaggio di posta elettronica.
Accessori inclusi nel messaggio
Questo esempio dimostra come per inviare un messaggio che contiene un allegato.
<%@ page import="java.io.*,java.util.*,javax.mail.*"%> <%@ page import="javax.mail.internet.*,javax.activation.*"%> <%@ page import="javax.servlet.http.*,javax.servlet.*" %> <% String result; // 收件人的电子邮件 String to = "[email protected]"; // 发件人的电子邮件 String from = "[email protected]"; // 假设你是从本地主机发送电子邮件 String host = "localhost"; // 获取系统属性对象 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("mail.smtp.host", host); // 获取默认的Session对象。 Session mailSession = Session.getDefaultInstance(properties); try{ // 创建一个默认的MimeMessage对象。 MimeMessage message = new MimeMessage(mailSession); // 设置 From: 头部的header字段 message.setFrom(new InternetAddress(from)); // 设置 To: 头部的header字段 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 设置 Subject: header字段 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 = "file.txt"; DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart); // 发送完整消息 message.setContent(multipart ); // 发送消息 Transport.send(message); String title = "Send Email"; result = "Sent message successfully...."; }catch (MessagingException mex) { mex.printStackTrace(); result = "Error: unable to send message...."; } %> <html> <head> <title>Send Attachement Email using JSP</title> </head> <body> <center> <h1>Send Attachement Email using JSP</h1> </center> <p align="center"> <% out.println("Result: " + result + "\n"); %> </p> </body> </html>
sezione Autenticazione utente
Se il server di posta richiede un nome utente e una password per l'autenticazione degli utenti, è possibile impostare come la seguente:
props.setProperty("mail.user", "myuser"); props.setProperty("mail.password", "mypwd");
Utilizza il modulo per inviare un messaggio
Utilizzando un modulo HTML per ricevere una e-mail e ottenere tutte le informazioni su richiesta e-mail a:
String to = request.getParameter("to"); String from = request.getParameter("from"); String subject = request.getParameter("subject"); String messageText = request.getParameter("body");
Dopo aver ottenuto queste informazioni, è possibile utilizzare gli esempi di cui sopra per inviare la posta.