我有这个代码使用SSL和公司邮件服务器发送邮件。
public static void Send(final String username, final String password, String recipientEmail, String ccEmail, String title, String message) throws AddressException, MessagingException {
System.setProperty("java.net.useSystemProxies", "true");
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
// Get a Properties object
Properties props = System.getProperties();
props.setProperty("mail.smtps.starttls.enable", "true");
props.setProperty("mail.smtps.host", "mail.company.au");
props.setProperty("mail.smtps.user", username);
props.setProperty("mail.smtps.password", password);
props.setProperty("mail.smtps.port", "587");
props.setProperty("mail.smtps.auth", "true");
props.setProperty("mail.smtps.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtps.timeout", "5000");
props.setProperty("mail.smtps.connectiontimeout", "5000");
props.setProperty("mail.smtps.writetimeout", "5000");
Session session = Session.getInstance(props,null);
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(username));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail, false));
if (ccEmail.length() > 0) {
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccEmail, false));
}
msg.setSubject(title);
msg.setText(message, "utf-8");
msg.setSentDate(new Date());
SMTPTransport t = (SMTPTransport)session.getTransport("smtps");
t.connect("mail.company.au", username, password);
t.sendMessage(msg, msg.getAllRecipients());
t.close();
}
但我得到这个错误可以有人帮忙吗?
DEBUG SMTP:异常读取响应:javax.net.ssl.SSLException: 无法识别的SSL消息,明文连接?
答案 0 :(得分:0)
这是我几个月前发现的一些片段。(我不记得到底在哪里)。
public static void sendMail(String recipient, String subject, String content) {
// config
// Sender's email ID
String from = "YOUR SENDER ADDRESS HERE";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtps.host", "PLACE YOUR SERVER DOMAIN e.g. smtps.example.com");
properties.setProperty("mail.smtps.port", "465");
properties.setProperty("mail.smtps.auth", "true");
properties.setProperty("mail.user", "YOUR USERNAME");
properties.setProperty("mail.password", "YOUR SMTP PASSWORD");
//properties.put("mail.debug", "true");
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(recipient));
message.setSubject(subject);
message.setText(content);
Transport trnsport;
trnsport = session.getTransport("smtps");
trnsport.connect(null, properties.getProperty("mail.password"));
message.saveChanges();
trnsport.sendMessage(message, message.getAllRecipients());
trnsport.close();
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}