Ciao a tutti!
In questo forum ho visto la seguente classe per implementare l'invio di mail tramite java:
import javax.mail.*;
import java.util.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.io.*;
public class javamaildemo {
String host = "nome_host"; //tuo smtp
String from = "ind_mail"; //tuo indirizzo email
String ToAddress = "ind_mail"; //destinatario
String user = "user";
String pass = "pwd";
public javamaildemo() {
try {
//initialize the StringBuffer object within the try/catch loop
StringBuffer sb = new StringBuffer( );
//Get system properties
Properties props = System.getProperties( );
//Setup mail server
props.put("mail.smtp.host", host);
props.put("mail.debug", "true");
props.put("mail.smtp.auth","true");
//Get session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
session.setPasswordAuthentication(new URLName("smtp",host,25,"INBOX",user,pass), new PasswordAuthentication(user,pass));
//Define message
MimeMessage msg = new MimeMessage(session);
//Set the from address
msg.setFrom(new InternetAddress(from));
//Set the to address
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(ToAddress));
//Set the subject
msg.setSubject("Test mail using JavaMail APIs");
//Set the text content for body
sb.append("This is the 1st String line.\n\n");
sb.append("This is the 2nd String line.\n\n");
sb.append("This is the 3rd String line.\n\n");
msg.setText(sb.toString( ));
//Send message
Transport tr = session.getTransport("smtp");
tr.connect(host, user, pass);
msg.saveChanges(); // don't forget this
tr.sendMessage(msg, msg.getAllRecipients());
tr.close();
}
catch (MessagingException e) {
System.out.println(e);
}
}
public static void main(String[] args) {
javamaildemo jv = new javamaildemo();
}
}
Funziona regolarmente, ma posso utilizzarla,richiamarla all'interno di una java server page? Se si, in che modo?
Visto la mia scarsa esperienza con java, sarebbe gradita una descrizione chiara e approfondita dell'argomento in questione
Grazie
![]()

Rispondi quotando