我正在一個項目中,我需要從數據庫中檢索電子郵件地址,然後發送電子郵件給他們。我用arraylist檢索了這些電子郵件地址。像這樣:發送電子郵件至arraylist從數據庫檢索
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javaapplication1.Person;
public class ABC {
public static void main(String[] args) throws SQLException {
ArrayList<Person> personlist = new ArrayList<Person>();
//List<Person> personlist = new List<Person>();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","gsjsc","gsjschanna");
Statement st=con.createStatement();
ResultSet srs = st.executeQuery("SELECT * FROM person2");
while (srs.next()) {
Person person = new Person();
person.setName(srs.getString("name"));
person.setJobtitle(srs.getString("jobtitle"));
// person.setFrequentflyer(srs.getInt("frequentflyer"));
personlist.add(person);
}
System.out.println(personlist.size());
for (int a=0;a<personlist.size();a++)
{
System.out.println(personlist.get(a).getName());
System.out.println(personlist.get(a).getJobtitle());
// System.out.println(personlist.get(2).getName());
// System.out.println(personlist.get(3).getName());
}
//System.out.println(personlist.get(4));
//System.out.println(namelist.);
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}
Person.java:包含所有的setter和getters。
編輯
現在我要發送電子郵件到對象人的ArrayList中檢索這些電子郵件地址。 我有一個代碼,像這樣的數組列表中發送郵件給多個收件人:
package javaapplication1;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmailToGroupDemo {
public static void main(String[] args) {
// Create a SendEmail object and call start
// method to send a mail in Java.
SendEmailToGroupDemo sendEmailToGroup = new SendEmailToGroupDemo();
sendEmailToGroup.start();
}
private void start() {
// For establishment of email client with
// Google's gmail use below properties.
// For TLS Connection use below properties
// Create a Properties object
Properties props = new Properties();
// these properties are required
// providing smtp auth property to true
props.put("mail.smtp.auth", "true");
// providing tls enability
props.put("mail.smtp.starttls.enable", "true");
// providing the smtp host i.e gmail.com
props.put("mail.smtp.host", "smtp.gmail.com");
// providing smtp port as 587
props.put("mail.smtp.port", "587");
// For SSL Connection use below properties
/*props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");*/
// Create Scanner object to take necessary
// values from the user.
Scanner scanner = new Scanner(System.in);
System.out.println("Please provide your Username for Authentication ...");
final String Username = scanner.next();
System.out.println("Please provide your Password for Authentication ...");
final String Password = scanner.next();
System.out.println("Please provide Email Address from which you want to send Email ...");
final String fromEmailAddress = scanner.next();
System.out.println("Please provide Email Addresses to which you want to send Email ...");
System.out.println("If you are done type : Done or done");
// ArrayLists to store email addresses entered by user
ArrayList< String> emails = (ArrayList<String>) getEmails();
System.out.println("Please provide Subject for your Email ... ");
final String subject = scanner.next();
System.out.println("Please provide Text Message for your Email ... ");
final String textMessage = scanner.next();
// Create a Session object based on the properties and
// Authenticator object
Session session = Session.getDefaultInstance(props,
new LoginAuthenticator(Username,Password));
try {
// Create a Message object using the session created above
Message message = new MimeMessage(session);
// setting email address to Message from where message is being sent
message.setFrom(new InternetAddress(fromEmailAddress));
// setting the email addressess to which user wants to send message
message.setRecipients(Message.RecipientType.BCC, getEmailsList(emails));
// setting the subject for the email
message.setSubject(subject);
// setting the text message which user wants to send to recipients
message.setText(textMessage);
// Using the Transport class send() method to send message
Transport.send(message);
System.out.println("\nYour Message delivered successfully ....");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
// This method takes a list of email addresses and
// returns back an array of Address by looping the
// list one by one and storing it into Address[]
private Address[] getEmailsList(ArrayList<String> emails) {
Address[] emaiAddresses = new Address[emails.size()];
for (int i =0;i < emails.size();i++) {
try {
emaiAddresses[i] = new InternetAddress(emails.get(i));
}
catch (AddressException e) {
e.printStackTrace();
}
}
return emaiAddresses;
}
// This method prompts user for email group to which he
// wants to send message
public List<String> getEmails() {
ArrayList<String> emails = new ArrayList<String>();
int counter = 1;
String address = "";
Scanner scanner = new Scanner(System.in);
// looping inifinitely times as long as user enters
// emails one by one
// the while loop breaks when user types done and
// press enter.
while(true) {
System.out.println("Enter E-Mail : " + counter);
address = scanner.next();
if(address.equalsIgnoreCase("Done")){
break;
}
else {
emails.add(address);
counter++;
}
}
return emails;
}
}
// Creating a class for Username and Password authentication
// provided by the user.
class LoginAuthenticator extends Authenticator {
PasswordAuthentication authentication = null;
public LoginAuthenticator(String username, String password) {
authentication = new PasswordAuthentication(username,password);
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}
}
這是一個完全不同的代碼。但是可以發送電子郵件給一羣人。但問題是我們必須手動輸入電子郵件地址。而我想將郵件發送到從ABC類檢索的地址。有人可以給我一個集成的代碼(對於這兩個類),這將是偉大的。
*注意:請用完整的工作代碼回答,截止日期接近。* - 大聲笑,祝你好運 – Idos
你的問題和你的代碼不匹配。在Person對象中設置的電子郵件在哪裏?您的其他代碼從控制檯獲取電子郵件列表,並且似乎沒有與人員列表獲取代碼的任何連接。 –
'請用完整的工作代碼回答,截止日期很近 - 我通常會編輯這類垃圾,但在您的情況下,我會將其保留,所以每個人都可以看到它。它的裸露的臉頰... [閱讀此](http://meta.stackoverflow.com/q/326569/472495)。 – halfer