0

この問題が古いことはわかっていますが、xml ではなくクラスにプロパティが追加された SimpleMailMessage の解決策は見つかりませんでした。アプリの起動後に例外はありません。単にメールを送信していません。メソッド CustomMailDeliveryImpl.sendMail() は Controllerller からトリガーされます。間違いが見つかりません。

@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);
        }
    }
}

助けていただければ幸いです!!!!

4

1 に答える 1

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) {
        }
    }
}

}

それは完璧に機能しています!!

于 2013-08-14T14:04:32.110 に答える