2010-06-03 9 views
2

J'essaie d'envoyer un mail en utilisant javamail api en utilisant le code ci-dessous: quand j'ai compilé le fichier de classe im en obtenant l'erreur ci-dessous qui dit 'doit lancer la commande starttls' au dessous de. et aussi erreur de fonction getProvider() je pense donc ... je ne sais pas ce que les erreurs signifient.Erreur javamail: doit lancer la commande starttls en premier

import javax.servlet.*; 
import javax.servlet.http.*; 
import java.io.*; 
import javax.mail.*; 
import javax.mail.internet.*; 
import javax.mail.event.*; 
import javax.mail.Authenticator; 
import java.net.*; 
import java.util.Properties; 
public class mailexample 
    { 
    public static void main (String args[]) throws Exception { 

    String from = args[0]; 
    String to = args[1]; 
try 
{ 
Properties props=new Properties(); 
props.put("mail.transport.protocol", "smtp"); 
props.put("mail.smtp.host","smtp.gmail.com"); 
props.put("mail.smtp.port", "25"); 
props.put("mail.smtp.auth", "true"); 
javax.mail.Authenticator authenticator = new javax.mail.Authenticator() 
    { 
    protected javax.mail.PasswordAuthentication getPasswordAuthentication() 
     { 
     return new javax.mail.PasswordAuthentication("[email protected]", "pass"); 
    } 
}; 
Session sess=Session.getDefaultInstance(props,authenticator); 
sess.setDebug (true); 
Transport transport =sess.getTransport ("smtp"); 
Message msg=new MimeMessage(sess); 
msg.setFrom(new InternetAddress(from)); 
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
msg.setSubject("Hello JavaMail"); 
msg.setText("Welcome to JavaMail"); 
transport.connect(); 
transport.send(msg); 

} 
catch(Exception e) 
{ 
System.out.println("err"+e); 
} 
} 
} 

erreur:

 
C:\Users\bobby\Desktop>java mailexample [email protected] [email protected] 
com 

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.s 
mtp.SMTPTransport,Sun Microsystems, Inc] 
DEBUG SMTP: useEhlo true, useAuth true 
DEBUG SMTP: useEhlo true, useAuth true 

DEBUG: SMTPTransport trying to connect to host "smtp.gmail.com", port 25 

DEBUG SMTP RCVD: 220 mx.google.com ESMTP q10sm12956046rvp.20 

DEBUG: SMTPTransport connected to host "smtp.gmail.com", port: 25 

DEBUG SMTP SENT: EHLO bobby-PC 
DEBUG SMTP RCVD: 250-mx.google.com at your service, [60.243.184.29] 
250-SIZE 35651584 
250-8BITMIME 
250-STARTTLS 
250 ENHANCEDSTATUSCODES 


DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.s 
mtp.SMTPTransport,Sun Microsystems, Inc] 
DEBUG SMTP: useEhlo true, useAuth true 
DEBUG SMTP: useEhlo true, useAuth true 

DEBUG: SMTPTransport trying to connect to host "smtp.gmail.com", port 25 

DEBUG SMTP RCVD: 220 mx.google.com ESMTP l29sm12930755rvb.16 

DEBUG: SMTPTransport connected to host "smtp.gmail.com", port: 25 

DEBUG SMTP SENT: EHLO bobby-PC 
DEBUG SMTP RCVD: 250-mx.google.com at your service, [60.243.184.29] 
250-SIZE 35651584 
250-8BITMIME 
250-STARTTLS 
250 ENHANCEDSTATUSCODES 

DEBUG SMTP SENT: MAIL FROM: 
DEBUG SMTP RCVD: 530 5.7.0 Must issue a STARTTLS command first. l29sm12930755rvb 
.16 

DEBUG SMTP SENT: QUIT 
errjavax.mail.SendFailedException: Sending failed; 
    nested exception is: 
     javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command f 
