2012-09-19 219 views
0

我想從我的Java類發送郵件,但不斷得到MessangingException。 我認爲它與認證問題有關,但我無法弄清楚問題所在。使用java郵件API發送郵件

以下是異常的堆棧跟蹤。

Sep 19, 2012 7:27:30 PM me.uni.sushilkumar.geodine.util.Mailer send 
SEVERE: null 
javax.mail.SendFailedException: Sending failed; 
    nested exception is: 
    javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first.  ko8sm1883776pbc.40 

    at javax.mail.Transport.send0(Transport.java:219) 
at javax.mail.Transport.send(Transport.java:81) 
at me.uni.sushilkumar.geodine.util.Mailer.send(Mailer.java:61) 
at me.uni.sushilkumar.geodine.util.Mailer.main(Mailer.java:73) 

我送使用下面的代碼的消息

public class Mailer { 
private Session session; 
private final String username; 
private final String password; 
public Mailer(String username,String password) 
{ 
    this.username=username; 
    this.password=password; 
    init(username,password); 
} 

public final void init(String username,String password) 
{ 
    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.getInstance(props, new Authenticator() 
    { 
     @Override 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication(Mailer.this.username,Mailer.this. password); 
     } 
    }); 

} 

public boolean send(String recipient,String subject,String body) 
{ 
    boolean status=false; 
    try { 

     Message message=new MimeMessage(session); 
     message.setFrom(new InternetAddress(username)); 
     message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient)); 
     message.setSubject(subject); 
     message.setText(body); 
     Transport.send(message); 
     status=true; 

    } catch (MessagingException ex) { 
     Logger.getLogger(Mailer.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    return status; 
} 

public static void main(String[] args) 
{ 
    Mailer mailer=new Mailer("ex[email protected]","password"); 
    boolean status=mailer.send("[email protected]", "Demo Mail", "This is a demo message"); 
} 

} 

任何人可以建議我有什麼不對的代碼?

+0

'props.put(「mail.smtp.socketFactory.port」,d_port);'這是什麼d_port? – kaysush

+1

選中此鏈接(http://www.oracle.com/technetwork/java/faq-135477.html#starttls)。 – Santosh

回答

1

做檢查這個蘇答案 - 他們指定套接字工廠明確地使用SSL套接字,您可能需要:

Using JavaMail with TLS

這可能是足以讓你和運行。

+0

當我添加此'props.put(「mail.smtp.socketFactory.class」,「javax.net.ssl.SSLSocketFactory」); props.put(「mail.smtp.socketFactory.fallback」,「false」);' 現在它給了我'無法連接到SMTP主機:smtp.gmail.com,端口:587'異常 – kaysush

+0

解決了錯誤,因爲Gmail使用端口465進行啓用SSL的身份驗證。謝謝 – kaysush

+0

[請勿使用套接字工廠。](http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes) –

-1
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 Send_Mail_Dao 

{ 

private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; 

    public static boolean Send_Mail(String to,String subject,String mess) 

    { 

     final String username = "[email protected]"; 

     final String password = "password"; 

       boolean flag=false; 

     Properties props = new Properties(); 

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

     props.put("mail.smtp.auth", "true"); 

     props.put("mail.debug", "true"); 

     props.put("mail.smtp.port", "465"); 

     props.put("mail.smtp.socketFactory.port", "465"); 

     props.put("mail.smtp.socketFactory.class", SSL_FACTORY); 

     props.put("mail.smtp.socketFactory.fallback", "false"); 

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

        { 
        @Override 
       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(subject); 
      message.setText(mess); 

      Transport.send(message); 
          flag=true; 
      System.out.println("Done"); 

      } 
     catch (MessagingException e) 
     { 
      throw new RuntimeException(e); 
     } 
       return flag; 
    } 
} 
相關問題