2017-06-21 5 views
-1

複数の送信者(リストで作成)から1人の受信者に電子メールを送信するコードが書かれています。私はそのリストを複数回繰り返すことができます。今、私はリスト内の各emailIDの数を、例えば、表示したいと思います。 [email protected] count = 2、[email protected] count = 3。送信された電子メールの数を取得する方法

私はbigennerです。それを行う方法を提案してください。以下は です。eamilを送信するコードです。

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    { 
     System.out.println(request.getParameter("toAddress")); 
     List<File> uploadedFiles= saveUploadedFiles(request); 
     System.out.println(request.getParameter("email")); 
     System.out.println("Reached servlet"); 
     response.getWriter().print("Hello"+request.getParameter("email")); 

     System.out.println("hii"+request.getParts()); 
     //List<File> uploadedFiles= saveUploadedFiles(request); 
     String toAddress=request.getParameter("toAddress"); 
     String subject=request.getParameter("subject"); 
     String content=request.getParameter("content"); 
     int sleeptime=Integer.parseInt(request.getParameter("emailInterval")); 
     int noOfEmailToSend=Integer.parseInt(request.getParameter("noOfEmailToSend")); 
     int iterationInterval=Integer.parseInt(request.getParameter("iterationInterval")); 
     String resultMessage = ""; //null 

     HttpSession session = request.getSession(false); 
     List<String> list = (List<String>) session.getAttribute("name"); 


     System.out.println(list); 


     List<String> sub=new ArrayList<String>(); 
     for(int i=1;i<=noOfEmailToSend;i++) 
     { 
      StringBuilder sb = new StringBuilder(); 
       sb.append(subject).append(i); 
       sub.add(sb.toString()); 
     }   
     try { 
       for(int j=0;j<noOfEmailToSend;j++) 
       {  
        Iterator<String> sendersInputIterate = list.iterator(); 
        Iterator<String> subject1 = sub.iterator(); 
        while (sendersInputIterate.hasNext() && subject1.hasNext()) 
        {    
         EmailFunction.sendEmail(ExchangeIP, port, sendersInputIterate.next(), toAddress, subject1.next(), content, uploadedFiles); 
         resultMessage = "The B-mail has been sent successfully :"; 



         Thread.sleep(sleeptime); 
        } 
         Thread.sleep(iterationInterval); 

       } 

      }catch (Exception ex) 
       { 
       ex.printStackTrace();    
       resultMessage = "There were an error: " + ex.getMessage(); 
       } finally 
       { 

         request.setAttribute("Message", resultMessage);    
         getServletContext().getRequestDispatcher("/result.jsp").forward(request, response); 
       }     
     } 
+1

てみましたか? –

+0

no..i arraylistを使用して送信者IDを作成しました。 – Dip

+0

'Map ' - key - emailID;価値カウンター –

答えて

0

あなたのコメント

への応答では、マップの使用方法について詳しく教えてくださいすることができます。

Mapインタフェースは、ここではoracleで覆われている: https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html

ここでは文字列の値に基づいてカウントするマップの使用の非常に基本的な例です。この例では、電子メールは大文字と小文字が区別されていると仮定していることに注意してください(私はそう思っていますが、実装の大部分はそうではないと想定します。小文字を区別しない)

public class EmailCounter { 

    private Map<String, Integer> destinationEmailCount = new HashMap<>(); 

    public static void main(final String[] args) { 
     EmailCounter emailCounter = new EmailCounter(); 
     emailCounter.addToMailCount("[email protected]"); 
     emailCounter.addToMailCount("[email protected]"); 
     emailCounter.addToMailCount("[email protected]"); 
     emailCounter.addToMailCount("[email protected]"); 

     System.out.println("[email protected] was mailed " + emailCounter.getEmailCount("[email protected]") + " times"); 
     System.out.println("[email protected] was mailed " + emailCounter.getEmailCount("[email protected]") + " times"); 
     System.out.println("[email protected] was mailed " + emailCounter.getEmailCount("[email protected]") + " times"); 
    } 

    public void addToMailCount(final String emailAddress) { 
     Integer existingCount = destinationEmailCount.get(emailAddress); 
     if (existingCount != null) { 
      destinationEmailCount.put(emailAddress, Integer.valueOf(existingCount.intValue() + 1)); 
      return; 
     } 
     destinationEmailCount.put(emailAddress, Integer.valueOf(1)); 
    } 

    public int getEmailCount(final String emailAddress) { 
     Integer count = destinationEmailCount.get(emailAddress); 
     return count == null ? 0 : count.intValue(); 
    } 

} 

出力は次のようになります。あなたは、地図を使用し

[email protected] was mailed 3 times 
[email protected] was mailed 1 times 
[email protected] was mailed 0 times 
+0

しかし、ここで私はemailIdのaaraylistその場合に何をする必要があります.. – Dip

+0

あなたは不適切なコレクションを使用しています。 Listは単なる値の集合です.ArrayListは配列を使用してそれらの値を格納します。一方のマップは、キーと値のペアの概念を使用します。上記の例は、電子メールアドレスをキーとして使用する方法と、値としてカウントを使用する方法を示しています。 – Goibniu

関連する問題