java mail to cc someone

Hi Experts,
I am using the following code to successfully send mail to one person but I want to be able to send to a few different people and also to CC someone.
What modifications do i need to make?

Regards,
Raftor.

**********************************************************************

package classfiles;

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import java.net.*;


public class Mail
{
      String from = "";
      String to = "";
      String subject = "";
      String body = "";

        public Mail( String to, String from, String subject, String body)
        {

            this.to = to;
            this.from = from;
            this.subject = subject;
            this.body = body;
      }

        public void sendMail()
        {
            boolean successful = false;
            try
            {

                  Properties props = System.getProperties();
                  props.put("mail.smtp.host", "irmail2.gtk.gtech.com");
                  Session session = Session.getDefaultInstance(props, null);
                  MimeMessage message = new MimeMessage(session);
                  message.setFrom(new InternetAddress(from));
                  message.addRecipient(Message.RecipientType.TO,
                  new InternetAddress(to));
                  message.setSubject(subject);
                  message.setContent(body, "text/html");
                  Transport.send(message);
                  successful = true;
            }
            catch(Exception e)
            {
                   System.out.println("Mail " +e.toString());
                   successful = false;
            }

        }

      public String getServerName()
      {
       try
       {
              return InetAddress.getLocalHost().getHostAddress();
       }
        catch (Exception e) {return "unknown";}
    }

}
RaftorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mayank SAssociate Director - Product EngineeringCommented:
>> message.addRecipient(Message.RecipientType.TO,
               new InternetAddress(to));

Add this after it:

message.addRecipient ( Message.RecipientType.CC, new InternetAddress ( cc ) ) ;

>> public Mail( String to, String from, String subject, String body)

Make that public Mail ( String to, String cc, String from, String subject, String body )

so that you pass a CC address to it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mayank SAssociate Director - Product EngineeringCommented:
>> I want to be able to send to a few different people

You might be better off by sending an array of TO and CC in that case, like:

public Mail ( String[] to, String[] cc, String from, String subject, String body )

Make your data-members:

>> String to = "";

as:

String[] to ;
String[] cc ;

Loop through the array while adding to-address:

for ( int i = 0 ; i < to.length ; i ++ )
  message.addRecipient ( Message.RecipientType.TO, new InternetAddress ( to[i] ) ) ;

Same way, do it for CC....
rama_krishna580Commented:
Hi,

   public boolean sendMail( Vector addressTo, String addressFrom, String msgSub,

                             String msgBody, String ehData )

    {

        if ( debug )

        {

            String logStr = "\n addressTo is " + addressTo ;

            logStr += "\n addressFrom is " + addressFrom;

            logStr += "\n ShallSend is " + shallSend;

            logStr += "\n debug is " + debug;

            log.debug( logStr );

            //return true;

        }

        if ( !shallSend )

        {

            log.debug(

                    "Mailer called. ShallSend value is false. Not sending email...." );

            return false;

        }

        if ( host == null )

        {

            log.debug( "Host is null.." );

            return false;

        }

        // create some properties and get the default Session

        Properties props = new Properties();

        props.put( "mail.smtp.host", host );

        Session session = Session.getDefaultInstance( props, null );

        session.setDebug( debug );

        try

        {

            // create a message

            String unixHostName = System.getProperty( "hostname" );
            String serverName = System.getProperty( "serverName" );

            if( msgBody != null && msgBody.trim().length() > 0 )

            {

                if( serverName != null && serverName.trim().length() > 0 )
                    msgBody = "ServerName : " + serverName + "\n\n" + msgBody;

                if( unixHostName != null && unixHostName.trim().length() > 0 )
                    msgBody = "  HostName : " + unixHostName + "\n\n" + msgBody;

            }

            else

            {

                msgBody = "";

                if( serverName != null && serverName.trim().length() > 0 )
                    msgBody = "ServerName : " + serverName + "\n\n" + msgBody;
               
                if( unixHostName != null && unixHostName.trim().length() > 0 )
                    msgBody = "  HostName : " + unixHostName + "\n\n" + msgBody;

            }

            MimeMessage msg = new MimeMessage( session );
            msg.setFrom( new InternetAddress( addressFrom ) );

            InternetAddress[] address = new InternetAddress[addressTo.size()];
            for ( int i = 0; i < addressTo.size(); i++ )
            {
                address[i] = new InternetAddress( ( String ) addressTo.elementAt( i ) );
            }

            msg.setRecipients( Message.RecipientType.TO, address );
            msg.setSubject( msgSub );
            msg.setSentDate( new Date() );

            // create and fill the first message part
            MimeBodyPart mbp1 = new MimeBodyPart();
            mbp1.setText( msgBody, "us-ascii" );

            // create the Multipart and its parts to it
            Multipart mp = new MimeMultipart();
            mp.addBodyPart( mbp1 );

            // add the Multipart to the message
            msg.setContent( mp );

            // send the message
            Transport.send( msg );
        }
        catch ( MessagingException mex )
        {
            log.debug( "Send mail failed ..." );

            mex.printStackTrace();

            Exception ex = null;

            if ( ( ex = mex.getNextException() ) != null )

            {

                ex.printStackTrace();

            }

            return false;

        }

        catch ( Throwable t )

        {

            log.debug( "Send mail failed ..." + t.toString() );

            return false;

        }

        return true;

    }


R.K
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Mayank SAssociate Director - Product EngineeringCommented:
Actually a Vector should not be used until synchronization is required. Prefer ArrayList or simply an array.
RaftorAuthor Commented:
I have hard coded my 'to' string to be something like this: person1@mail.com, person2@mail.com, person3@mail.com and it just does not send the mail. I have also tried this format :person1@mail.com;person2@mail.com;person3@mail.com and it does not send the mail.

This is similar to what mayankeagle is saying in your first comment is it not.
What am i doing wrong?
Mayank SAssociate Director - Product EngineeringCommented:
No, you need to make it an array like I have shown, or a collection.
RaftorAuthor Commented:
ok, ill give that a go and ill get back to you...
RaftorAuthor Commented:
mayankeagle  in your second example will that look to the user like i sent him a mail and i cc'd it to a few others? that is what i want.

Regards,
Raftor.
Mayank SAssociate Director - Product EngineeringCommented:
Yes.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.