Smanettando un po' sul forum della sun:
codice:
import javax.mail.*;
import java.util.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.io.*;
public class javamaildemo {
String host = "smtp.mail.yahoo.it"; //tuo smtp
String from = "xyzabgd@yahoo.it"; //tuo indirizzo email
String ToAddress = "destinatario@email.com"; //destinatario
String user = "xyzabgd";
String pass = "password";
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();
}
}
commenta la prop e session.setDebug se non vuoi vedere il log dei messaggi di debug.