我知道这个问题已经过时了,但我找不到SimpleMailMessage的任何解决方案,在类中添加了属性,而不是在xml中。我的应用程序启动后没有执行,它根本就不发送邮件。 CustomMailDeliveryImpl.sendMail()方法是从Controlller中获得的。我找不到错误。
@Component("CustomMailDelivery")
public class CustomMailDeliveryImpl implements CustomMailDelivery {
protected static Logger logger = Logger.getLogger(CustomMailDeliveryImpl.class);
private EmployeeService employeeService;
private String username = "* * *@gmail.com";
private String password = "* * *";
private JavaMailSenderImpl sender = new JavaMailSenderImpl();
@Autowired
public void setSender(JavaMailSenderImpl sender) {
this.sender = sender;
}
@Autowired
public void setEmployeeService(EmployeeService employeeService) {
this.employeeService = employeeService;
}
/**
* @param from - must contain address of sender,
* @param to - must contain address of target,
* @param subject - must be included with short description,
* @param body - main message of mail.
* @throws RuntimeException if any param is empty.
*/
public void sendMail(String from, String to, String subject, String body) {
// selecting settings from DB
SystemSettingsModel settingsModel = employeeService.getSettingsByName("default");
// defining settings
sender.setHost(settingsModel.getSmtpServerAddress());
sender.setPort(settingsModel.getSmtpServerPort());
sender.setUsername(username);
sender.setPassword(password);
// defining SMTP properties
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.debug", "1");
Session session = Session.getDefaultInstance(properties, null);
sender.setSession(session);
// initiating mail
SimpleMailMessage simpleMessage = new SimpleMailMessage();
try {
if (from != null && to != null) {
if (subject != null && body != null) {
simpleMessage.setFrom(from);
simpleMessage.setTo(to);
simpleMessage.setSubject(subject);
simpleMessage.setText(body);
sender.send(simpleMessage);
logger.debug("Mail has been sent successfully");
} else {
throw new MessagingException("Subject and message of mail is empty!");
}
} else {
throw new MessagingException("Address of object and target is empty!");
}
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
任何帮助表示赞赏!!!!
答案 0 :(得分:0)
我终于设法克服了这个问题,我确实找到了解决方案!用这种方式?您可以从页面配置或DAO服务传递属性!
@Component("CustomMailDeliveryImpl")
public class CustomMailDeliveryImpl implements CustomMailDelivery {
private EmployeeService employeeService;
private Transport transport;
private String username = "***@gmail.com";
private String password = "***";
private SystemSettingsModel settingsModel;
private Session session;
private MimeMessage message;
private InternetAddress fromAddress, toAddress;
private Properties properties;
@Autowired
public void setEmployeeService(EmployeeService employeeService) {
this.employeeService = employeeService;
}
public void sendMail(String from, String to, String subject, String body) throws SendFailedException {
settingsModel = employeeService.getSettingsByName("default");
// defining properties
properties = new Properties();
properties.put("mail.smtp.host", settingsModel.getSmtpServerAddress());
properties.put("mail.smtp.port", settingsModel.getSmtpServerPort());
properties.put("mail.smtp.user", username);
properties.put("mail.smtp.password", password);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
// InternetAddress initialisation
try {
fromAddress = new InternetAddress(from);
toAddress = new InternetAddress(to);
// new Authenticator session
session = (javax.mail.Session) Session.getDefaultInstance(properties, null);
} catch (AddressException e) {
throw new RuntimeException();
}
// executing mail sending
try {
if (from != null && to != null) {
if (subject != null && body != null) {
// defining message
message = new MimeMessage(session);
message.setFrom(fromAddress);
message.addRecipient(Message.RecipientType.TO, toAddress);
message.setSubject(subject);
message.setText(body);
message.setSentDate(new Date());
message.setHeader("MIME-Version" , "1.0" );
message.setHeader("Content-Type" , "text/html" );
// Transport initialisation
if (transport == null) {
transport = session.getTransport("smtp");
if (!transport.isConnected())
transport.connect(settingsModel.getSmtpServerAddress(),
settingsModel.getSmtpServerPort(), username, password);
}
transport.sendMessage(message, message.getAllRecipients());
} else {
throw new MessagingException("Subject and message of mail is empty!");
}
} else {
throw new MessagingException("Address and target of mail is empty!");
}
} catch (MessagingException e) {
e.printStackTrace();
throw new RuntimeException();
} finally {
if (transport != null) try {
transport.close();
} catch (MessagingException ignore) {
}
}
}
}
它的工作非常完美!!