Java使用Spring发邮件,javaspring发邮件,import org.s
分享于 点击 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
用户点评