MailSender接口: 它是根接口。它提供了发送简单邮件的基本功能。
JavaMailSender接口: 它是MailSender的子接口。它支持MIME消息。它主要与 MimeMessageHelper 类一起使用,以创建JavaMail MimeMessage ,以及附件等。spring框架推荐使用 MimeMessagePreparator 机制来使用此接口。
JavaMailSenderImpl类: 它提供JavaMailSender接口的实现。它支持JavaMail MimeMessages和Spring SimpleMailMessages。
SimpleMailMessage类: 用于创建简单的邮件,包括从,到,抄送,主题和文本邮件。
MimeMessagePreparator接口: 它是用于准备JavaMail MIME消息的回调接口。
MimeMessageHelper类: 它是用于创建MIME消息的帮助器类。它支持内联元素,例如图像,典型的邮件附件和HTML文本内容。
SimpleMailMessage 用于创建消息。
JavaMailSenderImpl 用于发送消息。
MailMail.java
applicationContext.xml
Test.java 您需要加载mail.jar和activation.jar文件才能运行此示例。
package com.lidihuo; import org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage; 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); } }
主机
用户名
密码
javaMailProperties
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.gmail.com" /> <property name="username" value="yourgmailid@gmail.com" /> <property name="password" value="yourgmailpassword" /> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> <prop key="mail.smtp.socketFactory.port">465</prop> <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop> <prop key="mail.smtp.port">465</prop> </props> </property> </bean> <bean id="mailMail" class="com.lidihuo.MailMail"> <property name="mailSender" ref="mailSender" /> </bean> </beans>
package com.lidihuo; import org.springframework.beans.factory.*; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.*; public class Test { public static void main(String[] args) { Resource r=new ClassPathResource("applicationContext.xml"); BeanFactory b=new XmlBeanFactory(r); MailMail m=(MailMail)b.getBean("mailMail"); String sender="sendergmailid@gmail.com";//write here sender gmail id String receiver="receiveremailid@gmail.com";//write here receiver id m.sendMail(sender,receiver,"hi","welcome"); System.out.println("success"); } }
为core和Java邮件加载spring jar文件
加载mail.jar和activation.jar
在applicationContext.xml文件中更改用户名和密码属性,并指定您的gmail ID和密码。
在Test.java文件中更改发件人gmail ID和Receivermail ID。
编译并运行Test类
mail.unitedsquaad.com作为主机名。更改它。
a@unitedsquaad.com作为用户名。更改它。
xxxxx作为密码。更改它。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="mail.unitedsquaad.com" /> <property name="username" value="a@unitedsquaad.com" /> <property name="password" value="xxxxx" /> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> </props> </property> </bean> <bean id="mailMail" class="MailMail"> <property name="mailSender" ref="mailSender" /> </bean> </beans>
.... public void sendMail(String from, String[] to, String subject, String msg) { //creating message SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); message.setTo(to);//passing array of recipients message.setSubject(subject); message.setText(msg); //sending message mailSender.send(message); } ...
import javax.mail.Message; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessagePreparator; public class MailMail{ private JavaMailSender mailSender; public void setMailSender(JavaMailSender mailSender) { this.mailSender = mailSender; } public void sendMail(final String from, final String to,final String subject,final String msg) { MimeMessagePreparator messagePreparator = new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws Exception { mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress(to)); mimeMessage.setFrom(new InternetAddress(from)); mimeMessage.setSubject(subject); mimeMessage.setText(msg); } }; mailSender.send(messagePreparator); } }
import java.io.File; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; public class MailMail{ private JavaMailSender mailSender; public void setMailSender(JavaMailSender mailSender) { this.mailSender = mailSender; } public void sendMail(final String from, final String to,final String subject,final String msg) { try{ MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(msg); // attach the file FileSystemResource file = new FileSystemResource(new File("c:/rr.jpg")); helper.addAttachment("mybrothermage.jpg", file);//image will be sent by this name mailSender.send(message); }catch(MessagingException e){e.printStackTrace();} } }