javax.mail.MessagingException:无法连接到SMTP主机:smtp.gmail.com,port:587,响应:421

时间:2016-01-21 12:22:27

标签: java email javamail

我正在尝试从java发送电子邮件,我收到上述错误。我已将端口号更改为465并且错误仍然存​​在。错误是由于任何内部系统属性?我试过的代码是

    public class Sendmail{
    static String success="";
    public static String sendMail(String to,String subject,String content)
    {

    //JOptionPane.showMessageDialog(null, "inside"+to);
           final String username = "samplemail@gmail.com";
    final String password = "*********";
            //String sub=subject;
            //String cont=content;
    Properties props = new Properties();

    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

            System.out.println(props.getProperty("mail.smtp.host"));
    Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
      });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("keepitsimplesafe@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
        InternetAddress.parse(to));
        message.setSubject(subject);
        message.setText(content);

        Transport.send(message);
                   success="Details Send Successfully";

    } catch (Exception e) {
        throw new RuntimeException(e);
    }

return success;
}

public static void main(String[] args) {
        Sendmail send=new Sendmail();
        System.out.println(send.sendMail("mypromini2016@gmail.com","hdfh","CHecking MAiling System"));


  }}

0 个答案:

没有答案