جاوا میل

کد و توضیحات

JavaMail

Code and Explanation 3

Spring framework provides many useful interfaces and classes for sending and receiving mails.

Spring Java Mail API

1.MailSender interface:  It is the root interface. It provides basic functionality for sending simple mails.
2. JavaMailSender interface: It is the subinterface of MailSender. It supports MIME messages. 
It is mostly used with MimeMessageHelper class for the creation of JavaMail MimeMessage,with attachment etc. The spring framework 
recommends MimeMessagePreparator mechanism for using this interface.
3. JavaMailSenderImpl class: It provides the implementation of JavaMailSender interface. 
It supports JavaMail MimeMessages and Spring SimpleMailMessages.
4. SimpleMailMessage class: It is used to create a simple mail message including from, to, cc, subject and text messages.
5. MimeMessagePreparator interface: It is the callback interface for the preparation of JavaMail MIME messages.
6. MimeMessageHelper class: It is the helper class for creating a MIME message. 
It offers support for inline elements such as images, typical mail attachments and HTML text content.


Spring API


Example of sending email in Java

Steps to send email using Spring Java Mail API
1. Get the session object
2. Compose the message
3. Send the message

 Two spring mail classes are used:

1.  SimpleMailMessagefor creating message.
2. JavaMailSenderImpl for sending message

1. Get the session object

It stores all the information of host like host name, username, password etc. in the xml file.

property name = javaMailProperties         
prop key=mail.smtp.host   localhost 
prop key=mail.smtp.port   25 

In that  we are creating a bean for JavaMailSenderImpl class.
Also creating the bean for MailBox class with mailSender property. 
The instance of JavaMailSenderImpl class will be set in the mailSender property of MailBox class.

bean id= mailSender  class= org.springframework.mail.javamail.JavaMailSenderImpl   
bean id= mailBox class= com.MailBox  
property name= mailSender ref= mailSender 
    
    

2) Compose the message An object of MailSender will be provided to this property at runtime. In the sendMail() method, we are creating the instance of SimpleMailMessage and storing the information into this object such as from, to, subject and message. The send() method of MailSender interface is used here to send the simple mail. mport org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage; public class MailBox{ 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); } } 3) Send the message //sending the message The class gets the bean of mailBox from the .xml file and calls the sendMail method of MailBox class. Resource r =new ClassPathResource("applicationContext.xml"); BeanFactory b=new XmlBeanFactory(r); MailBox m=(MailBox)b.getBean("mailBox"); String sender="sender@gmail.com";// The sender id String receiver="receivere@gmail.com";// The receiver id m.sendMail(sender,receiver,"hi","welcome");