java - 通过从数据库中获取邮件结构在 JAVA 中发送邮件
问题描述
我必须在 ADF 中使用 Java 发送邮件,但邮件结构来自数据库,但我不知道如何使用该结构并为其分配动态值。以下是结构:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<head>
<title>Email Notification</title>
<style type="text/css"> body.font-type { font-family: Arial, Helvetica, sans-serif; } div.main-border { border: 1px solid black; width: 100%; } table { width: 100%; border: 1px solid black; } th.main-header { font-size: 18px; font-weight: bold; background-color: rgb(57, 73, 94); text-align: center; color: white; border: 1px solid black; } th.sub-header { font-size: 14px; font-weight: bold; background-color: rgb(189, 215, 238); text-align: center; border: 1px solid black; } th.sub-header-h { font-size: 14px; font-weight: bold; background-color: rgb(189, 215, 238); text-align: right; border: 1px solid black; width: 29%; } table, th, td { border-collapse: collapse; } td { border: 1px solid black; text-align: center; } td.content { text-align: center; }button{background-image:none;border-bottom-left-radius:3px;border-bottom-right-radius:3px;border-bottom-style:solid;border-bottom-width:1px;border-image-outset:0;border-image-repeat:stretch;border-image-slice:100%;border-image-source:none;border-image-width:1;border-left-style:solid;border-left-width:1px;border-right-style:solid;border-right-width:1px;border-top-left-radius:3px;border-top-right-radius:3px;border-top-style:solid;border-top-width:1px;box-sizing:border-box;cursor:pointer;display:block;float:left;font-family:"Helvetica Neue",Roboto,Arial,"Droid Sans",sans-serif;font-feature-settings:normal;font-kerning:auto;font-language-override:normal;font-optical-sizing:auto;font-size:14px;font-size-adjust:none;font-stretch:100%;font-style:normal;font-variant:normal;font-variant-alternates:normal;font-variant-caps:normal;font-variant-east-asian:normal;font-variant-ligatures:normal;font-variant-numeric:normal;font-variant-position:normal;font-variation-settings:normal;font-weight:400;line-height:20px;min-height:1px;overflow:visible;overflow-x:visible;overflow-y:visible;padding-bottom:6px;padding-left:10px;padding-right:10px;padding-top:6px;text-align:center;text-decoration:none;text-decoration-color:#fff;text-decoration-line:none;text-decoration-style:solid;text-transform:none;white-space:nowrap;width:175.5px;-moz-appearance:button;-moz-user-select:none}button.approve{background-color:#2e6da4;border-bottom-color:#204d74;border-left-color:#204d74;border-right-color:#204d74;border-top-color:#204d74;color:#fff}button.reject{background-color:red;border-bottom-color:red;border-left-color:red;border-right-color:red;border-top-color:red;color:#fff}</style>
</head>
<table>
<tbody>
<tr>
<th class="main-header">LC Details</th>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<th class="sub-header-h">LC Number</th>
<td class="content"> </td>
</tr>
<tr>
<th class="sub-header-h">Facility Number</th>
<td class="content"> </td>
</tr>
<tr>
<th class="sub-header-h">LC Amount Created</th>
<td class="content"> </td>
</tr>
<tr>
<th class="sub-header-h">Expiry Date</th>
<td class="content"> </td>
</tr>
<tr>
<th class="sub-header-h">Project number</th>
<td class="content"> </td>
</tr>
<tr>
<th class="sub-header-h">Cost Center</th>
<td class="content"> </td>
</tr>
<tr>
<th class="sub-header-h">Bank Account</th>
<td class="content"> </td>
</tr>
</tbody>
</table>
</html>
</xsl:template>
</xsl:stylesheet>
以下是我发送邮件的班级
import java.time.LocalDate;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class Mail {
public static void main(String[] args) {
String host = "smtp.gmail.com";
final String user = "test.";// change accordingly
final String password = "Wel";// change accordingly
String to = "shwetangr@gmail.com";// change accordingly
StringBuilder msg = new StringBuilder();
// Get the session object
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
});
// Compose the message
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Dummy Email");
message.setContent(text, "text/html");
// send the message
Transport.send(message);
System.out.println("message sent successfully...");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
请告诉我如何通过使用 JAVA 从数据库中获取该结构来在运行时为那些空文本框设置值
解决方案
getDataFromDataBase() {
logger.info("---Début: récupération de données depuis la BD ---");
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
WriteTxtFile.setExitCode(1);
logger.error("Oracle Driver Class Not found Exception: ",e.toString());
return null;
}
DriverManager.setLoginTimeout(60);
try {
String url = url of your database;
conn = DriverManager.getConnection(url, user,
password);
} catch (SQLException e) {
return null;
}
// Creates a Statement object for sending SQL statements to the database
try {
statement = conn.createStatement();
String sql = your query;
rs = statement.executeQuery(sql);
String content="";
while (rs.next()) {
content=content+rs.getString("your column name"),
content=content+rs.getString("your column name2"),
}
statement.close();
} catch (SQLException e) {
logger.error(e.toString());
}
logger.info("---Fin: récupération de données depuis la BD ---");
return lines;
}
推荐阅读
- python - 如何在内部类中调用外部类参数?
- jquery - 如何覆盖整个页面的正文 CSS?
- javascript - 展平数组,但不包括根元素
- linux - 从 /dev/pts/x 读取时的竞争条件
- java - E/RecyclerView:没有附加适配器;跳过布局(需要帮助)
- react-native - 反应本机 IAP (Android) 验证接收问题
- typescript - 在 TypeScript 中实现 mixins 而不使用任何类型
- c++ - C++ 初学者得到 42
- python - 在方阵中找到线性独立列向量的适当方法是什么?
- node.js - 退出当前线程并移动到下一个