Search in sources :

Example 1 with EMail

use of org.compiere.util.EMail in project adempiere by adempiere.

the class DunningPrint method doIt.

//	prepare
/**
	 * Process
	 * @return info
	 * @throws Exception
	 */
protected String doIt() throws Exception {
    log.info("C_DunningRun_ID=" + p_C_DunningRun_ID + ",R_MailText_ID=" + p_R_MailText_ID + ", EmailPDF=" + p_EMailPDF + ",IsOnlyIfBPBalance=" + p_IsOnlyIfBPBalance + ",PrintUnprocessedOnly=" + p_PrintUnprocessedOnly);
    //	Need to have Template
    if (p_EMailPDF && p_R_MailText_ID == 0)
        throw new AdempiereUserError("@NotFound@: @R_MailText_ID@");
    //		String subject = "";
    MMailText mText = null;
    if (p_EMailPDF) {
        mText = new MMailText(getCtx(), p_R_MailText_ID, get_TrxName());
        if (p_EMailPDF && mText.get_ID() == 0)
            throw new AdempiereUserError("@NotFound@: @R_MailText_ID@ - " + p_R_MailText_ID);
    //			subject = mText.getMailHeader();
    }
    //
    MDunningRun run = new MDunningRun(getCtx(), p_C_DunningRun_ID, get_TrxName());
    if (run.get_ID() == 0)
        throw new AdempiereUserError("@NotFound@: @C_DunningRun_ID@ - " + p_C_DunningRun_ID);
    MClient client = MClient.get(getCtx());
    int count = 0;
    int errors = 0;
    MDunningRunEntry[] entries = run.getEntries(false);
    for (int i = 0; i < entries.length; i++) {
        MDunningRunEntry entry = entries[i];
        //	Print Format on Dunning Level
        MDunningLevel level = new MDunningLevel(getCtx(), entry.getC_DunningLevel_ID(), get_TrxName());
        MPrintFormat format = null;
        if (level.getDunning_PrintFormat_ID() > 0)
            format = MPrintFormat.get(getCtx(), level.getDunning_PrintFormat_ID(), false);
        if (p_IsOnlyIfBPBalance && entry.getAmt().signum() <= 0)
            continue;
        if (p_PrintUnprocessedOnly && entry.isProcessed())
            continue;
        //	To BPartner
        MBPartner bp = new MBPartner(getCtx(), entry.getC_BPartner_ID(), get_TrxName());
        if (bp.get_ID() == 0) {
            addLog(entry.get_ID(), null, null, "@NotFound@: @C_BPartner_ID@ " + entry.getC_BPartner_ID());
            errors++;
            continue;
        }
        //	To User
        MUser to = new MUser(getCtx(), entry.getAD_User_ID(), get_TrxName());
        if (p_EMailPDF) {
            if (to.get_ID() == 0) {
                addLog(entry.get_ID(), null, null, "@NotFound@: @AD_User_ID@ - " + bp.getName());
                errors++;
                continue;
            } else if (to.getEMail() == null || to.getEMail().length() == 0) {
                addLog(entry.get_ID(), null, null, "@NotFound@: @EMail@ - " + to.getName());
                errors++;
                continue;
            }
        }
        //	query
        MQuery query = new MQuery("C_Dunning_Header_v");
        query.addRestriction("C_DunningRunEntry_ID", MQuery.EQUAL, new Integer(entry.getC_DunningRunEntry_ID()));
        //	Engine
        PrintInfo info = new PrintInfo(bp.getName(), MDunningRunEntry.Table_ID, entry.getC_DunningRunEntry_ID(), entry.getC_BPartner_ID());
        info.setDescription(bp.getName() + ", Amt=" + entry.getAmt());
        ReportEngine re = null;
        if (format != null)
            re = new ReportEngine(getCtx(), format, query, info);
        boolean printed = false;
        if (p_EMailPDF) {
            EMail email = client.createEMail(to.getEMail(), null, null);
            if (!email.isValid()) {
                addLog(entry.get_ID(), null, null, "@RequestActionEMailError@ Invalid EMail: " + to);
                errors++;
                continue;
            }
            //	variable context
            mText.setUser(to);
            mText.setBPartner(bp);
            mText.setPO(entry);
            String message = mText.getMailText(true);
            if (mText.isHtml())
                email.setMessageHTML(mText.getMailHeader(), message);
            else {
                email.setSubject(mText.getMailHeader());
                email.setMessageText(message);
            }
            //
            if (re != null) {
                File attachment = re.getPDF(File.createTempFile("Dunning", ".pdf"));
                log.fine(to + " - " + attachment);
                email.addAttachment(attachment);
            }
            //
            String msg = email.send();
            MUserMail um = new MUserMail(mText, entry.getAD_User_ID(), email);
            um.saveEx();
            if (msg.equals(EMail.SENT_OK)) {
                addLog(entry.get_ID(), null, null, bp.getName() + " @RequestActionEMailOK@");
                count++;
                printed = true;
            } else {
                addLog(entry.get_ID(), null, null, bp.getName() + " @RequestActionEMailError@ " + msg);
                errors++;
            }
        } else {
            if (re != null) {
                re.print();
                count++;
                printed = true;
            }
        }
        if (printed) {
            entry.setProcessed(true);
            entry.save();
        }
    }
    //	for all dunning letters
    if (errors == 0) {
        run.setProcessed(true);
        run.saveEx();
    }
    if (p_EMailPDF)
        return "@Sent@=" + count + " - @Errors@=" + errors;
    return "@Printed@=" + count;
}
Also used : AdempiereUserError(org.compiere.util.AdempiereUserError) MMailText(org.compiere.model.MMailText) MUserMail(org.compiere.model.MUserMail) MQuery(org.compiere.model.MQuery) PrintInfo(org.compiere.model.PrintInfo) MDunningRun(org.compiere.model.MDunningRun) MBPartner(org.compiere.model.MBPartner) EMail(org.compiere.util.EMail) MClient(org.compiere.model.MClient) MDunningRunEntry(org.compiere.model.MDunningRunEntry) MPrintFormat(org.compiere.print.MPrintFormat) ReportEngine(org.compiere.print.ReportEngine) MUser(org.compiere.model.MUser) File(java.io.File) MDunningLevel(org.compiere.model.MDunningLevel)

