我们的产品实际上有一些通知代码,如果有可用的话,它会使用TLS发送邮件。
您需要设置Java Mail属性。您只需要TLS,但如果您的SMTP服务器使用SSL,则可能需要SSL。
Properties props = new Properties(); props.put("mail.smtp.starttls.enable","true"); props.put("mail.smtp.auth", "true"); // If you need to authenticate // Use the following if you need SSL props.put("mail.smtp.socketFactory.port", d_port); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.socketFactory.fallback", "false");
然后,您可以将其传递给JavaMail会话或任何其他会话实例化器 Session.getDefaultInstance(props) 。
Session.getDefaultInstance(props)
上面示例中的设置对我使用的服务器不起作用( authsmtp.com )。我一直收到这个错误:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
我删除了mail.smtp.socketFactory设置,一切正常。最终设置是这样的(没有使用SMTP身份验证,我在其他地方设置了端口):
java.util.Properties props = new java.util.Properties(); props.put("mail.smtp.starttls.enable", "true");
同 简单的Java Mail 5.0.0 (simplejavamail.org)它非常简单,库将为您处理所有Session属性。
以下是使用Google的SMTP服务器的示例:
Email email = EmailBuilder.startingBlank() .from("lollypop", "lol.pop@somemail.com") .to("C.Cane", "candycane@candyshop.org") .withSubject("hey") .withPlainText("We should meet up!") .withHTMLText("<b>We should meet up!</b>") .buildEmail(); MailerBuilder.withSMTPServer("smtp.gmail.com", 25, "user", "pass", SMTP_TLS) .buildMailer() .sendMail(email); MailerBuilder.withSMTPServer("smtp.gmail.com", 587, "user", "pass", SMTP_TLS) .buildMailer() .sendMail(email); MailerBuilder.withSMTPServer("smtp.gmail.com", 465, "user", "pass", SMTP_SSL) .buildMailer() .sendMail(email);
如果您启用了双因素登录,则需要生成一个 应用专用密码 来自您的Google帐户。
只需使用以下代码即可。通过Java发送电子邮件非常有用,它可以工作:
import java.util.*; import javax.activation.CommandMap; import javax.activation.MailcapCommandMap; import javax.mail.*; import javax.mail.Provider; import javax.mail.internet.*; public class Main { public static void main(String[] args) { final String username="your@gmail.com"; final String password="password"; Properties prop=new Properties(); prop.put("mail.smtp.auth", "true"); prop.put("mail.smtp.host", "smtp.gmail.com"); prop.put("mail.smtp.port", "587"); prop.put("mail.smtp.starttls.enable", "true"); Session session = Session.getDefaultInstance(prop, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { String body="Dear Renish Khunt Welcome"; String htmlBody = "<strong>This is an HTML Message</strong>"; String textBody = "This is a Text Message."; Message message = new MimeMessage(session); message.setFrom(new InternetAddress("your@gmail.com")); message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("receiver@gmail.com")); message.setSubject("Testing Subject"); MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap(); mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html"); mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml"); mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain"); mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed"); mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822"); CommandMap.setDefaultCommandMap(mc); message.setText(htmlBody); message.setContent(textBody, "text/html"); Transport.send(message); System.out.println("Done"); } catch (MessagingException e) { e.printStackTrace(); } } }