Enviando email

2 respostas
A

Galera to tentando fazer uns testes de envio de email usando a api javax.mail usando o smtp server do yahoo. Alguem conhece algum free na web? To jogando minha senha e login mas fica dando falha na autenticacao. O problema eh que ano passado esse mesmo codigo funcionou. Segue codigo

import java.io.IOException;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;




 public class Email {
	 
	 private static Session session = null;
     private static final String contentType = "text/html";
     private static String servidor = "smtp.mail.yahoo.com.br";


     class Autenticacao extends Authenticator {

         protected PasswordAuthentication getPasswordAuthentication() {

             System.out.println("Autenticando...");

             return new PasswordAuthentication("login","senha");
         }

     }


     

     public void enviarEmail(String assunto, String mensagem, String de) throws
             MessagingException {

         String emailPara = "[email removido]";
         Properties props = new Properties();


         props.put("mail.smtp.host", servidor);
         props.put("mail.smtp.auth", "true");
         session = Session.getDefaultInstance(props, new Autenticacao());

         Message message = new MimeMessage(session);

         InternetAddress endMail = new InternetAddress(emailPara);
         message.addRecipient(Message.RecipientType.TO, endMail);

         InternetAddress fromEmail = new InternetAddress(de);
         message.setFrom(fromEmail);
         message.setSubject(assunto);

         MimeMultipart mpRoot = new MimeMultipart("mixed");
         MimeMultipart mpContent = new MimeMultipart("alternative");

         MimeBodyPart mbp1 = new MimeBodyPart();

         mbp1.setContent(mensagem.toString(), contentType);
         mpContent.addBodyPart(mbp1);

         MimeBodyPart contentPartRoot = new MimeBodyPart();
         contentPartRoot.setContent(mpContent);
         mpRoot.addBodyPart(contentPartRoot);

         message.setContent(mpRoot);
         message.saveChanges();

         Transport.send(message);
     }




	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
        
        Email email = new Email();
        try {
			email.enviarEmail("Teste assunto","primeira mensagem","[email removido]");
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

2 Respostas

A

Galera, desculpe a insistencia mas niguem sabe ao menos um servidor de email disponivel na web para uso?

Alberto

T

Já ouvi falar que o IG permite o uso SMTP/POP, mas normalmente uso para testes um servidor de email muito bobo chamado James. ( http://james.apache.org ).

Criado 19 de abril de 2006
Ultima resposta 20 de abr. de 2006
Respostas 2
Participantes 2