Example 2 with EMail

use of org.compiere.util.EMail in project adempiere by adempiere.

the class PayrollViaEMail method sendIndividualMail.

//	sendBPGroup
/**
	 * 	Send Individual Mail
	 *	@param bPartnerId user
	 *	@param unSubscribe unsubscribe message
	 *	@return true if mail has been sent
	 */
private Boolean sendIndividualMail(int bPartnerId, String unSubscribe) {
    try {
        MBPartner employee = new MBPartner(getCtx(), bPartnerId, null);
        String message = mailText.getMailText(true);
        if (unSubscribe != null)
            message += unSubscribe;
        StringBuffer whereClause = new StringBuffer();
        whereClause.append(MBPartnerLocation.COLUMNNAME_C_BPartner_ID).append(" = ? AND ").append(MBPartnerLocation.COLUMNNAME_ContactType).append("=?");
        MBPartnerLocation location = new Query(getCtx(), MBPartnerLocation.Table_Name, whereClause.toString(), get_TrxName()).setOnlyActiveRecords(true).setParameters(bPartnerId, MBPartnerLocation.CONTACTTYPE_Primary).first();
        if (location == null) {
            addLog(0, null, null, employee.getName() + " @Email@ @NotFound@");
            return false;
        }
        MClient client = MClient.get(getCtx());
        String eMailFrom = client.getRequestEMail();
        String emailFrom = location.get_ValueAsString("EMail");
        String userMailFrom = client.getRequestUser();
        String password = client.getRequestUserPW();
        //	FR [ 402 ]
        //	Add support to new send mail
        EMail email = new EMail(client, eMailFrom, emailFrom, mailText.getMailHeader(), message);
        if (mailText.isHtml())
            email.setMessageHTML(mailText.getMailHeader(), message);
        else {
            email.setSubject(mailText.getMailHeader());
            email.setMessageText(message);
        }
        email.addAttachment(CreatePDF(bPartnerId));
        if (!email.isValid() && !email.isValid(true)) {
            log.warning("NOT VALID - " + email);
            employee.setIsActive(false);
            employee.save();
            return Boolean.FALSE;
        }
        email.createAuthenticator(userMailFrom, password);
        boolean OK = EMail.SENT_OK.equals(email.send());
        if (OK) {
            addLog(0, null, null, employee.getName() + " @Email@ @OK@");
            log.fine(employee.getURL());
        } else
            log.warning("FAILURE - " + employee.getURL());
        addLog(0, null, null, (OK ? "@OK@" : "@ERROR@") + " - " + emailFrom);
        return OK;
    } catch (Exception e) {
        return Boolean.FALSE;
    }
}
Also used : Query(org.compiere.model.Query) MBPartner(org.compiere.model.MBPartner) EMail(org.compiere.util.EMail) MBPartnerLocation(org.compiere.model.MBPartnerLocation) MClient(org.compiere.model.MClient)

