【问题标题】:Sending email from domain't address从域的地址发送电子邮件
【发布时间】:2018-07-02 01:15:52
【问题描述】:

如何从域的地址发送一封信?我就是不明白。它显示

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

我有 [something]@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("Что-то неверно.");
    }
}

【问题讨论】:

    标签: java api email java-8 jakarta-mail


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我找到了答案。您必须启用 ssl 会话:

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

      【讨论】:

        猜你喜欢
        • 2013-01-20
        • 2014-06-18
        • 1970-01-01
        • 1970-01-01
        • 2015-06-27
        • 2017-06-07
        • 2016-11-04
        • 2014-04-28
        • 1970-01-01
        相关资源
        最近更新 更多