Sending mail to a group

Hi all
I got the following example from the net. This is to send mail to one person, but how do I send the same message to more than one person?
Thanx
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException
boolean debug = false;
//Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.jcom.net");
// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
// create a message
Message msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++)
addressTo[i] = new InternetAddress(recipients);
msg.setRecipients(Message.RecipientType.TO, addressTo);
msg.addHeader("MyHeaderName", "myHeaderValue");
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);

Here is an SMTP class I wrote using java mail. It supports mulitple addresses and attachements. When calling setToAddress make sure your addresses are in a string each seperated by a semi colon.
package com.elg.utils;
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
*This class is used to provide a easier means to send an smtp email using Java Mail
* Last Updated: 11-3-2003  By: Paul Zepernick
* @author     Paul Zepernick
* @version 1.0
public class SmtpEmail {
     /**array to hold attachments*/
     private ArrayList attachments = null;
     /**from email address*/
     private String fromAddress = null;
     /**to email address*/
     private String toAddress = null;
     /**email subject*/
     private String subject = null;
     /**email body*/
     private String emailBody = null;
     /**email properties*/
     private Properties props = null;
      * Constructs a new smtp class.
      * @param Smtp Server
      * @param Smtp Port - should be set to 25 for default
     public SmtpEmail(String smtpServer,int port){
          super();
          props = System.getProperties();
          props.put("mail.smtp.host",smtpServer);
          props.put("mail.smtp.port",String.valueOf(port)); 
          attachments = new ArrayList();
     public void sendMail() throws MessagingException{
               Session s = Session.getInstance(props,null);          
               MimeMessage message = new MimeMessage(s);     
               message.setFrom(new InternetAddress(fromAddress));
               message.addRecipients(Message.RecipientType.TO, buildAddress(toAddress));
               message.setSubject(subject);
               if (attachments.size() == 0){
                    message.setContent(emailBody, "text/plain");
               }else{
                    Multipart mp = new MimeMultipart();
                    message.setContent(mp);
                    // Message part 1 is text - add to multipart
                    MimeBodyPart m1 = new MimeBodyPart();
                    m1.setContent(emailBody, "text/plain");
                    mp.addBodyPart(m1);
                    // Message part 2 is binary (file to send)
                    MimeBodyPart m2 = null;
                    /**add attachments to the multipart message*/
                    for (int i=0; i<attachments.size(); i++){
                         FileDataSource fds = new FileDataSource((String)attachments.get(i));
                         m2 = new MimeBodyPart();
                         m2.setDataHandler(new DataHandler(fds) );
                         m2.setFileName( new File((String)attachments.get(i)).getName() );
                         mp.addBodyPart(m2);     
               Transport.send(message);
               return;
      * Attachs a file to the email
      * @param String path to file
      * @return void
      * @exception FileNotFound
    public void attachFile(String path) throws FileNotFoundException{
         if (!new File(path).exists()){
              throw new FileNotFoundException ("INVALID FILE");
         attachments.add(path);
      * Sets the emailBody.
      * @param emailBody The emailBody to set
     public void setEmailBody(String emailBody) {
          this.emailBody = emailBody;
      * Sets the fromAddress.
      * @param fromAddress The fromAddress to set
     public void setFromAddress(String fromAddress) {
          this.fromAddress = fromAddress;
      * Sets the toAddress. Seperate multiple to addresses by a semi colon
      * @param toAddress The toAddress to set
     public void setToAddress(String toAddress) {
          this.toAddress = toAddress;
      * Sets the subject.
      * @param subject The subject to set
     public void setSubject(String subject) {
          this.subject = subject;
      * Builds a to address array
      * @param String address list semi colon seperated
      * @return InternetAddress
     private static InternetAddress[] buildAddress(String addressList) throws AddressException{
          StringTokenizer st = new StringTokenizer(addressList, ";");
          InternetAddress[] address = new InternetAddress[st.countTokens()];
          int i = 0;
          while ( st.hasMoreTokens() ){
               address[i++] = new InternetAddress( st.nextToken() );
          return address;
}

Similar Messages

  • Cannot send mail to a group if hiding addresses

    I can't send mail to a group unless I show all member addresses. If I uncheck this preference I recieve the following error dialogue:
    This message could not be delivered and will remain in your Outbox until it can be delivered.
    Sending the message content to the server failed.
    Very frustrating and infuriating for my reciepients. Any ideas?
    MacBook Pro   Mac OS X (10.4.9)   2.16 GHz Intel Core Duo - 1GB 667 MHz RAM - 100GB

    Thank you Klaus,
    In mail help, Bcc is indicated as an alternative to unchecking the box saying show names in group when sending. Any ideas as to why the preference doesn't work?
    MacBook Pro   Mac OS X (10.4.9)   2.16 GHz Intel Core Duo - 1GB 667 MHz RAM - 100GB

  • Best way to send mail to a group of more than 500

    One of our users has to send email to a group of more than 500 (all external, not spam). What is the best way ? At present the user is using a php script and while the emails are sitting in the queue to be sent, all other user's emails are queued. Is there any other way (not using a second server).
    Also interested in knowing if there is anyway to see that these emails are not marked as junk by yahoo or hotmail.
    Appreciate your help.

    I would not have thought that 500 emails would take too much time to go out (but can the script be triggered at some time in the morning?). However, if you have a standard server then all outgoing mail will be getting scanned for junk & viruses (same as inbound) and that will take the majority of time spent. To avoid this, you can set up an alternative submission port which bypasses the scans. See UptimeJeff's web site how-to...
    http://mac007.com/?Tips:AlternateSMTPPorts
    The original forum thread prior to the website article...
    http://discussions.apple.com/thread.jspa?messageID=3008740
    As for yahoo/hotmail question, unless your server is badly configured (host name, etc) and/or the emails 'look' like spam, I would doubt they will trigger the usual tests. You could always create a yahoo/hotmail account to check.
    -david

  • Cannot Send Mail to Distribution Group

    We are running Exchange 2013 and have a problem where users are getting NDR reports when trying to send mail with a small attachment a Universal Distribution Group. The message received back is:
    Delivery has failed to these recipients or groups:
    _Group ([email protected])
    The recipient won't be able to receive this message because it's too large.
    The maximum message size that's allowed is 2 MB. This message is 3 MB
    Why are messages to distribution groups capped at 2MB and how can I increase it to at least 10?

    Hi Will,
    Check the distributiongroup maxreceivesize parameter. With Powershell.
    Get-DistributionGroup -Name <Name> | FL
    It can be changed using:
    Set-DistributionGroup -Name <Name> -MaxReceiveSize XXXXX
    It can also be changed in the Exchange Administreative Center here:
     Recipients > Mailboxes > EditEdit icon > Mailbox features > Mail flow > Message size restrictions > View details > Received messages
    You can Refer to this link for further Exchange 2013 Message size limits.
    http://technet.microsoft.com/en-us/library/bb124345(v=exchg.150).aspx
    All the best, Jesper Hassing - MCTS SCCM 2012 - MCSA 2012 Server - MCP

  • Send mail to list group with PL/SQL

    I have a source code for sending mails using PL/SQL, but when I tried to put a group list name it doesn't work.
    For example I have a group list called "several" this is the name of the group which mail addres is [email protected]
    When I send a mail to [email protected] also the message must be sent to this directions [email protected], [email protected], [email protected], which are the email adresses that belong to the group calle "several", when I try to send to this group the message is not sent.
    As you know when you are try to send with outlook, firebird, etc and you write the name of the contact or group, because is most easier than write the email address.
    Do you have some source code for sending mail?
    This source code must send mails when in the TO parameter contains the name of the contact o name of group contacts without have to write the email address. I have do it in Java but I need to know if is posible in PL/SQL.
    Best Regards
    Edited by: Erik Dguez. Ant. on Mar 25, 2009 8:57 AM

    Hello Erik Dguez,
    for example...
    Create OR Replace Procedure PRC_SEND_MAIL( pSender     IN Varchar2
                                             , pRecipients IN Varchar2
                                             , pSubject    IN Varchar2
                                             , pBody       IN Varchar2 ) Is
       vConn  Utl_Smtp.connection;
       vReply Utl_Smtp.reply;
       vRcpt  Varchar2(128);
       vRcpts Varchar2(2000);
    Begin
       vReply := Utl_Smtp.Open_Connection( 'smtp.domain.com', 25, vConn, 60 );
       vReply := Utl_Smtp.Helo( vConn, 'domain.com' );
       vReply := Utl_Smtp.Mail( vConn, pSender || '@domain.com', Null);
       vRcpts := pRecipients;
       If Substr(vRcpts,Length(vRcpts),1) <> ';' Then
          vRcpts := vRcpts || ';';
       End If;
       Loop
          vRcpt  := Trim(Substr(vRcpts,1,InStr(vRcpts,';') - 1));
          vRcpts := Trim(Substr(vRcpts  ,InStr(vRcpts,';') + 1));
          vReply := Utl_Smtp.Rcpt( vConn,vRcpt,Null );
          If vRcpts Is Null Then
             Exit;
          End If;
       End Loop;
       vReply := Utl_Smtp.Data(vConn,'Subject: ' || pSubject || chr(13) || chr(10) || pBody);
       Utl_Smtp.Quit(vConn);
    End;Hope this helps
    Christian Balz

  • Send Mail to Contact Group

    I'm helping someone set up their new iPad.  One of the most common tasks she hopes to use it for is sending e-mail to several groups.  On her iMac she has the groups set up in Contacts.  To send a message to them she opens mail, selects the group, and all the addresses populate the To: field.
    The groups are there in iOS on the iPad too, but there doesn't seem to be any way to select them when you're sending a mail message.  If you go to the + button, click Groups (little back arrow), and click the group it just adds or removes the little check mark by the group name.  There's no way to chose the group to say "send to all these people."
    You CAN select just the specific group, go to the contact list, and tap each name one at a time to populate the To: field, but with 50+ e-mail addresses...  That's NOT a solution, that's a pain.
    There must be some way in iOS to send mail messages to everyone in a contact group... BUT HOW?
    Thanks!

    Um, Apple... REALLY?  Why on earth would they leave out something this fundamental?
    Please tell me they added it in iOS 7.  This is one of the primary reasons my friend bought the iPad.  She's considering ditching it because it can't do something as fundamental as send to a group.
    Thanks!

  • How do I send mail to multiple groups simultaneously in Mavericks?

    Recently updated to Mavericks, and I used to be able to just add a contact group to the BCC field.  I can still add a contact group to the BCC field, as I have searched up till this point, but my issue is adding multiple groups into these fields.  I thought a solution might be to make one huge group, but I use different groups for different things constantly, and combine and alter them continuously.
    How on earth is it that I can no longer email multiple groups simultaneously?  There has to be something I'm missing!!

    I did this and the recipient gets an email From: Me and To: Me.
    If you are sending email to multiple recipients by bcc, the recipients should not be able to see the other recipients you are sending the mail to.  However, they will see who the email is from which is you!
    What is confusing me is how are your recipients getting your email if it's not addressed to them but to yourself?
    Have you tried rebuilding Mail?
    Which version of OS & Mail are you using?  Asking because your system profile is showing you are using Snow Leopard yet, you posted in the ML forum.

  • Trouble sending mail to a group

    HI, I set up a new group in my mail program and when I try to send it an error comes up saying "This message could not be delivered and will remain in your Outbox until it can be delivered.
    Sending the message content to the server failed."
    I believe I have all my settings correct since I can send and receive mail to individuals with no problem. Does anyone know why I can't send an email to a group?
    Thanks

    Beth,
    Welcome to the Discussions.
    Are you using 10.3.9, as your system info reports?
    A likely explanation is that you have one or more addresses, where there is a comma that is delimiting an address in a problematic way. This would most often happen, if in front of the email address, the names were listed as Howell, Beth<[email protected]>. In such a case "Howell" is stranded by the comma, and first treated as an address, all by itself, but then this is recognized as an invalid form, and sending it not attempted with any of the other, valid, addresses. Proof your list, very carefully, for this.
    This is different from a failure due to obsolete address.
    However, other problems can arise from the number of recipients, and the SMTP you are using. How many recipients are in the group?
    Ernie

  • Problem sending mail to large group

    I've got problems sending an email to a large group of recepients using 'Mail'. The mailserver rejects the mail. How should it be done to send a mail to a large group?

    What are you calling a "large group"? Servers usually have limits on the number of emails you can send per hour. Try sending to just a small group of 50 or 100 addresses and see if that works.

  • How to send mail to a group?

    Hi,
    I might have just missed something, but how to I sent email to a group that is in my address book? When I type the group name, the To field doesn't auto fill like my Mac. When I click the + button, I can view the group and add the people one by one to the To field, but that not sustainable.
    Your help is appreciated. Hopefully this is an easy one to answer.
    Thanks,
    Allan

    The mail app doesn't handle groups. My work around is to use the iPhone app called Pastebot. I set up my groups as a string of email addresses separated by a semicolon in Pastebot. Then when I want to send an email to a particular group, I open Pastebot first, select my group (which loads the string into the clipboard), then go to the Mail app and hold my finger down in the TO field. When the "Paste" pop up opens, I just click on it. Works fine as long as you don't have too many groups.

  • Unable to send mails to my group

    Hi,
    i have created BeehiveOnline Group 'collections_consumers' and i got a mail as below:
    The following group was created by [email protected]
    Group Name : collections_consumers
    Description : DL for all the collections consumers.
    Group Manager:  [email protected],[email protected]
    The group will be validated and, if approved (normally within 24 hours) the group will be set up.
    It is YOUR responsibility to check the group membership page in the Group Creation tool after 10-15 minutes to check
    the member Status. This will be in one of three States: Verified, Pending or Awaiting Fix.
    I have verified all my members and all are verified successfully.

    Sireesha,
    That is because the email address is not  [email protected] but  [email protected] if the group is on BeehiveOnline
    Phil

  • How to send a mail attachment to group of users

    i have a group ZTACT TEAM Which has 10 mail ID's. I want to send mails to those 10 ID's in ZTACT team . Plz tell me ..How to do this to send mails to the group.....
    Thanks
    Srinivas

    Hi ,
    try this code.
      DATA: LD_ERROR    TYPE SY-SUBRC,
             LD_RECIEVER TYPE SY-SUBRC,
             LD_MTITLE LIKE SODOCCHGI1-OBJ_DESCR,
             LD_EMAIL LIKE  SOMLRECI1-RECEIVER,
             LD_FORMAT TYPE  SO_OBJ_TP ,
             LD_ATTDESCRIPTION TYPE  SO_OBJ_NAM ,
             LD_ATTFILENAME TYPE  SO_OBJ_DES ,
             LD_SENDER_ADDRESS LIKE  SOEXTRECI1-RECEIVER,
             LD_SENDER_ADDRESS_TYPE LIKE  SOEXTRECI1-ADR_TYP,
             LD_RECEIVER LIKE  SY-SUBRC.
      DATA:   T_PACKING_LIST LIKE SOPCKLSTI1 OCCURS 0 WITH HEADER LINE,
              T_CONTENTS LIKE SOLISTI1 OCCURS 0 WITH HEADER LINE,
              T_RECEIVERS LIKE SOMLRECI1 OCCURS 0 WITH HEADER LINE,
              T_ATTACHMENT LIKE SOLISTI1 OCCURS 0 WITH HEADER LINE,
              T_OBJECT_HEADER LIKE SOLISTI1 OCCURS 0 WITH HEADER LINE,
              W_CNT TYPE I,
              W_SENT_ALL(1) TYPE C,
              W_DOC_DATA LIKE SODOCCHGI1.
      LD_EMAIL   = P_EMAIL.
      LD_MTITLE = P_MTITLE.
      LD_FORMAT              = P_FORMAT.
      LD_ATTDESCRIPTION      = P_ATTDESCRIPTION.
      LD_ATTFILENAME         = P_FILENAME.
      LD_SENDER_ADDRESS      = P_SENDER.
      LD_SENDER_ADDRESS_TYPE = P_SENDER_ADDRES_TYPE.
    * Fill the document data.
      W_DOC_DATA-DOC_SIZE = 1.
    * Populate the subject/generic message attributes
      W_DOC_DATA-OBJ_LANGU = SY-LANGU.
      W_DOC_DATA-OBJ_NAME  = 'SAPRPT'.
      W_DOC_DATA-OBJ_DESCR = LD_MTITLE .
      W_DOC_DATA-SENSITIVTY = 'F'.
    * Fill the document data and get size of attachment
      CLEAR W_DOC_DATA.
      READ TABLE IT_ATTACH INDEX W_CNT.
      W_DOC_DATA-DOC_SIZE =
         ( W_CNT - 1 ) * 255 + STRLEN( IT_ATTACH ).
      W_DOC_DATA-OBJ_LANGU  = SY-LANGU.
      W_DOC_DATA-OBJ_NAME   = 'SAPRPT'.
      W_DOC_DATA-OBJ_DESCR  = LD_MTITLE.
      W_DOC_DATA-SENSITIVTY = 'F'.
      CLEAR T_ATTACHMENT.
      REFRESH T_ATTACHMENT.
      T_ATTACHMENT[] = IT_ATTACH[].
    * Describe the body of the message
      CLEAR T_PACKING_LIST.
      REFRESH T_PACKING_LIST.
      T_PACKING_LIST-TRANSF_BIN = SPACE.
      T_PACKING_LIST-HEAD_START = 1.
      T_PACKING_LIST-HEAD_NUM = 0.
      T_PACKING_LIST-BODY_START = 1.
      DESCRIBE TABLE IT_MESSAGE LINES T_PACKING_LIST-BODY_NUM.
      T_PACKING_LIST-DOC_TYPE = 'RAW'.
      APPEND T_PACKING_LIST.
    * Create attachment notification
      T_PACKING_LIST-TRANSF_BIN = 'X'.
      T_PACKING_LIST-HEAD_START = 1.
      T_PACKING_LIST-HEAD_NUM   = 1.
      T_PACKING_LIST-BODY_START = 1.
      DESCRIBE TABLE T_ATTACHMENT LINES T_PACKING_LIST-BODY_NUM.
      T_PACKING_LIST-DOC_TYPE   =  LD_FORMAT.
      T_PACKING_LIST-OBJ_DESCR  =  LD_ATTDESCRIPTION.
      T_PACKING_LIST-OBJ_NAME   =  LD_ATTFILENAME.
      T_PACKING_LIST-DOC_SIZE   =  T_PACKING_LIST-BODY_NUM * 255.
      APPEND T_PACKING_LIST.
    * Add the recipients email address
      CLEAR T_RECEIVERS.
      REFRESH T_RECEIVERS.
      LOOP AT TB_EMAIL.
        T_RECEIVERS-RECEIVER = TB_EMAIL-EMAIL.
        T_RECEIVERS-REC_TYPE = 'U'.
        T_RECEIVERS-COM_TYPE = 'INT'.
        T_RECEIVERS-NOTIF_DEL = 'X'.
        T_RECEIVERS-NOTIF_NDEL = 'X'.
        APPEND T_RECEIVERS.
      ENDLOOP.
      W_SENT_ALL = 'X'.
      CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
           EXPORTING
                DOCUMENT_DATA              = W_DOC_DATA
                PUT_IN_OUTBOX              = 'X'
    *            SENDER_ADDRESS             = LD_SENDER_ADDRESS
    *            SENDER_ADDRESS_TYPE        = LD_SENDER_ADDRESS_TYPE
                COMMIT_WORK                = 'X'
           IMPORTING
                SENT_TO_ALL                = W_SENT_ALL
           TABLES
                PACKING_LIST               = T_PACKING_LIST
                OBJECT_HEADER              = OBJHEAD
                CONTENTS_BIN               = T_ATTACHMENT
                CONTENTS_TXT               = IT_MESSAGE
                RECEIVERS                  = T_RECEIVERS
           EXCEPTIONS
                TOO_MANY_RECEIVERS         = 1
                DOCUMENT_NOT_SENT          = 2
                OPERATION_NO_AUTHORIZATION = 4
                OTHERS                     = 99.
      SUBMIT RSCONN01 WITH MODE = 'INT' AND RETURN.
      IF SY-SUBRC NE 0.
        MESSAGE E000 WITH 'Error occurred while sending'.
      ELSE.
        MESSAGE I000 WITH 'The document was sent'.
      ENDIF.
    Notice that i am looping at T_receivers to get multiple number of receivers.
    Hope this helps you.
    Vikki.

  • Sending mail to group... help!

    I have created a group in Address Book... Some of the people have two or more email addresses in Address Book (home, school, work, etc.). When I go into Mail, and put the group in the "To:" box, all the names appear... but Mail automatically defaults to the first email listed for each person. Instead of selecting each person in the message, can I easily set the "primary" email address for each person. For example, I want my particular group to only send to the ".edu" addresses of each person. How can I easily change this?
    Thanks for the help!

    Copied from Address Book Help.
    If group members have multiple email addresses, you can choose which address you want to use when sending mail to the group. Select the group, then choose Edit > Edit Distribution List. Select which of the members' addresses you want to use.

  • Send mail to group

    Having trouble in Snow sending mail to a group. No trouble before Snow, now groups won't go into "to" with new mail when clicked. Any ideas?

    Thanks for your help with this. Doing as you suggested didn't work any differently, however it seems there isn't really a problem after all.
    I'm a little embarrassed and sorry to have wasted your time.
    I have created a dummy test group and done some experiments. It seems Apple have changed the way it works: Where before I could see a list of email addresses and edit them if I needed to, all I now see is the name of the group and there is no way to edit them.
    So forgive me for thinking it wouldn't work, but I couldn't experiment with the real group and risk it going wrong!
    I have no idea how they (Apple) are doing it but the names of the recipients don't seem to appear anywhere in the email header...
    +Mime-Version: 1.0 (Apple Message framework v753.1)+
    +To: Test Group+
    +Message-Id: <[email protected]>+
    +Content-Type: multipart/signed;+
    + micalg=sha1;+
    + boundary=Apple-Mail-25-164972374;+
    + protocol="application/pkcs7-signature"+
    +From: "xxxxxx.co.uk" <[email protected]>+
    +Subject: test email to group 002+
    +Date: Sat, 19 Sep 2009 09:47:58 0800
    This is annoying because I cannot now tailor the list of recipients. In the past I used to be able to weed out people who were legitimately in the group but shouldn't receive the email.
    Not sure If they have done this for some oddly perceived security reason: I used to Apple-X the addresses from the To: field, and Apple-V them into the Bcc: field, so recipients couldn't see the whole list of addresses or "Reply to all". Surely this was enough for the vast majority of users.
    Thanks again for your time.

  • Tasks List not sending email to AD Groups

    Hello,
    I have several Tasks list on my farm and all behave the same way.
    If I assign a task to an individual person, he will receive email notification.
    However, if I assign it to an Active Directory group, no email notification is sent. The AD group does have an email address associated. Using outlook it can receive email.
    The email notifications for the task list is enabled. If I check the SharePoint logs, I don't see that SharePoint is trying to send mail to the group's email. If I assign the task to a single person, he receives the mail and I can see in the logs that the
    email has been sent.
    I have no idea where to continue the troubleshooting.

    Try the following:
    Verify that the group is an AD security group (not a distribution list) and double check that the AD group is email-enabled.
    Check that the group had been explicitly added to a SharePoint group (such as the default Viewers group) with at least read permission to the site and to the list on which the alert was created.
    Verify that the SharePoint group’s membership is set to “Visible to everyone.” (People and Groups>SharePointGroupInQuestion > Settings>Group Settings)
    Would also be worth checking on the Exchange server to see if there any 'Message Delivery Restrictions' for the AD group:
    Exchange Management Console -> Recipient Configuration
    Right-click on the problem group and choose properties.
    On the Mail Flow Settings tab, double click on Message Delivery Restrictions.
    Uncheck the check box “Require that all senders are authenticated”

Maybe you are looking for