This topic describes how to use the Java sample code to send messages.

public class ProducerDemo {

    public static void main(String[] args) {
        CloudAccount account = new CloudAccount("YourAccessId", "YourAccessKey", "MNSEndpoint");

        //Initialize the client. The client is initialized only once.
        MNSClient client = account.getMNSClient(); 

        //Send 10 messages (send one message at a time).
        try{
            //Specify a queue that you have created for the TestQueue parameter.
            CloudQueue queue = client.getQueueRef("TestQueue");
            for (int i = 0; i < 10; i++)
            {
                Message message = new Message();
                message.setMessageBody("I am test message " + i);
                message.setPriority(8);
                Message putMsg = queue.putMessage(message);
                System.out.println("Send message id is: " + putMsg.getMessageId());
            }
        } catch (ClientException ce)
        {
            System.out.println("Something wrong with the network connection between client and MNS service."
                    + "Please check your network and DNS availablity.");
            ce.printStackTrace();
        } catch (ServiceException se)
        {
            se.printStackTrace();
            logger.error("MNS exception requestId:" + se.getRequestId(), se);
            if (se.getErrorCode() ! = null) {
                if (se.getErrorCode().equals("QueueNotExist"))
                {
                    System.out.println("Queue is not exist.Please create before use");
                } else if (se.getErrorCode().equals("TimeExpired"))
                {
                    System.out.println("The request is time expired. Please check your local machine timeclock");
                }
            /*
            you can get more MNS service error code from following link:
            https://www.alibabacloud.com/help/doc-detail/27501.htm
            */
            }
        } catch (Exception e)
        {
            System.out.println("Unknown exception happened!") ;
            e.printStackTrace();
        }

        client.close();
    }

}