2013-05-03 137 views
0

我需要將郵件從我的Gmail帳戶發送到另一個郵件帳戶。我使用了下面的代碼。使用smtp發送郵件時出錯

   String fromaddress = "[email protected]"; 
    String password = "yyy"; 
    String hostname = "smtp.gmail.com"; 
    String hoststring = "mail.smtp.host"; 
    String toaddress = "[email protected]"; 
    String emailcontent; 

    Properties properties = System.getProperties(); 
    properties.setProperty(hoststring, hostname); 
    Session session = Session.getDefaultInstance(properties); 

    try{ 
     MimeMessage message = new MimeMessage(session); 
     message.setFrom(new InternetAddress(fromaddress)); 
     message.addRecipient(Message.RecipientType.TO, new InternetAddress(toaddress)); 
     message.setSubject("hi"); 
     emailcontent = "hi..."; 
     message.setText(emailcontent); 
     System.out.println(emailcontent); 
     Transport.send(message); 
     System.out.println("Sent...."); 
    }catch (MessagingException mex) 
    { 
     mex.printStackTrace(); 
    } 

,但我得到的錯誤如下... javax.mail.MessagingException的:無法連接到SMTP主機:smtp.gmail.com端口:25

,我該怎樣解決這個問題。你能幫我解決嗎?

回答

1
public static void sendEmail(Email mail) { 

    String host = "smtp.gmail.com"; 
    String from = "YOUR_GMAIL_ID"; 
    String pass = "YOUR_GMAIL_PASSWORD"; 
    Properties props = System.getProperties(); 
    props.put("mail.smtp.starttls.enable", "true"); // added this line 
    props.put("mail.smtp.host", host); 
    props.put("mail.smtp.user", from); 
    props.put("mail.smtp.password", pass); 
    props.put("mail.smtp.port", "587"); 
    props.put("mail.smtp.auth", "true"); 

    // Get the default Session object. 
    Session session = Session.getDefaultInstance(props, null); 

    try { 
     // Create a default MimeMessage object. 
     MimeMessage message = new MimeMessage(session); 

     // Set sender 
     message.setFrom(new InternetAddress("Senders_EMail_Id")); 

     // Set recipient 
     message.addRecipient(Message.RecipientType.TO, new InternetAddress("RECIPIENT_EMAIL_ID")); 

     // Set Subject: header field 
     message.setSubject("SUBJECT"); 

     // set content and define type 
     message.setContent("CONTENT", "text/html; charset=utf-8"); 

     Transport transport = session.getTransport("smtp"); 
     transport.connect(host, from, pass); 
     transport.sendMessage(message, message.getAllRecipients()); 
     transport.close(); 
     } catch (MessagingException mex) { 
     System.out.println(mex.getLocalizedMessage()); 
    } 

} 

}`

我想這應該做的伎倆。

1

我認爲你需要改變端口號。 25至587

你可以從

http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/

Gmail的設置幫助聯繫獲得幫助:

http://gmailsmtpsettings.com/

+0

嗨,我試着做這個配置。我將郵件主機更改爲「smtp.googlemail.com」並在thunderbird中配置了所需的端口。傳出的SMTP端口僅爲587。但是,該代碼會引發以下錯誤。 javax.mail.MessagingException:無法連接到SMTP主機:smtp.googlemail.com,端口:25; – user1551550 2013-05-03 10:02:35

1

只是增加一些更多的調整,以上面的問題:

變化將端口設爲465以啓用ssl發送。

我不認爲上面的代碼可以工作,因爲你也需要一個認證對象。由於smtp在gmail的情況下也需要身份驗證。

你可以這樣做:

有一個布爾標誌,

boolean authEnable = true; //True for gmail 
boolean useSSL = true; //For gmail 
//Getters and setters for the same 

    if (isUseSSL()) { 
     properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
     properties.put("mail.smtp.socketFactory.port", "465"); 
    } 

Authenticator authenticator = new javax.mail.Authenticator() { 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication("[email protected]", "xyz")); 
     } 
    }; 
    if (isAuthEnable()) { 
     properties.put("mail.smtp.auth", "true"); 
     session = Session.getDefaultInstance(properties, authenticator); 
    } else { 
     session = Session.getDefaultInstance(properties); 
    }