我试图通过java代码发送html表
public static void main(String args[]){
try{
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.gmail.com");
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("HTML mail with images");
message.setFrom(new InternetAddress("abc@gmail.com"));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("abc@gmail.com"));
String html = "<html><head><title>" +
message.getSubject() +
"</title></head><body><h1>" +
message.getSubject() +
"</h1><p>This is a test of sending an HTML e-mail" +
" through Java.</p>";
// HTMLDataSource is an inner class
message.setDataHandler(new DataHandler(new HTMLDataSource(html)));
transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
static class HTMLDataSource implements DataSource {
private String html;
public HTMLDataSource(String htmlString) {
html = htmlString;
}
// Return html string in an InputStream.
// A new stream must be returned each time.
public InputStream getInputStream() throws IOException {
if (html == null) throw new IOException("Null HTML");
return new ByteArrayInputStream(html.getBytes());
}
public OutputStream getOutputStream() throws IOException {
throw new IOException("This DataHandler cannot write HTML");
}
public String getContentType() {
return "text/html";
}
public String getName() {
return "JAF text/html dataSource to send e-mail only";
}
}
}
我已经将表数据存储在单独的类中的 ArrayList 中。下面是它的片段。
List objList = new ArrayList();
try
{
conn=dao.DBConnection();
sql1.append(" select MENU_ID, MENU_CODE, MENU_NAME, DESCRIPTON, ACTION_URL, CATEGORY, SORT_ORDER from m_menus order by MENU_ID ");
iPreparedStatement1=conn.prepareStatement(sql1.toString());
iResultSet1=iPreparedStatement1.executeQuery();
while(iResultSet1.next())
{
afUserAdminMenuMaster objafuseradminmenumaster = new afUserAdminMenuMaster();
Integer i=0;
objafuseradminmenumaster.setMenuId(iResultSet1.getString(++i));
objafuseradminmenumaster.setMenuCode(iResultSet1.getString(++i));
objafuseradminmenumaster.setMenuName(iResultSet1.getString(++i));
objafuseradminmenumaster.setMenuDesc(iResultSet1.getString(++i));
objafuseradminmenumaster.setAction_URL(iResultSet1.getString(++i));
objafuseradminmenumaster.setCategory(iResultSet1.getString(++i));
objafuseradminmenumaster.setSortOrder(iResultSet1.getString(++i));
objList.add(objafuseradminmenumaster);
}
我想将该 sql 表嵌入到邮件正文中,这将取决于条件是动态的。请指导我应该如何进行。