Balasaheb
Balasaheb

Reputation: 635

How to send mails using Spring Framework

I am working on Spring based project; I am looking to implement an use-case in which I can send email to specific userId, As I know I can send mail using SimpleMailMessage Interface and MailSender Class of SpringFramework. Is there other way to do same one? Are there any references available for more specific study...?

Upvotes: 1

Views: 2221

Answers (2)

Ralph
Ralph

Reputation: 120761

@Service
public void MailDemo() {

  @Resource
  private JavaMailSender sender;

  public void sendDemo() {
     MimeMessage message = sender.createMimeMessage();
     MimeMessageHelper helper = new MimeMessageHelper(message, false);
     helper.setTo("[email protected]");
     helper.setSubject("Demo");
     helper.setText("Demo...");

     sender.send(message);
  }
}


<bean id="mailSender"
      class="org.springframework.mail.javamail.JavaMailSenderImpl">
   <property name="javaMailProperties" ref="javaMailProperties"/>
   <property name="password" value="${smtp.password}"/>
   <property name="username" value="${smtp.username}"/>     
</bean>

<util:properties id="javaMailProperties">
   <prop key="mail.store.protocol">imap</prop>
   <prop key="mail.debug">false</prop>
   <prop key="mail.auth">true</prop>
   <prop key="mail.host">myHost</prop>      
</util:properties>

For more Details have a look at Chapter Email of the Spring Reference.

Upvotes: 0

matt b
matt b

Reputation: 139921

As mentioned, you can use SimpleMailMessage and/or MailSender if you like; the Spring classes are intended to expose a simpler interface over the traditional JavaMail API:

The Spring Framework provides a helpful utility library for sending email that shields the user from the specifics of the underlying mailing system and is responsible for low level resource handling on behalf of the client.

Is there a reason why you wouldn't want to use these classes if you are already using Spring? What is the actual problem you are having?

Upvotes: 4

Related Questions