Example 3 with EMail

use of org.compiere.util.EMail in project adempiere by adempiere.

the class MRfQResponse method sendRfQ.

//	toString
/**************************************************************************
	 * 	Send RfQ
	 *	@return true if RfQ is sent per email.
	 */
public boolean sendRfQ() {
    MUser to = MUser.get(getCtx(), getAD_User_ID());
    if (to.get_ID() == 0 || to.getEMail() == null || to.getEMail().length() == 0) {
        log.log(Level.SEVERE, "No User or no EMail - " + to);
        return false;
    }
    MClient client = MClient.get(getCtx());
    //
    String message = m_rfq.getDescription();
    if (message == null || message.length() == 0)
        message = getHelp();
    else if (m_rfq.getHelp() != null)
        message += "\n" + m_rfq.getHelp();
    if (message == null)
        message = getName();
    //
    EMail email = client.createEMail(to.getEMail(), "RfQ: " + getName(), message);
    MRole rol = new MRole(getCtx(), Env.getAD_Role_ID(getCtx()), get_TrxName());
    if (rol.getSupervisor() != null && rol.getSupervisor().getEMail() != null)
        email.addCc(rol.getSupervisor().getEMail());
    MUser user = new MUser(getCtx(), Env.getAD_User_ID(getCtx()), get_TrxName());
    email.addAttachment(createPDF());
    if (user.getEMail() != null && user.getEMailUserPW() != null) {
        email.setFrom(user.getEMail());
        email.createAuthenticator(user.getEMail(), user.getEMailUserPW());
    }
    if (EMail.SENT_OK.equals(email.send())) {
        setDateInvited(new Timestamp(System.currentTimeMillis()));
        save();
        return true;
    }
    return false;
}
Also used : EMail(org.compiere.util.EMail) Timestamp(java.sql.Timestamp)

Example 4 with EMail

use of org.compiere.util.EMail in project adempiere by adempiere.

the class SendMailText method sendIndividualMail.

//	sendBPGroup
/**
	 * 	Send Individual Mail
	 *	@param Name user name
	 *	@param AD_User_ID user
	 *	@param unsubscribe unsubscribe message
	 *	@return true if mail has been sent
	 */
private Boolean sendIndividualMail(String Name, int AD_User_ID, String unsubscribe) {
    //	Prevent two email
    Integer ii = new Integer(AD_User_ID);
    if (m_list.contains(ii))
        return null;
    m_list.add(ii);
    //
    MUser to = new MUser(getCtx(), AD_User_ID, null);
    //	parse context
    m_MailText.setUser(AD_User_ID);
    String message = m_MailText.getMailText(true);
    //	Unsubscribe
    if (unsubscribe != null)
        message += unsubscribe;
    //
    EMail email = m_client.createEMail(m_from, to, m_MailText.getMailHeader(), message);
    if (m_MailText.isHtml())
        email.setMessageHTML(m_MailText.getMailHeader(), message);
    else {
        email.setSubject(m_MailText.getMailHeader());
        email.setMessageText(message);
    }
    if (!email.isValid() && !email.isValid(true)) {
        log.warning("NOT VALID - " + email);
        to.setIsActive(false);
        to.addDescription("Invalid EMail");
        to.saveEx();
        return Boolean.FALSE;
    }
    boolean OK = EMail.SENT_OK.equals(email.send());
    new MUserMail(m_MailText, AD_User_ID, email).saveEx();
    //
    if (OK)
        log.fine(to.getEMail());
    else
        log.warning("FAILURE - " + to.getEMail());
    addLog(0, null, null, (OK ? "@OK@" : "@ERROR@") + " - " + to.getEMail());
    return new Boolean(OK);
}
Also used : MUserMail(org.compiere.model.MUserMail) EMail(org.compiere.util.EMail) MUser(org.compiere.model.MUser)

