1 14 package org.compiere.process; 15 16 import java.sql.*; 17 import java.math.*; 18 19 import org.compiere.util.*; 20 import org.compiere.model.*; 21 22 28 public class SendMailText extends SvrProcess 29 { 30 31 private int m_R_MailText_ID = -1; 32 33 private MMailText m_MailText = null; 34 35 36 private int m_AD_User_ID = -1; 37 38 private MClient m_Client = null; 39 40 private MUser m_User = null; 41 42 private String m_from = null; 43 private String m_fromID = null; 44 private String m_fromPW = null; 45 46 47 48 private int m_R_InterestArea_ID = -1; 49 50 private int m_C_BP_Group_ID = -1; 51 52 54 55 58 protected void prepare() 59 { 60 ProcessInfoParameter[] para = getParameter(); 61 for (int i = 0; i < para.length; i++) 62 { 63 String name = para[i].getParameterName(); 64 if (para[i].getParameter() == null) 65 ; 66 else if (name.equals("R_InterestArea_ID")) 67 m_R_InterestArea_ID = ((BigDecimal)para[i].getParameter()).intValue(); 68 else if (name.equals("R_MailText_ID")) 69 m_R_MailText_ID = ((BigDecimal)para[i].getParameter()).intValue(); 70 else if (name.equals("C_BP_Group_ID")) 71 m_C_BP_Group_ID = ((BigDecimal)para[i].getParameter()).intValue(); 72 else if (name.equals("AD_User_ID")) 73 m_AD_User_ID = ((BigDecimal)para[i].getParameter()).intValue(); 74 else 75 log.error("prepare - Unknown Parameter: " + name); 76 } 77 } 79 84 protected String doIt() throws Exception 85 { 86 log.info("doIt - R_MailText_ID=" + m_R_MailText_ID); 87 m_MailText = new MMailText (getCtx(), m_R_MailText_ID); 89 if (m_MailText.getR_MailText_ID() == 0) 90 throw new Exception ("Not found @R_MailText_ID@=" + m_R_MailText_ID); 91 m_Client = MClient.get (getCtx()); 93 if (m_Client.getAD_Client_ID() == 0) 94 throw new Exception ("Not found @AD_Client_ID@"); 95 if (m_Client.getSMTPHost() == null || m_Client.getSMTPHost().length() == 0) 96 throw new Exception ("No SMTP Host found"); 97 if (m_AD_User_ID != -1) 99 { 100 m_User = new MUser (getCtx(), m_AD_User_ID); 101 if (m_User.getAD_User_ID() == 0) 102 throw new Exception ("No found @AD_User_ID@=" + m_AD_User_ID); 103 } 104 m_from = m_User == null ? m_Client.getRequestEMail() : m_User.getEmail(); 105 m_fromID = m_User == null ? m_Client.getRequestUser() : m_User.getEmailUser(); 106 m_fromPW = m_User == null ? m_Client.getRequestUserPW() : m_User.getEmailUserPW(); 107 if (m_from == null || m_from.length() == 0 || m_fromID == null || m_fromID.length() == 0 || m_fromPW == null || m_fromPW.length() == 0) 108 throw new Exception ("From EMail not complete - " + m_from + "(" + m_fromID + "/" + m_fromPW + ")"); 109 log.debug("doIt - send from " + m_from + "(" + m_fromID + "/" + m_fromPW + ")"); 110 111 long start = System.currentTimeMillis(); 112 log.info("doIt - Send to R_InterestArea_ID=" + m_R_InterestArea_ID); 114 String sql = "SELECT u.Name, u.EMail, u.AD_User_ID " 115 + "FROM R_ContactInterest ci" 116 + " INNER JOIN AD_User u ON (ci.AD_User_ID=u.AD_User_ID) " 117 + "WHERE ci.IsActive='Y' AND u.IsActive='Y'" 118 + " AND ci.OptOutDate IS NULL" 119 + " AND u.EMail IS NOT NULL" 120 + " AND ci.R_InterestArea_ID=?"; 121 PreparedStatement pstmt = null; 122 int counter = 0; 123 int errors = 0; 124 try 125 { 126 pstmt = DB.prepareStatement(sql); 127 pstmt.setInt(1, m_R_InterestArea_ID); 128 ResultSet rs = pstmt.executeQuery(); 129 while (rs.next()) 130 { 131 if (sendIndividualMail (rs.getString(1), rs.getString(2), rs.getInt(3))) 132 counter++; 133 else 134 errors++; 135 } 136 rs.close(); 137 pstmt.close(); 138 pstmt = null; 139 } 140 catch (SQLException ex) 141 { 142 log.error("doIt - InterestArea", ex); 143 } 144 log.info("doIt - Send to C_BP_Group_ID=" + m_C_BP_Group_ID); 146 sql = "SELECT u.Name, u.EMail, u.AD_User_ID " 147 + "FROM AD_User u" 148 + " INNER JOIN C_BPartner bp ON (u.C_BPartner_ID=bp.C_BPartner_ID) " 149 + "WHERE u.IsActive='Y' AND bp.IsActive='Y'" 150 + " AND u.EMail IS NOT NULL" 151 + " AND bp.C_BP_Group_ID=?"; 152 try 153 { 154 pstmt = DB.prepareStatement(sql); 155 pstmt.setInt(1, m_C_BP_Group_ID); 156 ResultSet rs = pstmt.executeQuery(); 157 while (rs.next()) 158 { 159 if (sendIndividualMail (rs.getString(1), rs.getString(2), rs.getInt(3))) 160 counter++; 161 else 162 errors++; 163 } 164 rs.close(); 165 pstmt.close(); 166 pstmt = null; 167 } 168 catch (SQLException ex) 169 { 170 log.error("doIt - BP_Group", ex); 171 } 172 try 174 { 175 if (pstmt != null) 176 pstmt.close(); 177 } 178 catch (SQLException ex1) 179 { 180 } 181 pstmt = null; 182 183 return "@Created@=" + counter + ", @Errors@=" + errors + " - " 184 + (System.currentTimeMillis()-start) + "ms"; 185 } 187 193 private boolean sendIndividualMail (String Name, String EMailAddress, int AD_User_ID) 194 { 195 String subject = m_MailText.getMailHeader(); 196 String message = m_MailText.getMailText(); 197 EMail mail = new EMail (m_Client.getSMTPHost(), m_from, EMailAddress, subject, message); 199 if (!mail.isValid()) 200 { 201 log.warn("sendIndividualMail NOT VALID - " + EMailAddress); 202 deactivate (AD_User_ID); 203 return false; 204 } 205 mail.setEMailUser(m_fromID, m_fromPW); 206 boolean OK = EMail.SENT_OK.equals(mail.send()); 207 if (OK) 208 log.debug("sendIndividualMail - " + EMailAddress); 209 else 210 log.warn("sendIndividualMail FAILURE - " + EMailAddress); 211 addLog(0, null, null, (OK ? "OK" : "ERROR") + " - " + EMailAddress); 212 return OK; 213 } 215 219 private void deactivate (int AD_User_ID) 220 { 221 String sql = "UPDATE AD_User " 222 + "SET IsActive='N', Help=Help||' - Invalid User EMail' " 223 + "WHERE AD_User_ID=" + AD_User_ID; 224 DB.executeUpdate(sql); 225 } 227 } | Popular Tags |