Quello che voglio fare è abbastanza facile da spiegare a parole, vorrei avere la possiblità di spedire dei messaggi di posta elettronica con java.
Ho trovato questa classe sul forum
ho bisogno dell'autenticazione non avendo un server smtp!!codice:import javax.mail.*; import java.util.*; import javax.mail.internet.*; import javax.activation.*; import java.io.*; public class javamaildemo { String host = ".........."; //tuo smtp String from = "..........."; //tuo indirizzo email String ToAddress = "........"; //destinatario String user = "..........."; String pass = "........"; 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); } } }
La classe viene lanciata da una servlet tramite:
Il problema è che la classe mi genera un eccezionecodice:javamaildemo jv = new javamaildemo();
javax.mail.MessagingException: Could not connect to SMTP host
i parametri che ho inserito sono
host = "out.virgilio.it" oppure host ="smtp.gmail.com"
ma con nessuna delle due funziona!
Dove sbaglio?

Rispondi quotando