如何使用gmail smtp发送邮件Spring实现?

执行main方法后异常
线程“main”中的异常 java.lang.NoClassDefFoundError: javax/activation/FileTypeMap

public static void main(String[] args) {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost("smtp.gmail.com");
        sender.setPort(25);
        sender.setPassword("xxxxxxx");
        sender.setUsername("[email protected]");

        MimeMessage message = sender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message);
            helper.setTo("[email protected]");
            helper.setText("Thank you for ordering!");
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        sender.send(message);
    }

将 activation.jar 放在类路径中后得到此异常
javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. 21sm3277189pzk.7

最佳答案

在更短的修订版 Saurabh 帖子中,您可以简单地:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl" p:host="smtp.gmail.com"
    p:port="587" p:username="[email protected]" p:password="aSmartPassWord">
    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.starttls.enable">true</prop>
            <prop key="mail.smtp.timeout">8500</prop>
        </props>
    </property>
</bean>

关于java - 如何使用gmail smtp发送邮件Spring实现?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2256699/

10-10 14:09