fail_msgToAddress = xxxx@yyyy.zzz
fail_msgSubject = [FAIL TO PROCESS EXAM] Study: %s for Patient %s could not be processed due to invalid examination codes
fail_msgText = <p>Case: %s, for patient: %s, failed to be processed since it seemed not to correspond to a valid examination type!</p><p> Please communicate with Npap lab for specific instructions</p>
在我的代码中,我想传递两个参数:学习ID和患者姓名,以电子邮件主题和消息。所以我设置了一些从我的属性文件中获取此信息的方法,并且我尝试使用String.format方法将一些参数值插入到文本中:
....
GlobalConfig cfg = new GlobalConfig();
String emailAddr = cfg.failMsgToAddress();
String emailSubj = cfg.failMsgSubject();
String emailTxt = cfg.failMsgText();
...
try {
Notifier ntf = new Notifier();
emailSubj = String.format("[FAIL TO PROCESS EXAM] Study: %s for Patient %s could not be processed due to invalid examination codes", d, dcSerMeta.getAnonymizationData(d).getPatientName());
emailTxt = String.format(emailTxt, d, dcSerMeta.getAnonymizationData(d).getPatientName());
ntf.sendMail(emailAddr, emailSubj, emailTxt);
....
}
其中Notifier是我的邮件包装类。它的sendMail方法是发送html电子邮件的经典实现。
我使用的是常见的javax.mail库,而Notifier.sendMail方法是:
public boolean sendMail(String msgToAddress, String msgSubject, String msgText) throws GeneralSecurityException {
boolean result = false;
GlobalConfig cfg = new GlobalConfig();
smtpServer = cfg.getSmtpServer();
smtpPort = Integer.parseInt(cfg.getSmtpPort());
smtpUser = cfg.getSmtpUsername();
smtpPassword = cfg.getSmtpPassword();
emailSender = cfg.getSmtpSender();
smtpProtocol = cfg.getSmtpProtocol();
smtpSecure = ("0".equals(cfg.getSmtpSecure())) ? "false" : "true";
log.info("###SMTP server: " + smtpServer + ", SMTP port: " + smtpPort + ", SMTP username: " + smtpUser + ", SMTP password: " + smtpPassword + ", email Sender: " + emailSender + ", smtpSecure: " + smtpSecure + ", smtpProtocol: " + smtpProtocol);
Properties props = new Properties();
MailSSLSocketFactory socketFactory = new MailSSLSocketFactory(); socketFactory.setTrustedHosts(new String[] { smtpServer });
socketFactory.setTrustAllHosts(true);
if("true".equals(smtpSecure)) {
props.put("mail.transport.protocol", smtpProtocol);
props.put("mail.smtps.host", smtpServer);
props.put("mail.smtps.port", smtpPort);
props.put("mail.smtps.ssl.enable", "false");
props.put("mail.smtps.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtps.socketFactory", socketFactory);
} else {
props.put("mail.transport.protocol", smtpProtocol);
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.port", smtpPort);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.enable", "false");
props.put("mail.smtps.socketFactory", socketFactory);
}
Authenticator auth = new SMTPAuthenticator();
javax.mail.Session mailSession = javax.mail.Session.getInstance(props, auth);
mailSession.setDebug(true);
Transport transport;
try {
transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
try {
message.setSubject(msgSubject);
//message.setContent(msgText, "text/plain");
message.setContent(msgText, "text/html" );
message.setFrom(new InternetAddress(emailSender));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(msgToAddress));
transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
result = true;
} catch (MessagingException e) {
e.printStackTrace();
}
} catch (NoSuchProviderException e1) {
e1.printStackTrace();
}
return result;
}
然而,String.format方法似乎不起作用,当我收到电子邮件时,我只是得到了&#39;%s&#39;而不是实际的参数值!我错过了什么?