0

私はJava Spring、Mavenに取り組んでおり、gmail idを使用してgmailでメールを送信しようとしています。このエラーが発生しています:

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. 11sm885884pfp.38 - gsmtp

パラメータを設定しても

mail.smtp.starttls.enable

真に。

これは私の機能です:

public String sendMail(String to, String subject, String textMessage) throws IOException {
    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.auth", "true");
    Session mailSession = Session.getInstance(props);
    mailSession.setDebug(true);
    Message simpleMessage = new MimeMessage(mailSession);

    InternetAddress fromAddress = from@gmail.com;
    InternetAddress toAddress = null;
    try {
        toAddress = new InternetAddress(to);
    } catch (AddressException e) {
        System.out.print("\nError in Address of Sender or reciever\n");
        e.printStackTrace();
    }
    try {
        simpleMessage.setFrom(fromAddress);
        simpleMessage.setRecipient(RecipientType.TO, toAddress);
        simpleMessage.setSubject(subject);
        simpleMessage.setText(textMessage);
        SMTPTransport t = (SMTPTransport) mailSession.getTransport("smtp");
        t.setStartTLS(true);
        t.connect("smtp.gmail.com", "from@gmail.com", "from.password!");
        t.sendMessage(simpleMessage, simpleMessage.getAllRecipients());
        t.close();

    } catch (MessagingException e) {             
        System.out.print("\nError in message Genration\n");
        e.printStackTrace();
    }
    return "success";
}

SO に関するすべての回答は、mail.smtp.starttls.enable を true に設定するように指示しています。それをしてもうまくいきません。

また、次のように pom に javax.mail 依存関係を含めました。

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4</version>
</dependency>
4

1 に答える 1

0

javax.mailSpring を使用している場合は、低レベルの APIではなく、それらの抽象化を組み込むことをお勧めします。実際、JavaMailSenderとその実装を使用しJavaMailSenderImplて、同じ結果を得ることができます。例については、これを参照してください。

于 2015-12-25T16:27:10.473 に答える