Jump to content

[Resolvido] Verificar mail


joaos

Recommended Posts

package cript;


import java.security.Security;
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.PasswordAuthentication; 
import javax.swing.JOptionPane;


public class SendMailControlImpl  {
private String mailhost = "smtp.gmail.com";
private static int index;

 public void sendMail(String de, String para, String assunto, String menssagem) throws Exception {

    Properties props = new Properties();  


    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

	 props.put("mail.transport.protocol", "smtp"); //define protocolo de envio como SMTP  
	 props.put("mail.smtp.starttls.enable","true");  
	 props.put("mail.smtp.host",mailhost); //server SMTP do GMAIL  
	 props.put("mail.smtp.auth", "true"); //ativa autenticacao  
	 props.put("mail.smtp.user", de); //usuario ou seja, a conta que esta enviando o email (tem que ser do GMAIL)  
	 props.put("mail.debug", "true");  
	 props.put("mail.smtp.port", "465"); //porta  
	 props.put("mail.smtp.socketFactory.port", "465"); //mesma porta para o socket  
	 props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");  
	 props.put("mail.smtp.socketFactory.fallback", "false");  


	 Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
		    protected PasswordAuthentication getPasswordAuthentication() {
			    return new PasswordAuthentication("userlogin","palavrapasse");
		    }
	    });  


	 //Objeto que contém a mensagem  
	 Message msg = new MimeMessage(session);  

	 try {  
		 //Setando o destinatário  
		 msg.setRecipient(Message.RecipientType.TO, new InternetAddress(para));  
		 //Setando a origem do email  
		 msg.setFrom(new InternetAddress(de));  
		 //Setando o assunto  
		 msg.setSubject(assunto);  
		 //Setando o conteúdo/corpo do email  
		 msg.setContent(menssagem,"text/plain");  

	 } catch (Exception e) {  
		 System.out.println(">> Erro: Completar Mensagem");  
		 e.printStackTrace(); 
		 throw e;
	 }  

	 //Objeto encarregado de enviar os dados para o email  
	 Transport tr;  
	 try {  
		 tr = session.getTransport("smtp"); //define smtp para transporte  

		 tr.connect(mailhost, "", "");  
		 msg.saveChanges(); // don't forget this  
		 //envio da mensagem  
		 tr.sendMessage(msg, msg.getAllRecipients());  
		 tr.close();  
	 } catch (Exception e) {  
		 // TODO Auto-generated catch block
	  index=1;
	  JOptionPane.showMessageDialog(null,"Mail Não existe");


	 }  

 }  



 public static void main(String[] args) {
  SendMailControlImpl sm=new SendMailControlImpl();
  try {
   sm.sendMail("de","para","assunto","menssagem");
   System.out.print(index);
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

}

Não é a melhor maneira mas dá.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.