Tuesday, February 11, 2014

Spring Send mail

//Dependencys

<dependency>

<groupId>javax.mail</groupId>

   <artifactId>mail</artifactId>

 <version>1.4</version>
</dependency>

// controller method 

@RequestMapping(value = "sendEmail/mailAddress/{mailAddress}", method = RequestMethod.POST )
 + public String sendEmail(Model model, @PathVariable String mailAddress) {
 +
 + SimpleMailMessage email = new SimpleMailMessage();
 + email.setTo("mail@gmail.com");
 + email.setSubject("Hello");
 + email.setText("Mail Testing....");
 +
 + // sends the e-mail
 + mailSender.send(email);
 + System.out.println("Mail sent...");
 + System.out.println("Mail sent..."+mailAddress);
 + return "regsuccess";
 + }




there is a servlet setting


<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.gmail.com"/>
        <property name="port" value="25"/>
        <property name="username" value="mail@gmail.com"/>
        <property name="password" value="your password"/>
        <property name="javaMailProperties">
            <props>
                <!-- Use SMTP transport protocol -->
                <prop key="mail.transport.protocol">smtp</prop>
                <!-- Use SMTP-AUTH to authenticate to SMTP server -->
                <prop key="mail.smtp.auth">true</prop>
                <!-- Use TLS to encrypt communication with SMTP server -->
                <prop key="mail.smtp.starttls.enable">true</prop>
                <prop key="mail.debug">true</prop>
            </props>
        </property>
    </bean>