Program-Me-Rev
Program-Me-Rev

Reputation: 6624

How to send Emails via Spring

In my JavaFx project I inject my controllers like so, which works fine:

@Configuration
public class AppFactory {

    @Bean
    public HomeController homeController() throws IOException {
        return (HomeController) loadController("/wakiliproject/Home.fxml");
    }

    FXMLLoader loader = null;

    protected Object loadController(String url) throws IOException {
        loader = new FXMLLoader(getClass().getResource(url));
        loader.load();
        return loader.getController();
    }
}

However, for non-controller classes, I get a null pointer exception, probably because the class never gets injected. Say, for example, I try to call a method in a non-controller class from another non-controller class like so:

Sending email via Spring, for example:

public class HomeController extends Application {

    @Autowired
    MailMail mailMail;


    // Send email here
    public void sendEmailMethod() {
        System.out.println("Here ----------------------------------------------------------------");
        mailMail.sendMail("[email protected]", "[email protected]", "Hello", "Subjecto");

    }

    // The other class methods

The other classes:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">  
    <property name="host" value="smtp.gmail.com" />  
    <property name="username" value="[email protected]" />  
    <property name="password" value="gmailpassword" />  
    <property name="javaMailProperties">  
        <props>  
            <prop key="mail.smtp.auth">true</prop>  
        </props>  
    </property>  
</bean>

<bean id="mailMail" class="wakiliproject.ServerSide.EMail.MailMail">  
    <property name="mailSender" ref="mailSender" />  
</bean>

--

public class MailMail {

    private MailSender mailSender;

    public void setMailSender(MailSender mailSender) {
        this.mailSender = mailSender;
    }

    public void sendMail(String from, String to, String subject, String msg) {
        //creating message  
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(msg);
        //sending message  
        mailSender.send(message);
    }
}

The stacktrace:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:367)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:305)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:894)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:56)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:158)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.mail.MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. k10sm2188955lae.42 - gsmtp
; message exceptions (1) are:
Failed message 1: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. k10sm2188955lae.42 - gsmtp

    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:448)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:308)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:297)
    at wakiliproject.ServerSide.EMail.MailMail.sendMail(MailMail.java:22)
    at wakiliproject.HomeController$HomeInit.homeInit(HomeController.java:618)
    at wakiliproject.HomeController$HomeInit.access$100(HomeController.java:613)
    at wakiliproject.HomeController.initHomeController(HomeController.java:319)
    at wakiliproject.WakiliProject.start(WakiliProject.java:34)
    at com.sun.javafx.application.LauncherImpl$8.run(LauncherImpl.java:837)
    at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:335)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:298)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
    at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
    ... 1 more
Exception running application wakiliproject.WakiliProject
Java Result: 1

Upvotes: 2

Views: 6425

Answers (2)

steineron
steineron

Reputation: 419

had a similar problem... got me some attempts to configure it right in my application.properties (I'm injecting JavaMailSender using spring)

spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.auth = true;
spring.mail.properties.mail.socketFactory.port=587
spring.mail.properties.mail.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.socketFactory.fallback=false

later i was required to enable looser security to my account during the test:

"change your settings at https://www.google.com/settings/security/lesssecureapps so that your account is no longer protected by modern security standards"

or i got an authentication error. I hope that helps

Upvotes: 6

Mailkov
Mailkov

Reputation: 1231

According to Mladen Uzelac You need to set some properties for your SMTP connection.

Link related: http://forum.spring.io/forum/spring-projects/web/68425-javax-mail-messagingexception-530-5-7-0-must-issue-a-starttls-command-first-5sm505

Upvotes: 3

Related Questions