JavaMail multiple recipients

Discussion in 'Java' started by John Bailo, Jul 25, 2006.

  1. John Bailo

    John Bailo Guest

    I can send a JavaMail message to multiple recipients with


    ///
    javax.mail.internet.InternetAddress[] addressTo = new
    javax.mail.internet.InternetAddress[recipients.length];

    for (int i = 0; i < recipients.length; i++)
    addressTo = new javax.mail.internet.InternetAddress(recipients);

    msg.setRecipients(javax.mail.Message.RecipientType.TO, addressTo);
    ///

    But this sends out individual emails to each recipient. How can I send
    a single email with multiple recipients so that all the recipients names
    appear on the To: line?
    John Bailo, Jul 25, 2006
    #1
    1. Advertising

  2. John Bailo

    The_Sage Guest

    >Reply to article by: John Bailo <>
    >Date written: Tue, 25 Jul 2006 14:29:37 -0700
    >MsgID:<>


    >I can send a JavaMail message to multiple recipients with


    >///
    >javax.mail.internet.InternetAddress[] addressTo = new
    >javax.mail.internet.InternetAddress[recipients.length];


    >for (int i = 0; i < recipients.length; i++)
    >addressTo = new javax.mail.internet.InternetAddress(recipients);


    >msg.setRecipients(javax.mail.Message.RecipientType.TO, addressTo);
    >///


    >But this sends out individual emails to each recipient. How can I send
    >a single email with multiple recipients so that all the recipients names
    >appear on the To: line?


    Your addressTo object is a multiple element array when the "To:" field needs to
    be a single line of text, with each receipient email address concantenated to
    the others with a space and a semi-colon. Just look at how it is done on your
    email application on your computer, ie -- MS Outlook for example.

    The Sage

    =============================================================
    http://members.cox.net/the.sage/index.htm

    "All those painted screens erected by man to shut out reality
    -- history, religion, duty, social position --
    all were illusions, mere opium fantasies"
    John Fowles, The French Lieutenant's Woman
    =============================================================
    The_Sage, Jul 26, 2006
    #2
    1. Advertising

  3. John Bailo

    philmin

    Joined:
    Jun 30, 2010
    Messages:
    1
    Sendmail Help

    Hi, another newbie - my code is almost right, but I cannot work out how to send duplicate emails. I get a java.lang.ArrayIndexOutOfBoundsException, I have had the whole system working for one email, just not lots. Please help me, I am sure I am nearly there, I just need a final shove..
    thanks
    phil

    import java.io.IOException;
    import java.util.Date;
    import java.util.Properties;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;

    public class SendEmail extends Helper
    {
    //Variables to pull in form details
    public static String strChangeRequestNumber;
    public static Object objChangeRequestType;
    public static String strChangeDescription;
    public static Object objRecipients;
    public static String[] a;


    //main procedure
    public static void main(String[] args) throws IOException
    {
    ChangeRequestForm ChangeRequestForm = new ChangeRequestForm(null);
    //get variables from Form when control is returned
    String strChangeRequestNumber = ChangeRequestForm.strChangeRequestNumber;
    String strRecipients = ChangeRequestForm.strRecipients;
    Object objChangeRequestType = ChangeRequestForm.objChangeType;
    String strChangeDescription = ChangeRequestForm.strChangeDescription;
    objRecipients = ChangeRequestForm.objRecipient;
    a = ChangeRequestForm.a;

    Properties props = (Properties)System.getProperties().clone();
    props.put("mail.smtp.host", "smtp.mysmtp.here");


    // set as properties as needed using current session
    Session session = Session.getInstance(props, null);
    session.setDebug(true);

    try {
    Message msg = new MimeMessage(session);

    //***edit in to dictate from address (if allowed)***
    msg.setFrom(new InternetAddress("MyFromEmailAddressHere"));
    //InternetAddress[] address = {new InternetAddress((String) objRecipients)};
    //InternetAddress[] address = {new InternetAddress((String) strRecipients)};

    InternetAddress[] addressTo = new InternetAddress[strRecipients.length()];
    //(StrRecipients is produced from a combibox with multiple selections enabled, I am happy with the output from this - each email addr is seperated by a comma etc)

    //For each recipient, send an email..if only...
    for (int i = 0; i < strRecipients.length(); i++)
    addressTo = new InternetAddress(a);
    msg.setRecipients(Message.RecipientType.TO, addressTo);

    ///


    //msg.setRecipients(Message.RecipientType.TO, address);
    //msg.setRecipients(Message.RecipientType.TO, address);
    msg.setSubject("My subject here: " + objChangeRequestType + strChangeRequestNumber);
    msg.setSentDate(new Date());

    //HTML format of email
    strMsgText =("<html><head><title></title></head>" +
    "<span style = \"FONT-family: Tahoma\">" +
    "<body><p>Hi,</p>" +
    "<p>some text:<br><br>" +
    "<span style=\"BACKGROUND-COLOR: #ffffff\">" +
    objChangeRequestType +
    strChangeRequestNumber +
    "<br><br>" + "Description: " +
    strChangeDescription +
    "</span></p>" +
    "<p>&nbsp;</p></body></html>");

    msg.setContent(Helper.strMsgText, "text/html");

    calculateDate();
    writeEmailToFile();

    Transport.send(msg);
    displayMsgBox("email sent to" + ChangeRequestForm.a[ChangeRequestForm.q]);
    displayMsgBox("email sent, no option to recall!");
    }
    catch (MessagingException e) {
    displayMsgBox("error sending message");
    e.printStackTrace();
    }

    }

    }
    philmin, Jun 30, 2010
    #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. Erica

    Multiple bcc recipients

    Erica, Oct 6, 2003, in forum: Perl
    Replies:
    1
    Views:
    768
    Gunnar Hjalmarsson
    Oct 6, 2003
  2. .Net Newbie

    Single email to multiple recipients

    .Net Newbie, Jun 30, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    9,398
    .Net Newbie
    Jul 1, 2004
  3. Alex Hunsley
    Replies:
    2
    Views:
    861
    Paul Wright
    Jul 1, 2004
  4. Replies:
    1
    Views:
    386
    Rene Pijlman
    Feb 20, 2006
  5. news.microsoft.com

    ASPEmail to send to multiple recipients?

    news.microsoft.com, Dec 8, 2003, in forum: ASP General
    Replies:
    15
    Views:
    325
    Roland Hall
    Dec 10, 2003
Loading...

Share This Page