Home > Enterprise >  StringBuilder Usage in Mail Sending Batch Java
StringBuilder Usage in Mail Sending Batch Java

Time:10-05

I have a method for filling html data cells with data.

In this method I have a mailBody variable. I want to use StringBuilder instead of mailBody. But I don't know how to do this in my case.

Note: tdClose is defined as "</td>"

Where I want to use StringBuilder:

mailBody  = "<tr><td>"   notificationList.get(i).getMid()   tdClose;
        mailBody  = "<td>"   notificationList.get(i).getMdStatus()   tdClose;
        mailBody  = "<td>"   notificationList.get(i).getBin()   tdClose;
        mailBody  = "<td>"   notificationList.get(i).getCount()   tdClose;
        mailBody  = "<td>"   notificationList.get(i).getRatio()   tdClose;
        mailBody  = "<td>"   notificationList.get(i).getMerchantName()   "</td></tr>";

My method:

public String createMailBody(List<VposNotificationBatchDto> notificationList) {

    StringBuilder header = new StringBuilder("<table><tr><th>ÜYE İŞYERİ ID</th><th>MD STATUS</th><th>BIN</th>");
    header.append("<th>İŞLEM SAYISI</th><th>ORAN</th><th>ÜYE İŞYERİ</th></tr>");

    String mailBody = "Merhaba,<br/><br/>Güvenli İşlem Raporu için işlem detayları aşağıdaki gibidir.";
    mailBody  = "<br/><br/><br/>";
    mailBody  = "<html><head><style>table{font-family: arial, sans-serif;border-collapse: collapse;width: 75%;}th{background-color: #5fa8cc}";
    mailBody  = "td,th{border: 1px solid #dddddd;text-align: left;padding: 8px;}tr:nth-child(even){background-color: #dddddd;}</style>";
    mailBody  = "</head><body><h3>Güvenli İşlem Rapor Detayları:</h3>"   header;

    for (int i = 0; i < notificationList.size(); i  ) {

        mailBody  = "<tr><td>"   notificationList.get(i).getMid()   tdClose;
        mailBody  = "<td>"   notificationList.get(i).getMdStatus()   tdClose;
        mailBody  = "<td>"   notificationList.get(i).getBin()   tdClose;
        mailBody  = "<td>"   notificationList.get(i).getCount()   tdClose;
        mailBody  = "<td>"   notificationList.get(i).getRatio()   tdClose;
        mailBody  = "<td>"   notificationList.get(i).getMerchantName()   "</td></tr>";

        String currentMailSubject = batchProps.getJobProps().get(jobName).getAlertProps().getMailSubject();
    }
    mailBody  = endOfHtmlString;
    return mailBody;
}

CodePudding user response:

You have already used StringBuilder in header same you can use in mailBody.

public String createMailBody(List<VposNotificationBatchDto> notificationList) {

    StringBuilder header = new StringBuilder("<table><tr><th>ÜYE İŞYERİ ID</th><th>MD STATUS</th><th>BIN</th>");
    header.append("<th>İŞLEM SAYISI</th><th>ORAN</th><th>ÜYE İŞYERİ</th></tr>");

    StringBuilder mailBody =
            new StringBuilder("Merhaba,<br/><br/>Güvenli İşlem Raporu için işlem detayları aşağıdaki gibidir.");
    mailBody.append("<br/><br/><br/>");
    mailBody.append(
            "<html><head><style>table{font-family: arial, sans-serif;border-collapse: collapse;width: 75%;}th{background-color: #5fa8cc}");
    mailBody.append(
            "td,th{border: 1px solid #dddddd;text-align: left;padding: 8px;}tr:nth-child(even){background-color: #dddddd;}</style>");
    mailBody.append("</head><body><h3>Güvenli İşlem Rapor Detayları:</h3>").append(header.toString());

    for (int i = 0; i < notificationList.size(); i  ) {

        mailBody.append("<tr><td>").append(notificationList.get(i).getMid()).append(tdClose);
        mailBody.append("<td>").append(notificationList.get(i).getMdStatus()).append(tdClose);
        mailBody.append("<td>").append(notificationList.get(i).getBin()).append(tdClose);
        mailBody.append("<td>").append(notificationList.get(i).getCount()).append(tdClose);
        mailBody.append("<td>").append(notificationList.get(i).getRatio()).append(tdClose);
        mailBody.append("<td>").append(notificationList.get(i).getMerchantName()).append("</td></tr>");

        String currentMailSubject = batchProps.getJobProps().get(jobName).getAlertProps().getMailSubject();
    }
    mailBody.append(endOfHtmlString);
    return mailBody.toString();
}

If you are using eclipse then you can configure save action in that it will automatically convert string concat with string builder.

CodePudding user response:

You have already used StringBuilder in header which is perfectly correct. Then just the same way you can implement. just return "return mailBody.toString();"

  •  Tags:  
  • java
  • Related