import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.swing.JOptionPane;
public class Email extends javax.swing.JDialog {
private String mailSMTPServer;
private String mailSMTPServerPort;
public Email(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
}
Email() { //Para o GMAIL
mailSMTPServer = "smtp.gmail.com";
mailSMTPServerPort = "465";
}
Email(String mailSMTPServer, String mailSMTPServerPort) {
this.mailSMTPServer = mailSMTPServer;
this.mailSMTPServerPort = mailSMTPServerPort;
}
public void Email(String from, String to, String subject, String message) {
Properties props = new Properties();
// quem estiver utilizando um SERVIDOR PROXY descomente essa parte e atribua as propriedades do SERVIDOR PROXY utilizado
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host", mailSMTPServer);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.user", from);
props.put("mail.debug", "true");
props.put("mail.smtp.port", mailSMTPServerPort);
props.put("mail.smtp.socketFactory.port", mailSMTPServerPort);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
SimpleAuth auth = null;
auth = new SimpleAuth ("usuario login","senha login");
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(true);
Message msg = new MimeMessage(session);
try {
//Setando o destinatário
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setFrom(new InternetAddress(from));
msg.setSubject(subject);
msg.setContent(message,"text/plain");
} catch (Exception e) {
System.out.println(">> Erro: Completar Mensagem");
e.printStackTrace();
}
Transport tr;
try {
tr = session.getTransport("smtp");
tr.connect(mailSMTPServer, "usuario login", "senha login");
msg.saveChanges();
tr.sendMessage(msg, msg.getAllRecipients());
tr.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Erro ao enviar Email", "ERRO", JOptionPane.ERROR_MESSAGE);
System.out.println(">> Erro: Envio Mensagem");
e.printStackTrace();
}
}
class SimpleAuth extends Authenticator {
public String username = null;
public String password = null;
public SimpleAuth(String user, String pwd) {
username = user;
password = pwd;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication (username,password);
}
}
esse codigo funciona perfeitamente para envio de mensagens queria colocar arquivo junto com a mensagem alguem teria uma ideia de como faze-lo?????????
Email java com arquivo
1 Resposta
RESOLVIDO
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("hi");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
String filename = "build.xml";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
Criado 19 de janeiro de 2009
Ultima resposta 19 de jan. de 2009
Respostas 1
Participantes 1
Alura Sistemas operacionais: entenda seu conceito e suas funções Descubra o que são sistemas operacionais, suas funções e tipos. Aprenda tudo de forma clara e objetiva. Não perca tempo!
Casa do Codigo Orientacao a Objetos: Aprenda seus conceitos e suas... Por Thiago Leite e Carvalho — Casa do Codigo