0

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

Ashot Karakhanyan
  • 2,804
  • 3
  • 23
  • 28

1 Answers1

0

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.

geoand
  • 60,071
  • 24
  • 172
  • 190
  • I have checked in thunderbird outgoing settings. Server name are same but i have the port number different in outgoing settings.Now i have the following error after changing port. Exception in thread "main" org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not convert socket to TLS;` –  Mar 13 '14 at 07:15
  • Use the exact same settings that you have in the Outgoing Server in your code. – geoand Mar 13 '14 at 07:17
  • he following error occurs : `Exception in thread "main" org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not convert socket to TLS;` –  Mar 13 '14 at 07:21
  • Try setting the property mail.smtp.starttls.enable to true – geoand Mar 13 '14 at 07:27
  • Your right, I didn't see that. I guess the server is not accepting TLS secured connections. What happens if you set the setting to false? – geoand Mar 13 '14 at 07:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/49628/discussion-between-vignesh-and-geoand) –  Mar 13 '14 at 07:32