詳細

2013-07-25 70 views
0

發送郵件通過Java程序我不能發送電子郵件,我認爲可能會有一些問題ü可以請檢查代碼詳細

*import java.util.Date; 
import java.util.Properties; 
import javax.mail.Address; 
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 Mail { 

    public static void sendReminder(String Message) throws AddressException, MessagingException{ 

      String to="[email protected]"; 
      String password="784518"; 
      String from = "[email protected]"; 
      String subject = "Reminder"; 



      String body = "Dear Sir"; 
      Properties props = new Properties(System.getProperties()); 
      // -- Attaching to default Session, or we could start a new one -- 
      props.put("mail.smtp.host", "smtp.gmail.com"); 
      SMTPAuthenticator smtpAuth = null; 
      Session session = Session.getDefaultInstance(props, smtpAuth); 
      // -- Create a new message -- 
      javax.mail.Message msg = new MimeMessage(session); 
      // -- Set the FROM and TO fields -- 
      try { 
      msg.setFrom(new InternetAddress(from)); 

      msg.setRecipients(javax.mail.Message.RecipientType.TO, 
      InternetAddress.parse(username, false));     
      msg.setSubject(subject); 
      msg.setContent(body, "text/html"); 
      // -- Set some other header information -- 
      msg.setSentDate(new Date()); 
      // -- Send the message -- 
      Transport.send(msg); 
      //MessageDao.addToHistory(task, new Date(), remindBefore); 
      }catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

    } 
    private static class SMTPAuthenticator extends javax.mail.Authenticator { 
     private PasswordAuthentication authentication; 
     public SMTPAuthenticator(String username,String password) {   
      authentication = new PasswordAuthentication(username, password); 
     } 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return authentication; 
     } 
    } 

    public static void main(String args[]) throws AddressException, MessagingException 
    { 
     Mail m=new Mail(); 


     Mail.sendReminder("this is a test mail"); 

    } 
}* 



com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. 7sm50596735paf.22 - gsmtp 

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388) 
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959) 

以下是執行該程序後錯誤。 ..我想有一些問題在那裏,如果有可能請檢查它

+0

這可能是一個愚蠢的http://stackoverflow.com/questions/10509699/must-issue-a-starttls-command-first –

回答

0

我認爲你只需要指定正確的端口,安全的電子郵件。添加props.put(「mail.smtp.port」,「587」);