JavaMail - 无法使用SSL发送邮件

时间:2015-06-10 13:17:39

标签: java email ssl javamail

我尝试使用带有office365作为主机的JavaMail启用SSL来发送邮件。当我将主机用作[UniqueTheta, ~, subsTheta]=unique(CylCoorsSample(:,1)) [UniqueH,~,subsH]=unique(CylCoorsSample(:,3)) sz = [numel(UniqueH), numel(UniqueTheta)] Image = accumarray([subsH, subsTheta], PointValuesSample, sz, @mean, NaN) 时,它工作正常但不适用于smtp.gmail.com以下是我的方法:

smtp.office365.com

它将异常抛出为 - public class SendMail { public static void main(String args[]){ String username = "abc@xyz.com"; String password = "abc123"; Properties properties = new Properties(); properties.put("mail.smtp.host", "smtp.office365.com"); properties.put("mail.smtp.socketFactory.port",995); properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.ssl.enable", "true"); Session session = null; if (authenticationRequired) { session = Session.getInstance(properties, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName,password); } }); }else{ session = Session.getDefaultInstance(properties); } try{ MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(username)); message.setSubject("Test mail"); message.setContent("Test Mail", "text/html"); message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(john@gmail.com)); Transport transport = session.getTransport("smtp"); transport.connect(); transport.sendMessage(message,message.getAllRecipients()); }catch(Exception e){ e.printStack(); } } } 。我也试过这个端口为Could not connect to SMTP host: smtp.office365.com, port: 995 response: -1,但这也不起作用。请帮助我。 993

1 个答案:

答案 0 :(得分:0)

正如你所说,使用gmail你是否将gmail的证书导入你的密钥库?