2014-04-09 12 views
7

Próbuję wysłać wiadomość e-mail na konto Gmail przy użyciu interfejsu JavaMail API. Zrobiłem następujące code.I chcą wysłał wiadomość do wielokrotnego recipents.But nie jest working.It jego dając wyjątek jak „nie można połączyć się z SMTP host.sending failed”Nie można połączyć się z wyjątkiem hosta SMTP podczas wysyłania poczty za pośrednictwem interfejsu JavaMail API

package com.cts.email; 

import java.util.Properties; 


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.MimeMessage; 

public class SendEmail {  
     Properties emailProperties; 
     Session mailSession; 
     MimeMessage emailMessage; 
     public static void main(String args[]) throws MessagingException, javax.mail.MessagingException {  
      SendEmail javaEmail = new SendEmail(); 
      Session session=javaEmail.setMailServerProperties(); 
      javaEmail.createEmailMessage(session); 
      // javaEmail.sendEmail(); 
      }  
     public Session setMailServerProperties() {  
      Properties props = new Properties(); 
     props.put("mail.smtp.host", "smtp.gmail.com"); 
     props.put("mail.smtp.socketFactory.port", "465"); 
     props.put("mail.smtp.socketFactory.class", 
       "javax.net.ssl.SSLSocketFactory"); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.port", "465"); 

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

      public void createEmailMessage(Session session) throws MessagingException, javax.mail.MessagingException {  
        String[] toEmails = { "[email protected]","[email protected]" }; 
        try { 
         for (String to_mail : toEmails) { 


        Message message = new MimeMessage(session); 
        message.setFrom(new InternetAddress("[email protected]")); 
        message.setRecipients(Message.RecipientType.TO, 
          InternetAddress.parse(to_mail)); 
        message.setSubject("Java Email"); 
        message.setText("This is an email sent by <b>JavaMail</b> api."); 

        Transport.send(message); 



       } 
         System.out.println("Done"); 
        }catch (MessagingException e) { 
        throw new RuntimeException(e); 
       } 
       }  


         } 

otrzymuję następujący wyjątek:

Exception in thread "main" java.lang.RuntimeException: javax.mail.SendFailedException: Sending failed; 
    nested exception is: 
    class javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465; 
    nested exception is: 
    java.net.ConnectException: Connection refused: connect 
    at com.cts.email.SendEmail.createEmailMessage(SendEmail.java:62) 
    at com.cts.email.SendEmail.main(SendEmail.java:21) 
Caused by: javax.mail.SendFailedException: Sending failed; 
    nested exception is: 
    class javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465; 
    nested exception is: 
    java.net.ConnectException: Connection refused: connect 
    at javax.mail.Transport.send0(Transport.java:218) 
    at javax.mail.Transport.send(Transport.java:80) 
    at com.cts.email.SendEmail.createEmailMessage(SendEmail.java:55) 
    ... 1 more 

pomóżcie mi przez to.

+0

Works dla mnie: przynajmniej ja można się z nim połączyć. – EJP

+0

Czy może to być problem z siecią? Zapora sieciowa, proxy, zły DNS? –

Odpowiedz

7

Działa to dla mnie:

import java.util.Properties; 
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.MimeMessage; 

public class SendMail { 


    public static void sendEmail(String to) 
    { 
     final String username = "your_usename_goes_here"; 
     final String password = "your_password_goes_here"; 

     Properties props = new Properties(); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.host", "smtp.gmail.com"); 
     props.put("mail.smtp.port", "587"); 

     Session session = Session.getInstance(props, 
      new javax.mail.Authenticator() { 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication(username, password); 
      } 
      }); 

     try { 

      Message message = new MimeMessage(session); 
      message.setFrom(new InternetAddress("[email protected]")); 
      message.setRecipients(Message.RecipientType.TO, 
       InternetAddress.parse(to)); 
      message.setSubject("A testing mail header !!!"); 
      message.setText("Dear Mail Crawler," 
       + "\n\n No spam to my email, please!"); 

      Transport.send(message); 

      System.out.println("Done"); 

     } 

     catch (MessagingException e) 
     { 
      // throw new RuntimeException(e); 
      System.out.println("Username or Password are incorrect ... exiting !"); 
     } 
    } 


    public static void main(String[] args) 
    { 
     String to = "[email protected]"; 
     sendEmail(to); 
    } 
} 
+0

to też działa dla mnie, gdybym miał pobrać plik? gdzie powinienem dodać linię do nadawania? – Scarl

+0

Dzięki człowieku. To zadziałało dla mnie. –

+0

uzyskiwanie błędu Nazwa użytkownika lub hasło są nieprawidłowe ... wychodzące! , ale mój adres gmail i hasło są poprawne? Jaki byłby inny powód? –

0

// mieć pewność, że jeśli używasz ANTIVIRUS AOR wszelkiego rodzaju pożegnanie UPEWNIAŁ // TO OFF czasowo je INACZEJ „DŁONI” wyjątki ARISE ...

String user= "[email protected]"; 
    String pass= "FROMPASSWORD"; 
    String mailhost = "smtp.gmail.com"; 

    Properties props= System.getProperties(); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.port", "465"); 
     props.put("mail.smtp.user", user); 
     props.put("mail.smtp.password", pass); 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.socketFactory.port", "465"); 
     props.put("mail.smtp.socketFactory.class", 
     "javax.net.ssl.SSLSocketFactory"); 
     props.put("mail.smtp.socketFactory.fallback", "false"); 
     props.setProperty("mail.smtp.quitwait", "false"); 

    Session session= Session.getDefaultInstance(props,new         javax.mail.Authenticator() { 

     @Override 
     protected javax.mail.PasswordAuthentication getPasswordAuthentication(){ 
      return new javax.mail.PasswordAuthentication(user,pass); 
     } 

}); 

    try{ 
     MimeMessage message= new MimeMessage(session); 
     message.setFrom(new InternetAddress("[email protected]")); 
     message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]")); 
     message.setSubject("Checking...."); 
     message.setText("Successful",user,pass); 
     Transport.send(message); 
     System.out.println("Sent!"); 
    }catch(Exception ee){ 
     ee.printStackTrace();; 
    } 
Powiązane problemy