通过Spring发送imap邮件服务器的邮件?

时间:2014-03-13 05:33:29

标签: java spring mail-sender

您好我使用Spring框架发送邮件。我不知道邮件服务器设置。我们正在使用mozilla雷鸟。我在春天写了一个样本邮件发送应用程序。我用谷歌搜索并得出结论,我们需要一个服务器主机和端口。我设置了所有这些,但我的问题是它得到以下异常。

Exception in thread "main" org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: mail.supremecluster.com, port: 143, response: -1
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:418)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:307)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:297)
    at com.javacodegeeks.spring.mail.MailService.sendMail(MailService.java:24)
    at com.javacodegeeks.spring.mail.MailServiceTest.main(MailServiceTest.java:14)
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: mail.supremecluster.com, port: 143, response: -1
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1694)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
    at javax.mail.Service.connect(Service.java:291)
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:388)
    ... 4 more

Spring.xml

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="mail.supremecluster.com" />
    <property name="port" value="143" />
    <property name="username" value="myusername@host.com" />
    <property name="password" value="*********" />
    <property name="javaMailProperties">
        <props>
            <prop key="mail.transport.protocol">smtp</prop>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.starttls.enable">true</prop>
            <prop key="mail.debug">true</prop>
                 </props>
    </property>
</bean>

MailService的

public void sendMail(String from, String to, String subject, String body) {

    SimpleMailMessage message = new SimpleMailMessage();

    message.setFrom(from);
    message.setTo(to);
    message.setSubject(subject);
    message.setText(body);
    mailSender.send(message);

}

public void sendAlertMail(String alert) {

    SimpleMailMessage mailMessage = new SimpleMailMessage(alertMailMessage);
    mailMessage.setText(alert);
    mailSender.send(mailMessage);

}

Mailltest

public static void main(String[] args) {

        ApplicationContext context = new FileSystemXmlApplicationContext("conf/spring.xml");

        MailService mailService = (MailService) context.getBean("mailService");

        mailService.sendMail("myusername@host.com", "myuser@gmail.com",
                "Testing123", "Testing only \n\n Hello Spring Email Sender");

        mailService.sendAlertMail("Exception occurred");

    }

请有人告诉我解决方案吗?感谢

1 个答案:

答案 0 :(得分:0)

似乎Java无法连接到SMTP服务器mail.supremecluster.com:143

我猜您提供的邮件设置不是SMTP的邮件设置(SMTP仅用于传出电子邮件,IMAP用于接收邮件)。 为什么不打开Thunderbird并在“帐户设置”下检查并查看“发送服务器(SMTP)”项目中的设置。