• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

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";}
    }

}
0
Raftor
Asked:
Raftor
  • 5
  • 3
2 Solutions
 
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.

0
 
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....
0
 
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
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Mayank SAssociate Director - Product EngineeringCommented:
Actually a Vector should not be used until synchronization is required. Prefer ArrayList or simply an array.
0
 
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?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
No, you need to make it an array like I have shown, or a collection.
0
 
RaftorAuthor Commented:
ok, ill give that a go and ill get back to you...
0
 
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.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Yes.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now