我编写了整个代码,并导入了邮件,激活,com.sun.mail和smtp jar,但是在starttls中仍然给我一个错误,这是错误: javax.mail.MessagingException:必需STARTTLS,但是主机不支持STARTTLS,
boolean sessionDebug=false;
Properties props=System.getProperties();
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "465");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.required", "true");
final javax.mail.Session mailSession = javax.mail.Session.getInstance(props);
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
props.put("mail.smtp.debug", "true");
//Session mailSession=Session.getDefaultInstance(props,null);
mailSession.setDebug(sessionDebug);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address={new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO,address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(msgText);
Transport transport =mailSession.getTransport("smtp");
System.out.println("smtp");
transport.connect(host,user,pass);
System.out.println("smtp connection");
transport.sendMessage(msg,msg.getAllRecipients());
System.out.println("smtp message");
transport.close();
答案 0 :(得分:0)
如果您的邮件服务器不支持STARTTLS,则不能使用它。尝试改为设置mail.smtp.ssl.enable
。
还要修复所有这些common JavaMail mistakes。