Example 5 with EMail

use of org.compiere.util.EMail in project adempiere by adempiere.

the class ConfigurationData method testMailServer.

//	testMail
/**
	 * 	Test Mail
	 * 	@param mailServer mail server
	 * 	@param adminEMail email of admin
	 * 	@param mailUser user ID
	 * 	@param mailPassword password
	 * 	@param port
	 * 	@param protocol
	 * 	@param encryptionType
	 * 	@param authMechanism
	 *  @return true of OK
	 */
private boolean testMailServer(InetAddress mailServer, InternetAddress adminEMail, String mailUser, String mailPassword, int port, String protocol, String encryptionType, String authMechanism) {
    boolean smtpOK = false;
    boolean imapOK = false;
    //	Change Protocol
    if (protocol == null) {
        protocol = MEMailConfig.PROTOCOL_SMTP;
    } else {
        if (protocol.length() > 1) {
            protocol = protocol.substring(0, 1);
        }
    }
    //	Change Encryption Type
    if (encryptionType == null) {
        encryptionType = MEMailConfig.ENCRYPTIONTYPE_None;
    } else {
        if (encryptionType.length() > 1) {
            encryptionType = encryptionType.substring(0, 1);
        }
    }
    //	Change Authentication Mechanism
    if (authMechanism == null) {
        authMechanism = MEMailConfig.AUTHMECHANISM_Login;
    } else {
        if (authMechanism.length() > 1) {
            authMechanism = authMechanism.substring(0, 1);
        }
    }
    if (testPort(mailServer, port, true)) {
        log.config("OK: SMTP Server contacted");
        smtpOK = true;
    } else {
        log.info("SMTP Server NOT available");
    }
    //	For IMAP
    if (testPort(mailServer, port, true)) {
        log.config("OK: IMAP4 Server contacted");
        imapOK = true;
    } else {
        log.info("IMAP4 Server NOT available");
    }
    //
    if (!smtpOK) {
        String error = "No active Mail Server";
        if (p_panel != null)
            p_panel.signalOK(p_panel.okMailServer, "ErrorMailServer", false, false, error);
        log.warning(error);
        return false;
    }
    //
    try {
        //	FR [ 402 ]
        //	Add support to send mail without context
        EMail email = new EMail(mailServer.getHostName(), port, protocol, encryptionType, authMechanism, adminEMail.toString(), adminEMail.toString(), "Adempiere Server Setup Test", "Test: " + getProperties(), false);
        email.createAuthenticator(mailUser, mailPassword);
        if (EMail.SENT_OK.equals(email.send())) {
            log.info("OK: Send Test Email to " + adminEMail);
        } else {
            log.warning("Could NOT send Email to " + adminEMail);
        }
    } catch (Exception ex) {
        log.severe(ex.getLocalizedMessage());
        return false;
    }
    //
    if (!imapOK)
        return false;
    //	
    return true;
}
Also used : EMail(org.compiere.util.EMail) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Aggregations

EMail (org.compiere.util.EMail)17 MUserMail (org.compiere.model.MUserMail)8 MUser (org.compiere.model.MUser)5 MMailText (org.compiere.model.MMailText)4 File (java.io.File)3 MClient (org.compiere.model.MClient)3 ReportEngine (org.compiere.print.ReportEngine)3 SQLException (java.sql.SQLException)2 Timestamp (java.sql.Timestamp)2 StringTokenizer (java.util.StringTokenizer)2 MAsset (org.compiere.model.MAsset)2 MBPartner (org.compiere.model.MBPartner)2 MQuery (org.compiere.model.MQuery)2 PrintInfo (org.compiere.model.PrintInfo)2 MPrintFormat (org.compiere.print.MPrintFormat)2 AdempiereUserError (org.compiere.util.AdempiereUserError)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 PreparedStatement (java.sql.PreparedStatement)1