欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Java使用Spring发邮件,javaspring发邮件,import org.s

来源: javaer 分享于  点击 49553 次 点评:136

Java使用Spring发邮件,javaspring发邮件,import org.s


import org.springframework.beans.factory.annotation.Autowired;import org.springframework.mail.MailSender;import org.springframework.mail.SimpleMailMessage;import org.springframework.stereotype.Service;@Service("mailService")public class MailService {    @Autowired    private MailSender mailSender;    @Autowired    private SimpleMailMessage alertMailMessage;    public void sendMail(String from, String to, String subject, String body) {        SimpleMailMessage message = new SimpleMailMessage();        message.setFrom(from);        message.setTo(to);        message.setSubject(subject);        message.setText(body);        mailSender.send(message);    }    public void sendAlertMail(String alert) {        SimpleMailMessage mailMessage = new SimpleMailMessage(alertMailMessage);        mailMessage.setText(alert);        mailSender.send(mailMessage);    }}

用了spring就得配置xml,配置文件如下:

None

如果你要用Gmail发邮件,需要确认JavaMail属性正确配置:

host=smtp.gmail.comport=25 username=your-gmail-usernamepassword=your-gmail-passwordmail.transport.protocol=smtpmail.smtp.auth=true mail.smtp.starttls.enable=true```最后是测试代码:```javaNone
相关栏目:

用户点评