如何在java中发送电子邮件中的多个数据

时间:2013-10-08 05:21:41

标签: java javamail

我有一个要求,一天结束我想从不同的表中收集所有员工的登录和注销详细信息,我想发邮件并发送给管理员,我的邮件代码工作正常,但我不知道怎么把所有的细节都放在邮件里面,任何人都可以帮我解决....... 这是我的要求,但这里我为每个人发送单独的邮件,我想把它放在一个邮件中

sql="select * from employee_reg where status=?";
        ps1=con.prepareStatement(sql);
        ps1.setString(1,status);
        ps1.execute();
        rs1=ps1.getResultSet();
        while(rs1.next()){
        String employeeid=rs1.getString("employeeid");  
        sql1="select * from employee_login where employeeid=? and date=?";
        ps2=con.prepareStatement(sql1);
        ps2.setString(1,employeeid);
        ps2.setDate(2,Sqldob);
        ps2.execute();
        rs2=ps2.getResultSet();
        if(rs2.next()){
            sql1="select * from employee_logout where employeeid1=? and date1=?";
            ps2=con.prepareStatement(sql1);
            ps2.setString(1,employeeid);
            ps2.setDate(2,Sqldob);
            ps2.execute();
            rs2=ps2.getResultSet();
            if(rs2.next()){
             continue;
            }
            else{
                String name=rs1.getString("name");
                String to="ranjithgowda.gr@gmail.com";
                 String from ="ranjeeth@theaalpine.com";
                 String pwd = "9611455509";
                 String subject= "Login Detailes of  "+name+" "+Sqldob;
                 String fileAttachment = "Logout is not been done"; 
                 System.out.println("inside process");
                TestJavaMail m = new TestJavaMail();
                String result=m.send(to, from, pwd, subject, fileAttachment);
                if (result.equals("success")){
                    System.out.println("success");
                }
                else{
                    System.out.println("file");
                    }
            }
        }
        else{
            String name=rs1.getString("name");
            String to="ranjithgowda.gr@gmail.com";
             String from ="ranjeeth@theaalpine.com";
             String pwd = "9611455509";
             String subject= "Login Detailes of  "+name+" "+Sqldob;
             String fileAttachment = "Login and Logout is not been done"; 
             System.out.println("inside process");
            TestJavaMail m = new TestJavaMail();
            String result=m.send(to, from, pwd, subject, fileAttachment);
            if (result.equals("success")){
                System.out.println("success");
            }
            else{
                System.out.println("file");
                }
            }
        }

嗨,这是我的javamail代码

package com.uttara.reg;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class TestJavaMail {
private String SMTP_PORT = "465";
private String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
private String SMTP_HOST_NAME = "smtp.gmail.com";
private Properties smtpProperties;
public TestJavaMail(){
initProperties();
}
private void initProperties(){
smtpProperties = new Properties();
smtpProperties.put("mail.smtp.host", SMTP_HOST_NAME);
smtpProperties.put("mail.smtp.auth", "true");
smtpProperties.put("mail.debug", "true");
smtpProperties.put("mail.smtp.port", SMTP_PORT);
smtpProperties.put("mail.smtp.socketFactory.port",SMTP_PORT);
smtpProperties.put ("mail.smtp.socketFactory.class",SSL_FACTORY);
smtpProperties.put("mail.smtp.socketFactory.fallback","false");
}
public  String send(String to, final String from,final String pwd, String subject,String body){
TestJavaMail tjm = new TestJavaMail();
try
{
    Properties props = tjm.getSmtpProperties() ;
// -- Attaching to default Session, or we could start a new one --
Session session = Session.getDefaultInstance(props,new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, pwd);
}
});
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to, false));
// -- Set the subject and body text --
msg.setSubject(subject);
msg.setText(body);
msg.setSentDate(new Date());
// -- Send the message –
Transport.send(msg);
System.out.println("Message sent OK.");
return "success";
}
catch (Exception ex)
{
return "error"+ex.getMessage();
}
}
public Properties getSmtpProperties() {
return smtpProperties;
}
public void setSmtpProperties(Properties smtpProperties) {
this.smtpProperties = smtpProperties;
}}

1 个答案:

答案 0 :(得分:0)

使用addRecipients方法将所有地址添加为单个邮件的收件人。