public class glowna extends javax.swing.JFrame {
public static String url;
public static int index;
public static String data;
public static int konczace_przeglady;
public static String tablica1[]= new String[30];
public static boolean sendMail(String from, String password, String to[]){
String host = "smtp.gmail.com";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from );
props.put("mail.smtp.password", password);
props.put("mail.smtp.port", 587);
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, null);
MimeMessage mimeMessage = new MimeMessage(session);
try{
mimeMessage.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
for(int i=0;i<to.length;i++){
toAddress[i] = new InternetAddress(to[i]); }
for(int i=0;i<toAddress.length;i++){
mimeMessage.addRecipient(Message.RecipientType.TO, toAddress[i]); }
mimeMessage.setSubject("Temat : Kończy się ubezpieczenie samochodów :");
try {
Connection con = DriverManager.getConnection(url);
String sql = "SELECT * FROM dbo.UZYTKOWNIK INNER JOIN dbo.POJAZD ON dbo.POJAZD.ID_OSOBA = dbo.UZYTKOWNIK.ID_OSOBA where UBEZPIECZENIE_DO >= CAST(CURRENT_TIMESTAMP AS DATE) and UBEZPIECZENIE_DO <= DATEADD(MONTH, 2, CAST(CURRENT_TIMESTAMP AS DATE)) ORDER BY UBEZPIECZENIE_DO";
PreparedStatement pst = con.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
int i=0;
while (rs.next()){
String ID = rs.getString("ID_SAMOCHOD");
String MARKA = rs.getString("MARKA");
String REJESTRACJA = rs.getString("NR_REJESTRACYJNY");
String NAZWISKO = rs.getString("NAZWISKO");
String DATA = rs.getString("DATA_WPISU");
String OD = rs.getString("UBEZPIECZENIE_OD");
String DO = rs.getString("UBEZPIECZENIE_DO");
Object[] row = {ID, MARKA, REJESTRACJA, NAZWISKO, DATA, OD, DO};
tablica1[i] = Arrays.toString(row); // zapis do tablicy wyników o kończącym sie przeglądzie
i=i+1; }
} catch (SQLException ex) {
Logger.getLogger(glowna.class.getName()).log(Level.SEVERE, null, ex); }
Multipart multipart = new MimeMultipart(); // create the Multipart and add its parts to it
mimeMessage.setContent( multipart );
for (int i = 0; i < tablica1.length; i++) {
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent(tablica1[i].toString());
multipart.addBodyPart(textPart); }
mimeMessage.setText(multipart.toString());
mimeMessage.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host,from,password);
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
transport.close();
} catch (MessagingException ex) {
Logger.getLogger(glowna.class.getName()).log(Level.SEVERE, null, ex);
}
return (false); }
/**
* Creates new form glowna
* @throws java.sql.SQLException
*/
public glowna() throws SQLException {
try {
initComponents();
groupButton( );
LocalDate localtime = LocalDate.now();
txt_data.setText(localtime.toString());
txt_data1.setText(localtime.toString());
txt_data2.setText(localtime.toString());
data = txt_data.toString();
if(hidden == 1){ //true
btn_nowy.setVisible(true);
btn_popraw.setVisible(true);
btn_user.setVisible(true);
btn_usuwanie.setVisible(true);
btn_email_wysylka.setVisible(true);
btn_administracja.setVisible(true);
btn_nowy1.setVisible(true);
btn_popraw1.setVisible(true);
btn_user_przeglad.setVisible(true);
btn_usuwanie1.setVisible(true);
btn_cofnij_z_archiwum.setVisible(true);
} else
{ //false
btn_nowy.setVisible(false);
btn_popraw.setVisible(false);
btn_user.setVisible(false);
btn_usuwanie.setVisible(false);
btn_email_wysylka.setVisible(false);
btn_administracja.setVisible(false);
btn_nowy1.setVisible(false);
btn_popraw1.setVisible(false);
btn_user_przeglad.setVisible(false);
btn_usuwanie1.setVisible(false);
btn_cofnij_z_archiwum.setVisible(false);
}
lista_mail(); // odwołanie się do funkcji wysyłki MAILI
WypiszTabelePojazdow();
PojazdyArchiwalne();
} catch (ClassNotFoundException ex) {
Logger.getLogger(glowna.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
* @throws java.lang.ClassNotFoundException
* @throws java.sql.SQLException
*/
private void groupButton( ) {
ButtonGroup bg1 = new ButtonGroup( );
bg1.add(btn_sortowanie_ubezpieczenie_OD);
bg1.add(btn_sortowanie_ubezpieczenie_do);
bg1.add(btn_sortowanie_data_wpisu);
bg1.add(btn_sortowanie_przeglad_do);
bg1.add(btn_sortowanie_przeglad_OD);
bg1.add(btn_sortowanie_data_wpisu_przeglady);}
public void polaczenie() throws ClassNotFoundException, SQLException{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
url ="xxxxxxxxxxxxxxxxxxxxxxxxxxx";
}
public final void lista_mail(){
try {
polaczenie();
Connection con = DriverManager.getConnection(url);
String sql = "SELECT MAIL FROM dbo.MAIL";
PreparedStatement pst = con.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
while (rs.next()){
String data0 = rs.getString("MAIL");
String[] tablica = {data0};
if(glowna.sendMail
("[email protected]",
"xxxxxxxxxxxx",
//"Treść wiadomości : Ubezpieczenie",
tablica)) txt_mail.setText("Wiadomość wysłano");
else
txt_mail.setText("Wiadomości nie wysłano");
}
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(glowna.class.getName()).log(Level.SEVERE, null, ex);
}
}
0
0
Błąd jest taki że z zapytania SQL dane nie wpisują się w treść maila.
0
Problem leży w odczycie danych z tablicy i wrzucenie ich w maila. Nie bardzo wiem jak to ogarnąć. Byłbym wdzięczny za pomoc.
Błędu nie ma ponieważ dane w maila nie przechodzą. Zawiesza się na instrukcji z for.
Multipart multipart = new MimeMultipart();
mimeMessage.setContent( multipart );
for (int i = 0; i < tablica1.length; i++) {
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent(tablica1[i].toString());
multipart.addBodyPart(textPart); }
mimeMessage.setText(multipart.toString());
mimeMessage.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host,from,password);
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
transport.close();