Вопрос

Hi im using Spring framework to send the mail . I dont know about mail server settings. We are using mozilla thunderbird. I wrote a sample mail sending application in spring. I googled and came to a conclusion that we need a server host and port. i have set all those, But my problem is its getting the following exception.

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");

    }

Please can anyone tell me the solution? Thanks

Это было полезно?

Решение

It seems that Java cannot connect to the SMTP server mail.supremecluster.com:143

I am guessing that the mail settings you have provided are not the ones for SMTP (SMTP is only for outgoing email messages, IMAP is for receiving messages). Why don't you open Thunderbird and under Account Settings check and see what the settings are in the Outgoing Server (SMTP) item.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top