Ola!
Comecei a utilizar a JavaMail… e apos alguns testes conseguir enviar e-mail com anexo…
Mas ao ultilizar:
* SendMail.java
*
* Created on 26 de Maio de 2008, 09:36
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package mail;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import javax.swing.*;
/**
*
* @author Squall
*/
public class SendMail extends Thread{
private String from, to, host, password;
private File f;
private boolean b;
/** Creates a new instance of SendMail */
public SendMail(String from, String password, String to, String host, boolean b) {
this.password = password;
this.from = from;
this.to = to;
this.host = host;
this.b = b;
f = new File("tempMail.rtf");
System.out.println(this.from+" - "+this.password+" - "+this.to+" - "+this.host+" - "+f.getName()+" - "+f.length());
this.start();
}
private Session getSession(){
Session sessao = null;
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable","true");
sessao = Session.getDefaultInstance(props, new Autenticador(from, password));
return sessao;
}
public void run(){
Session session = getSession();
session.setDebug(b);
try{
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("Reembolso de "+from);
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText("Arquivo anexo!");
MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(f.getName());
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());
Transport.send(msg);
}catch(MessagingException mex){
mex.printStackTrace();
Exception ex = null;
if((ex = mex.getNextException()) != null){
ex.printStackTrace();
}
}
}
}
/*
* Autenticador.java
*
* Created on 26 de Maio de 2008, 11:01
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package mail;
import javax.mail.*;
/**
*
* @author Squall
*/
public class Autenticador extends javax.mail.Authenticator{
private String user;
private String senha;
public Autenticador(){}
public Autenticador(String user, String senha){
this.senha = senha;
this.user = user;
}
* @return Um objeto da classe PasswordAuthentication
* inicializado com o usuário e senha fornecidos.
*/
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(user, senha);
}
}
SendMail mail = new SendMail(from, pass, to, host, false);
Até funciona mas, quando chamo essa ultima linha com valores de from pass to e host diferente… o programa envia com os dados fornecidos anteriormente.
Porisso adicionei essa linha
System.out.println(this.from+" - "+this.password+" - "+this.to+" - "+this.host+" - "+f.getName()+" - "+f.length());
Isso pra verificar se estava fazendo algo de errado… mas os dados impresso estão corretos.
Recapitulando.
envio um primeiro e-mail normalmente, mas a partir do segundo não consigo alterar o remetente, destinatario, smtp… etc.
Obrigado pela atenção!!!