How to Send to Multiple People with Java.Mail

Discussion in 'Java' started by Shane Jaworski, Jun 29, 2004.

  1. Here is a example

    <%@ page import="java.sql.*" %>
    <%@ page import="java.util.*" %>
    <%@ page import="javax.mail.*" %>
    <%@ page import="javax.mail.internet.*" %>


    <%

    String toemail = "";
    String fromemail = "";
    String last_email = "";

    Properties props = new Properties();
    props.put("mail.smtp.host", mailserver);
    last_email = "";
    to_email = "";
    fromemail = "";
    toemail = toemail + "," + last_email;
    Session mailSession = Session.getInstance(props, null);
    Address fromUser = new InternetAddress(fromemail);
    Address anotherUser = new InternetAddress(last_email);
    Address[] toUser = InternetAddress.parse(toemail);
    //Address toUser = new InternetAddress(toemail);
    Message body = new MimeMessage(mailSession);
    body.setRecipients(Message.RecipientType.TO, toUser);
    body.setRecipient(Message.RecipientType.CC, fromUser);
    body.setFrom(fromUser);
    body.setSubject("Lots of Stuff.");
    body.setContent("Sent: " + new java.util.Date() + "\n\nPlease respond
    to this request as soon as possible.\n\n", "text/plain");
    Transport.send(body);

    %>
    Shane Jaworski, Jun 29, 2004
    #1
    1. Advertising

  2. Shane Jaworski

    Sumukh Guest

    HI Shane,
    You can read comma seperated list of recipients from some property
    file/text file. Store the list in an arraylist.


    String recipients=(String)props.getProperty("recipients");
    ArrayList recipientsArray=new ArrayList();
    StringTokenizer st = new StringTokenizer(recipients,",");
    while (st.hasMoreTokens()) {
    recipientsArray.add(st.nextToken());
    }


    and then you can set those reipients like this.

    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);
    int sizeTo=recipientsArray.size();
    InternetAddress[] addressTo = new InternetAddress[sizeTo];
    for (int i = 0; i < sizeTo; i++)
    {
    addressTo = new InternetAddress(recipientsArray.get(i).toString());
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);




    hth

    Sumukh





    Shane Jaworski wrote:

    > Here is a example
    >
    > <%@ page import="java.sql.*" %>
    > <%@ page import="java.util.*" %>
    > <%@ page import="javax.mail.*" %>
    > <%@ page import="javax.mail.internet.*" %>
    >
    >
    > <%
    >
    > String toemail = "";
    > String fromemail = "";
    > String last_email = "";
    >
    > Properties props = new Properties();
    > props.put("mail.smtp.host", mailserver);
    > last_email = "";
    > to_email = "";
    > fromemail = "";
    > toemail = toemail + "," + last_email;
    > Session mailSession = Session.getInstance(props, null);
    > Address fromUser = new InternetAddress(fromemail);
    > Address anotherUser = new InternetAddress(last_email);
    > Address[] toUser = InternetAddress.parse(toemail);
    > //Address toUser = new InternetAddress(toemail);
    > Message body = new MimeMessage(mailSession);
    > body.setRecipients(Message.RecipientType.TO, toUser);
    > body.setRecipient(Message.RecipientType.CC, fromUser);
    > body.setFrom(fromUser);
    > body.setSubject("Lots of Stuff.");
    > body.setContent("Sent: " + new java.util.Date() + "\n\nPlease respond
    > to this request as soon as possible.\n\n", "text/plain");
    > Transport.send(body);
    >
    > %>
    Sumukh, Jun 29, 2004
    #2
    1. Advertising

  3. Shane Jaworski

    Sumukh Guest

    HI Shane,
    You can read comma seperated list of recipients from some property
    file/text file. Store the list in an arraylist.


    String recipients=(String)props.getProperty("recipients");
    ArrayList recipientsArray=new ArrayList();
    StringTokenizer st = new StringTokenizer(recipients,",");
    while (st.hasMoreTokens()) {
    recipientsArray.add(st.nextToken());
    }


    and then you can set those reipients like this.

    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);
    int sizeTo=recipientsArray.size();
    InternetAddress[] addressTo = new InternetAddress[sizeTo];
    for (int i = 0; i < sizeTo; i++)
    {
    addressTo = new InternetAddress(recipientsArray.get(i).toString());
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);




    hth

    Sumukh





    Shane Jaworski wrote:

    > Here is a example
    >
    > <%@ page import="java.sql.*" %>
    > <%@ page import="java.util.*" %>
    > <%@ page import="javax.mail.*" %>
    > <%@ page import="javax.mail.internet.*" %>
    >
    >
    > <%
    >
    > String toemail = "";
    > String fromemail = "";
    > String last_email = "";
    >
    > Properties props = new Properties();
    > props.put("mail.smtp.host", mailserver);
    > last_email = "";
    > to_email = "";
    > fromemail = "";
    > toemail = toemail + "," + last_email;
    > Session mailSession = Session.getInstance(props, null);
    > Address fromUser = new InternetAddress(fromemail);
    > Address anotherUser = new InternetAddress(last_email);
    > Address[] toUser = InternetAddress.parse(toemail);
    > //Address toUser = new InternetAddress(toemail);
    > Message body = new MimeMessage(mailSession);
    > body.setRecipients(Message.RecipientType.TO, toUser);
    > body.setRecipient(Message.RecipientType.CC, fromUser);
    > body.setFrom(fromUser);
    > body.setSubject("Lots of Stuff.");
    > body.setContent("Sent: " + new java.util.Date() + "\n\nPlease respond
    > to this request as soon as possible.\n\n", "text/plain");
    > Transport.send(body);
    >
    > %>
    Sumukh, Jun 29, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nathan Sokalski
    Replies:
    3
    Views:
    1,931
    Larry Lard
    Nov 23, 2005
  2. =?Utf-8?B?Unlhbg==?=
    Replies:
    2
    Views:
    3,880
    Steven Cheng[MSFT]
    Feb 28, 2006
  3. refv8
    Replies:
    2
    Views:
    27,039
    ittower
    Oct 13, 2006
  4. Mike P
    Replies:
    1
    Views:
    557
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Mar 1, 2007
  5. Replies:
    1
    Views:
    693
    Roger Lindsjö
    Jun 11, 2008
Loading...

Share This Page