Bom gente estou querendo mandar e-mail com java… Eu tenhu uma conta no www.myjavaserver.com e estou querendo mandar e-mails por um JSP ou Servlet (Servlet de preferência) e estou com dificuldades quando pego algum exemplo, porque naum tem nem autenticação… e fiquei sem entender…
Resumindo, se alguém tem experiência com JavaMail e tem algum código, de preferência que já tenha usado no myjavaserver, pesso q me envie, ou melhor, poste aki pra galera poder peguar tmb…
Olá amigo,
segue o código que fiz para envio de email, não usei autenticação, testei usando o smtp da empresa, mas que eu me lembre eu tentei alguns que precisam de autenticação e deu certo, faça o teste ok.
package email;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Email extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
PrintWriter out = res.getWriter();
res.setContentType("text/html");
try {
String to = req.getParameter("to");
String from = "teste@dominio.com.br";
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.seudominio.com.br");
Session session = Session.getInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
Address toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);
message.setSubject("teste de envio de e-mails");
message.setContent("este eh um teste de envio", "text/plain");
Transport.send(message);
out.println("E-mail enviado
");
}
catch (MessagingException e) {
out.println("Email nao pode ser enviado! " + e.getMessage());
}
}
}
Eu copiei apenas do primeiro que fiz, o método ainda está Get, pois estava fazendo testes e é mais rápido de passar parametro, imagino que você vai precisar Post, é só fazer a troca, para usar o Get você pode fazer isso:
http://www.seusite.com.br/servlets/email?to=email@email.com.br
Abraço
[/code]
Exemplo utilizando autenticação:
[code]public class EnviaEmail {
public void send(String smtpHost, String smtpPort, String smtpUser, String passWord, String from, String[] to, String subject, String content, File[] attachsFile )throws SacException {
try{
//Create a mail session
Properties props = new Properties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", "" + smtpPort);
props.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator( smtpUser, passWord );
//Session session = Session.getDefaultInstance(props, auth);
Session session = Session.getInstance(props, auth);
// Construct the message
Message msg = new MimeMessage(session);
msg.setSentDate( new Date() );
msg.setFrom(new InternetAddress(from));
InternetAddress[] addressTo = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
addressTo[i] = new InternetAddress( to[i] );
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
msg.setSubject(subject);
msg.setHeader("Disposition-Notification-To", from );
// create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
//fill message
messageBodyPart.setContent( content, "text/plain");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
if ( attachsFile != null && attachsFile[0] != null ){
for ( int i = 0; i < attachsFile.length; i++ ){
messageBodyPart = new MimeBodyPart();
DataSource source = null;
source = new FileDataSource( attachsFile[i] );
messageBodyPart.setDataHandler( new DataHandler(source) );
messageBodyPart.setFileName( source.getName() );
multipart.addBodyPart(messageBodyPart);
}
}
//set the content
msg.setContent( multipart );
// Send the message
Transport.send(msg);
}catch( Exception e ) {
e.printStackTrace();
//throw new SacException();
}
}
}[/code]
Classe SMTPAuthenticator:
[code]public class SMTPAuthenticator extends javax.mail.Authenticator {
private String username;
private String password;
public SMTPAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}[/code]
flw