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) 

是的,我已经尝试过这样做,似乎有必要使用transport.connect(..)进行连接。 但如果是,mail.smtp.user和pass属性是什么? 他们还不足以使用smtp和starttls吗?

这是我的sendEmail方法,它使用带有STARTTLS的GMail smtp(JavaMail)

 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); } 

您必须inheritanceAuthenticator并为Session创建PasswordAuthentication对象以及要登录的env属性

 Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("user-name", "user-password"); } }); 

用户名有时是某些服务器(如gmail)的完整电子邮件ID。 希望这可以帮助。

您可以将用户指定为

 mail.smtps.user=cayhorstmann@gmail.com 

(或者,如果不使用mail.transport.protocol=smtps )在用于会话的属性中。

AFAIK,您无法提供密码。 但你可以把它放在道具中并自己检索。 或者以其他方式获取它,例如通过提示用户。

如果有,有两种方法可以将它提供给会话。 更简单的是使用

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

或者,正如所指出的,您可以使用身份validation器。 但是然后忽略了来自props的用户,你必须明确地将它传递给PasswordAuthentication 。 如果你这样做,那么你的奖励是你可以使用静态Transport.send

您必须在创建会话时提供身份validation器

 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()); 

使用Simple Java Mail应该很简单:

 Email email = new Email(); email.setFromAddress("lollypop", "lol.pop@somemail.com"); email.addRecipient("C.Cane", "candycane@candyshop.org", RecipientType.TO); email.setText("We should meet up!"); email.setTextHTML("We should meet up!"); 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帐户生成应用程序专用密码 。


如果你仍然想自己这样做, 这个库背后的代码非常简单。 它根据传递的TransportStrategy (plain,ssl或tls)设置Session的特定属性,并使用Authenticator执行身份validation:

 "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); } });