在Java中形成邮件正文

时间:2018-08-27 16:14:05

标签: java javamail

当前正在一个项目中,其中将在Java Mail API主体中设置我的DataRetrival类。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
//import com.mysql.jdbc.Statement;
public class DataRetrival {

    public List<Employee> javaData() {
        DatabaseConnect dc = new DatabaseConnect();
        Connection con;

        List<Employee> employees = new ArrayList<>();
        try {
            con = dc.connect();
            String query ="SELECT * FROM employee";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);

               while (rs.next())
                  {

                  Employee emp = new Employee();
                   emp.setID(rs.getInt("ID"));
                   emp.setEmployee_Number(rs.getString("Employee_Number"));
                   emp.setFirstName(rs.getString("FirstName"));
                   emp.setLastName(rs.getString("LastName"));
                   emp.setEmailAddress(rs.getString("EmailAddress"));
                   emp.setPdfName(rs.getString("PdfName"));
                   emp.setEmailAddress(rs.getString("Sup_EmailAddress")); 
                   employees.add(emp);


                   String employeeNumber = rs.getString("Employee_Number");
                   System.out.println(employeeNumber);

                  }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return employees;
    }

创建了这样的新实例。

DataRetrival dtr = new DataRetrival();

从这个新实例或任何其他方法中,我想填充 InterenetAddress.parse 字段,其中包含名为 JavaMail 的另一个类,其中上面的字段代码段中提到的EmailAddress

message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(Want get called Email_Adress));

1 个答案:

答案 0 :(得分:1)

来自comment

  

我想调用javaData()并将getEmailAddress获取到另一个类

呼叫javaData()

DataRetrival dtr = new DataRetrival();
List<Employee> employees = dtr.javaData();

要获取getEmailAddress

for (Employee employee : employees) {
    String emailAddress = employee.getEmailAddress();
    // use value here
}

以上两种构造都是Java的核心功能,因此,建议您重新阅读how to do method callshow to iterate a list上的Java指南。