Grazie per la risposta, ma ho risolto in un altro modo (o almeno credo).
codice:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package prog_minieditor;
/**
*
* @author Chiarula
*/
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class Email {
public static boolean sendEmail(String to, String subject, String msgText, String fileDaAllegare)
{
String from = "mail@gmail.com";
String smtpServer = "smtp.google.com";
String porta = "465";
String username = "nome";
String password = "password";
boolean esito=true;
Properties props = System.getProperties(); //imposto il server per la spedizione
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.host", smtpServer);
props.put("mail.smtp.port", porta);
props.put("mail.smtp.starttls.enable","true" );
props.put("mail.smtp.auth", "true" );
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
Authenticator auth = new SMTPAutenticazione();
try
{
MimeMessage msg = new MimeMessage(session); //creo il messaggio
msg.setFrom(new InternetAddress(from)); //imposto il mittente
InternetAddress[] address = {new InternetAddress(to)} ;
msg.setRecipients(Message.RecipientType.TO, address); //imposto il/i destinatario/i
msg.setSubject(subject); //imposto l'oggetto del messaggio
//crea e compila la prima parte del messaggio
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(msgText);
//crea la seconda parte del messaggio
MimeBodyPart mbp2 = new MimeBodyPart();
//inserisce il file come allegato del messaggio
FileDataSource fds = new FileDataSource(fileDaAllegare);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
msg.setContent(mp);
msg.setSentDate(new Date());
//invia il messaggio
Transport transport = session.getTransport("smtps");
transport.connect(smtpServer, 465, username, password);
transport.sendMessage(msg, msg.getAllRecipients());
Transport.send(msg);
transport.close();
}
catch (Exception e) {
e.printStackTrace();
esito=false;
}
return esito;
}
}
codice:
public class SMTPAutenticazione extends javax.mail.Authenticator {
@Override
public PasswordAuthentication getPasswordAuthentication() {
String username = "nome";
String password = "password";
return new PasswordAuthentication(username, password);
}
}
Ho fatto queste due classi, e adesso mi dà un errore ma non credo saio dovuto al codice infatti mi dice java.net.ConnectException: Operation timed out
Comunque visto che sei stato così gentile, posso chiederti un'altra cosa?
Io ho un pannello dove l'utente può disegnare, alla fine il programma deve salvare l'immagine... Ho trovato questo codice ma salva solo un'immagine tutta nera.
codice:
private void saveComponentAsJPEG(Component myComponent, String filename) {
Dimension size = myComponent.getSize();
BufferedImage myImage =new BufferedImage(size.width,
size.height,BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = myImage.createGraphics();
myComponent.paint(g2);
try {
OutputStream out = new FileOutputStream(filename);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(myImage);
out.close();}
catch (Exception e) { System.out.println(e);}
}