从域名地址发送电子邮件

时间:2018-01-23 09:15:39

标签: java api email java-8 javamail

我如何从域名地址发送信件?我无法得到它。它显示

DEBUG: getProvider() returning 

 javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
 DEBUG SMTP: useEhlo true, useAuth true
 DEBUG SMTP: useEhlo true, useAuth true
 DEBUG SMTP: trying to connect to host "smtp.yandex.ru", port 995, isSSL false

它无法连接到主机,但端口是正确的。 从域发送电子邮件的属性应该是什么? 我的是:

Properties props = new Properties();
        props.put("mail.smtp.starttls.enable", "true");//Enable tls session
        props.put("mail.smtp.auth", "true");//Enable authentication
        props.put("mail.smtp.host", "smtp.yandex.ru");//Server's host
        props.put("mail.smtp.port", "995");//Server's port

我有[某事] @ domain.ru

    Properties props = new Properties();
        props.put("mail.smtp.starttls.enable", "true");//Enable tls session
        props.put("mail.smtp.auth", "true");//Enable authentication
        props.put("mail.smtp.host", "smtp.yandex.ru");//Server's host
        props.put("mail.smtp.port", "995");//Server's port

    Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("name@domain.ru", "password102030");
                    }
                });
        session.setDebug(true);


    try {

        Scanner to = new Scanner(toWho);
        while (to.hasNextLine())
        {
            String touser = to.nextLine();

        try {
             if (howMany <= batch)
        {
            howMany++;
            System.out.println("Задержка "+delayevery/1000+" секунд");
            Thread.sleep(delayevery);
        }
        else
        {
            howMany = 0;
            System.out.println("Задержка "+delaybatch/1000+" секунд");
            Thread.sleep(delaybatch);
        }
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username,alias));

            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(touser));
            message.setSubject(subject);


             // Create the message part
     BodyPart messageBodyPart = new MimeBodyPart();
     BodyPart imagePart = new MimeBodyPart();

     if (image != "")
     imagePart.setContent("<img src=\""+image+"\">","text/html");
     // Now set the actual message
     messageBodyPart.setText(MailSender.message);

     // Create a multipar message
     Multipart multipart = new MimeMultipart();
     //Set image message part
     multipart.addBodyPart(imagePart);
     // Set text message part
     multipart.addBodyPart(messageBodyPart);

     // Part two is attachment
     messageBodyPart = new MimeBodyPart();

     if (attaching != "")      
      {
     DataSource source = new FileDataSource(attaching);
     messageBodyPart.setDataHandler(new DataHandler(source));

     messageBodyPart.setFileName(javax.mail.internet.MimeUtility.encodeWord(source.getName(),
            "UTF-8", null));
     multipart.addBodyPart(messageBodyPart);
      }
     // Send the complete message parts
     message.setContent(multipart);


            Transport.send(message);

            System.out.println("Done "+(++counter));

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }   catch (UnsupportedEncodingException ex) {
                Logger.getLogger(MailSender.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    } catch (FileNotFoundException ex) {
        System.out.print("База была не найдена.");
    } catch (InterruptedException ex) {
        System.out.print("Что-то неверно.");
    }
}

2 个答案:

答案 0 :(得分:1)

端口995是POP3 over SSL端口。您可能需要端口465,这是SMTP over SSL端口。或者更有可能的是,由于您已经设置了starttls属性,因此您只需要端口25,或者可能需要端口587.这些是纯文本SMTP端口。 starttls属性将在连接到端口后启用SSL / TLS。

答案 1 :(得分:0)

我找到了答案。你必须启用ssl session:

props.put("mail.smtp.ssl.enable", "true");