COn questo codice non riesco ad inviare una mail, dove sbaglio???
codice:package javaapplication6; import javax.mail.*; import java.util.*; import javax.mail.internet.*; import javax.activation.*; import java.io.*; public class javamail { String host = "smtp.mail.yahoo.it"; //tuo smtp String from = "mioindirizzo@yahoo.it"; //tuo indirizzo email String ToAddress = "destinatario@libero.it"; //destinatario String user = "mioindirizzo@yahoo.it"; String pass = "pass"; public javamail() { 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,1243,"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) { javamail jv = new javamail(); } }

Rispondi quotando