Que deberia modificar? He probado poniendo quitando el main y añadiendo un dopost pero no me funciona y me salen errores.
Código:
import javax.activation.DataHandler; //para enviar imagen adjunta import javax.activation.FileDataSource; //para enviar imagen adjunta import java.util.Properties; import javax.mail.*; import javax.mail.BodyPart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; //para enviar imagen adjunta import javax.mail.internet.MimeBodyPart; //para enviar imagen adjunta import javax.mail.Authenticator; public class correo { String from_email = "xxxx", password = "xxxx", host = "smtp.gmail.com", port = "465"; Properties props = new Properties(); public correo(){ props.put( "mail.smtp.user", from_email ); props.put( "mail.smtp.host", host ); props.put( "mail.smtp.port", port ); props.put( "mail.smtp.starttls,enable", "true" ); props.put( "mail.smtp.auth" , "true" ); props.put( "mail.smtp.socketFactory.class" ,"javax.net.ssl.SSLSocketFactory" ); props.put( "mail.smtp.socketFactory.fallback" , "false" ); } public void sendEmail(String m_to, String m_subject, String m_text){ SecurityManager security = System.getSecurityManager(); try{ Authenticator auth = new SMTPAuthenticator(); Session session = Session.getInstance(props,auth); session.setDebug(true); MimeMessage msg = new MimeMessage (session); BodyPart texto = new MimeBodyPart(); texto.setText(m_text); BodyPart adjunto = new MimeBodyPart(); adjunto.setDataHandler(new DataHandler(new FileDataSource("Datos.rar"))); adjunto.setFileName("Datos.rar"); MimeMultipart multiParte = new MimeMultipart(); multiParte.addBodyPart(texto); multiParte.addBodyPart(adjunto); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from_email)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to)); message.setSubject(m_subject); message.setContent(multiParte); Transport t = session.getTransport("smtp"); t.connect("",""); t.sendMessage(message,message.getAllRecipients()); t.close(); }catch(Exception exp){ exp.printStackTrace(); } } private class SMTPAuthenticator extends javax.mail.Authenticator { public PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(from_email, password); } } public static void main( String arg[] ) { correo mail = new correo(); mail.sendEmail("m_to", "m_subject", "m_text"); } }