0
votes

I am trying to use javamail in spring mvc 4 to send an email using gmail but got some error there. The error says...

Info: Receipient?= [email protected], Subject?= User Verification, Message?= Your pin is 158046

Info: DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]

Info: DEBUG SMTP: useEhlo true, useAuth true

Info: DEBUG SMTP: trying to connect to host "smtp.mail.gmail.com", port 587, isSSL false

Severe: PWC6117: File "null" not found

Here is my spring configuration

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="smtp.mail.gmail.com" />
    <property name="port" value="587" />
    <property name="username" value="[email protected]" />
    <property name="password" value="*******" />
    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.debug">true</prop>
            <prop key="mail.transport.protocol">smtp</prop>
            <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
            <prop key="mail.smtp.socketFactory.port">465</prop>
            <prop key="mail.smtp.starttls.enable">true</prop>
        </props>
    </property>
</bean>

This is how i made a controller

   @Controller
public class EmailController {

    private String emailToRecipient = "";
    private String emailSubject = "";
    private String emailMessage = "";
    private final String emailFromRecipient = "[email protected]";

    @Autowired
    private JavaMailSender mailSenderObj;

    @Autowired
    GeneratePIN generatePin;
    
    // This Method Is Used To Prepare The Email Message And Send It To The Client
    @RequestMapping(value = "/create-account", method = RequestMethod.POST)
    public @ResponseBody String sendEmailToClient() {

        // Reading Email Form Input Parameters      
        emailSubject = "User Verification";
        emailMessage = "Your pin is "+generatePin.pin();
        emailToRecipient = "[email protected]";

        // Logging The Email Form Parameters For Debugging Purpose
        System.out.println("\nReceipient?= " + emailToRecipient + ", Subject?= " + emailSubject + ", Message?= " + emailMessage + "\n");

        mailSenderObj.send(new MimeMessagePreparator() {
            @Override
            public void prepare(MimeMessage mimeMessage) throws Exception {

                MimeMessageHelper mimeMsgHelperObj = new MimeMessageHelper(mimeMessage, true, "UTF-8");
                mimeMsgHelperObj.setTo(emailToRecipient);
                mimeMsgHelperObj.setFrom(emailFromRecipient);
                mimeMsgHelperObj.setText(emailMessage);
                mimeMsgHelperObj.setSubject(emailSubject);

            }
        });
        System.out.println("\nMessage Send Successfully.... Hurrey!\n");

        return "Thank You! Your Email Has Been Sent!";
    }
}
1

1 Answers

2
votes

The problem is solved after updating my spring configuration as follow:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="smtp.gmail.com" />
    <property name="port" value="587" />
    <property name="username" value="[email protected]" />
    <property name="password" value="********" />

    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.starttls.enable">true</prop>
        </props>
    </property>
</bean>