spring 发送邮件

Spring mail 发送邮件
一:添加mail jar 包
二:在WEB-INF 下,建立文件mail.properties,填写下面的东西
mail.host=服务器
mail.username=用户名
mail.password=密码
mail.smtp.auth=true
三:xml 文件中
<!-- 发送邮件 -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">

<!-- 有些邮件服务器发送邮件需要认证,所以必须提供帐号 -->
<property name="host" value="${mail.host}"></property>
<property name="username" value="${mail.username}" />
<property name="password" value="${mail.password}" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
</props>
</property>
</bean>
<!--一个邮件模板 -->
<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
<!-- <property name="from" value="${mail.from}" /> <property name="to"
value="${mail.to}" /> <property name="subject" value="业务还款提醒" /> -->
</bean>

四:后台发送邮件

private MailSender mailSender;
private SimpleMailMessage mailMessage;// 邮件模板

public MailSender getMailSender() {
return mailSender;
}

public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
public SimpleMailMessage getMailMessage() {
return mailMessage;
}

public void setMailMessage(SimpleMailMessage mailMessage) {
this.mailMessage = mailMessage;
}

SimpleMailMessage msg = new SimpleMailMessage(this.mailMessage);
msg.setSubject("邮件标题");
msg.setFrom("发送人邮件地址");
msg.setTo("收件人");
msg.setText("内容");
mailSender.send(msg);

你可能感兴趣的:(spring,发送邮件)