irst. l29sm12930755rvb.16 
+0

@bobby voir aussi mes mises à jour de votre question précédente http://stackoverflow.com/questions/2963625/help-with-javamail-api – JoseK

+0

@bobby - avez-vous essayé toutes les étapes sur le lien de Matthieu Flaschen? – JoseK

Répondre

3

Probablement, vous avez besoin de mettre props.put("mail.smtp.starttls.enable","true"); avant authentifiez.

+0

hhey im ayant toujours le même problème ... même erreur .. – simplyblue

0

Ce code semble fonctionner correctement avec le serveur SMTP Gmail pour envoyer des courriels. Remarque - Ceci n'a pas de pièce jointe.

(Source: Modifié à partir de l'exemple sur https://forums.oracle.com/forums/thread.jspa?threadID=1587188)

package org.ssb.mail; 

import java.util.Date; 
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.AddressException; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class MailClient { 

/** 
* Entry method 
* 
* @param args 
*   String[] 
*/ 
public static void main(String[] args) { 

    MailClient client = new MailClient(); 

    try { 
     client.sendMail(); 
    } catch (AddressException ae) { 
     ae.printStackTrace(); 
    } catch (MessagingException me) { 
     me.printStackTrace(); 
    } 

} 

/** 
* Sends an email 
* 
* @param none 
*/ 
private void sendMail() throws AddressException, MessagingException { 

    // Get a Properties object 
    Properties props = System.getProperties(); 

    // ******************** FOR PROXY ****************** 

    // props.setProperty("proxySet","true"); 
    // props.setProperty("socksProxyHost","9.10.11.12"); 
    // props.setProperty("socksProxyPort","80"); 
    // props.setProperty("socksProxyVersion","5"); 

    props.setProperty("mail.smtp.host", "smtp.gmail.com"); 

    // ******************** FOR SSL ****************** 
    //final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; 
    //props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); 
    //props.setProperty("mail.smtp.socketFactory.fallback", "false"); 
    //props.setProperty("mail.smtp.port", "465"); 
    //props.setProperty("mail.smtp.socketFactory.port", "465"); 

    props.put("mail.smtp.starttls.enable", "true"); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.debug", "true"); 
    props.put("mail.store.protocol", "pop3"); 
    props.put("mail.transport.protocol", "smtp"); 
    final String username = "sender-username"; 
    final String password = "sender-password"; 
    Session session = Session.getDefaultInstance(props, 
      new Authenticator() { 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(username, password); 
       } 
      }); 

    // -- Create a new message -- 
    Message msg = new MimeMessage(session); 
    msg.setFrom(new InternetAddress("[email protected]")); 
    msg.setRecipients(Message.RecipientType.TO, 
      InternetAddress.parse("[email protected]", false)); 
    msg.setSubject("Hello"); 
    msg.setSentDate(new Date()); 

    // **************** Without Attachments ****************** 
    msg.setText("How are you"); 


    Transport.send(msg); 
    System.out.println("Message sent."); 

} 

} 
0

J'ai eu le même problème que vous avez décrit. Dans mon cas, le remplacement

Session session = Session.getDefaultInstance(props);

avec

Session session = Session.getInstance(props);

fait l'affaire. J'espère que cela sera utile.

0

Ce travail pour moi. Définissez les étiquettes suivantes. Ça va marcher.

props.put("mail.smtp.user","username"); 
props.put("mail.smtp.host", "smtp.gmail.com"); 
props.put("mail.smtp.port", "25"); 
props.put("mail.debug", "true"); 
props.put("mail.smtp.auth", "true"); 
props.put("mail.smtp.starttls.enable","true"); 
props.put("mail.smtp.EnableSSL.enable","true"); 

props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
props.setProperty("mail.smtp.socketFactory.fallback", "false"); 
props.setProperty("mail.smtp.port", "465"); 
props.setProperty("mail.smtp.socketFactory.port", "465");