2011-04-08 177 views
19

JavaMail指定了一組可用於配置SMTP連接的屬性。要使用STARTTLS,需要設置以下屬性JavaMail smtp屬性(適用於STARTTLS)

mail.smtp.starttls.enable=true 

我在哪裏指定使用smtp服務的用戶名/密碼?是否足以指定:

mail.smtp.user=me 
mail.smtp.password=secret 

或者我必須明確地登錄使用:

transport.connect(server, userName, password) 

是的,我已經嘗試過這樣做,似乎有必要在使用傳輸連接.connect(..)。但是,如果是,那麼mail.smtp.user傳遞屬性是什麼?他們不足以使用starttls使用smtp嗎?

回答

5

你必須繼承身份驗證,並與ENV屬性一起創建一個會話對象的PasswordAuthentication登陸

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

    protected PasswordAuthentication getPasswordAuthentication() { 
     return new PasswordAuthentication("user-name", "user-password"); 
    } 
    }); 

用戶名有時是如Gmail某些服務器完整的電子郵件ID。 希望這有助於。

22

這裏是一個使用的是Gmail SMTP(JavaMail的)與STARTTLS

public void sendEmail(String body, String subject, String recipient) throws MessagingException, 
      UnsupportedEncodingException { 
     Properties mailProps = new Properties(); 
     mailProps.put("mail.smtp.from", from); 
     mailProps.put("mail.smtp.host", smtpHost); 
     mailProps.put("mail.smtp.port", port); 
     mailProps.put("mail.smtp.auth", true); 
     mailProps.put("mail.smtp.socketFactory.port", port); 
     mailProps.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
     mailProps.put("mail.smtp.socketFactory.fallback", "false"); 
     mailProps.put("mail.smtp.starttls.enable", "true"); 

     Session mailSession = Session.getDefaultInstance(mailProps, new Authenticator() { 

      @Override 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication(login, password); 
      } 

     }); 

     MimeMessage message = new MimeMessage(mailSession); 
     message.setFrom(new InternetAddress(from)); 
     String[] emails = { recipient }; 
     InternetAddress dests[] = new InternetAddress[emails.length]; 
     for (int i = 0; i < emails.length; i++) { 
      dests[i] = new InternetAddress(emails[i].trim().toLowerCase()); 
     } 
     message.setRecipients(Message.RecipientType.TO, dests); 
     message.setSubject(subject, "UTF-8"); 
     Multipart mp = new MimeMultipart(); 
     MimeBodyPart mbp = new MimeBodyPart(); 
     mbp.setContent(body, "text/html;charset=utf-8"); 
     mp.addBodyPart(mbp); 
     message.setContent(mp); 
     message.setSentDate(new java.util.Date()); 

     Transport.send(message); 
    } 
+0

您好。你在哪裏輸入實際的用戶名和密碼? – masb 2014-05-01 09:42:07

+1

在Authenticator類的getPasswordAuthentication()方法 – 2014-05-02 04:55:57

+5

嗯,當我設置「... socketFactory.class」它似乎使用SMTP而不是StartTLS而使用SSL,並且我得到「無法識別的SSL消息,明文連接?例外。 – Stroboskop 2016-01-13 11:55:06

4

您可以指定用戶作爲

[email protected] 

(或mail.smtp.user如果你不使用mail.transport.protocol=smtps)我sendEmail方法在您用於會話的屬性中。

AFAIK,你不能提供密碼。但是你當然可以把它放在道具上並自己找回。或者以其他方式獲得它,例如通過提示用戶。

當你有它時,有兩種方式將它提供給會話。更簡單的是使用

Transport tr = session.getTransport(); 
tr.connect(null, password); 
tr.sendMessage(message, message.getRecipients()); 

或者,正如指出的那樣,您可以使用驗證器。但是道具的用戶會被忽略,你必須明確地將它傳遞給PasswordAuthentication。如果你這樣做,那麼你的獎勵是你可以使用靜態的Transport.send

4

你必須在創建

Authenticator authenticator = new PasswordAuthentication("username", "password"); 
Session session = Session.getInstance(properties, authenticator); 

則使用會話來發送消息的會話(跳過try/catch語句,爲了簡潔)提供的認證:

SMTPTransport tr = (SMTPTransport) session.getTransport("smtps"); 
tr.connect(); 
tr.sendMessage(mimeMessage, mimeMessage.getAllRecipients()); 
+0

正如[JavaMail FAQ](https://javaee.github.io/javamail/FAQ.html#commonmistakes)所述,使用Authenticator是可選的,通常很麻煩。 – MauganRa 2017-06-03 15:54:31

1

使用Simple Java Mail它應該很簡單:

Email email = new Email(); 

email.setFromAddress("lollypop", "[email protected]"); 
email.addRecipient("C.Cane", "[email protected]", RecipientType.TO); 
email.setText("We should meet up!"); 
email.setTextHTML("<b>We should meet up!</b>"); 
email.setSubject("hey"); 

new Mailer("smtp.gmail.com", 25, "your user", "your password", TransportStrategy.SMTP_TLS).sendMail(email); 
new Mailer("smtp.gmail.com", 587, "your user", "your password", TransportStrategy.SMTP_TLS).sendMail(email); 
new Mailer("smtp.gmail.com", 465, "your user", "your password", TransportStrategy.SMTP_SSL).sendMail(email); 

如果您有雙重登錄輪流編輯後,您需要從您的Google帳戶生成application specific password


如果你仍然想這個做自己,code behind this庫是非常簡單的。它設置在這取決於TransportStrategy傳遞(普通紙,SSL或TLS)會話特定的性質,它使用一個Authenticator執行認證:

"mail.transport.protocol" : "smtp" 
"mail.smtp.starttls.enable" : "true" 
"mail.smtp.host" : host 
"mail.smtp.port" : port 
"mail.smtp.username" : username 
"mail.smtp.auth" : "true" 

而且

return Session.getInstance(props, new Authenticator() { 
    @Override 
    protected PasswordAuthentication getPasswordAuthentication() { 
     return new PasswordAuthentication(username, password); 
    } 
});