在java mail api程序中添加邮件的reciepeints

时间:2015-05-17 04:26:00

标签: java javamail

我已经在名为A的类中创建了该方法,如下所示,该功能是使用java mail api发送邮件,现在bellow方法接受参数现在我的查询是我应该如何在数组中添加收件人作为收件人必须是通过的数组类型请告知

class A 
{
public boolean postMail(String recipients[], String subject, String message, String from) throws Exception {
        boolean debug = true;
        boolean result = false;

        // create a message
        try {
            // set the from and to address
            InternetAddress addressFrom = new InternetAddress(from);
            this.mimeMessage.setFrom(addressFrom);
            InternetAddress[] addressTo = new InternetAddress[recipients.length];
            for (int index = 0; index < recipients.length; index++) {
                addressTo[index] = new InternetAddress(recipients[index]);
            }
            this.mimeMessage.setRecipients(Message.RecipientType.BCC, addressTo);
            this.mimeMessage.addHeader("MyHeaderName", "myHeaderValue");

            // Setting the Subject and Content Type
            this.mimeMessage.setSubject(subject);
            this.mimeMessage.setContent(message, "text/html");
            Transport.send(this.mimeMessage);
            result = true;
        } catch (MessagingException messagingException) {
            result = false;
            messagingException.printStackTrace();
            // throw new CASSException(messagingException);
        }
        return result;

    }
    }

now my query is that i am trying to invoke this method from within the class shown below '


    class A 
    {


    String Subject  = "testmailsubject";
    String message = "simple test message";
    String from ="test@abc.com";


    A a1 = new A ();
    a1.postMail



    //***********method *************
    public boolean postMail(String recipients[], String subject, String message, String from) throws Exception {
        boolean debug = true;
        boolean result = false;

        // create a message
        try {
            // set the from and to address
            InternetAddress addressFrom = new InternetAddress(from);
            this.mimeMessage.setFrom(addressFrom);
            InternetAddress[] addressTo = new InternetAddress[recipients.length];
            for (int index = 0; index < recipients.length; index++) {
                addressTo[index] = new InternetAddress(recipients[index]);
            }
            this.mimeMessage.setRecipients(Message.RecipientType.BCC, addressTo);
            this.mimeMessage.addHeader("MyHeaderName", "myHeaderValue");

            // Setting the Subject and Content Type
            this.mimeMessage.setSubject(subject);
            this.mimeMessage.setContent(message, "text/html");
            Transport.send(this.mimeMessage);
            result = true;
        } catch (MessagingException messagingException) {
            result = false;
            messagingException.printStackTrace();
            // throw new CASSException(messagingException);
        }
        return result;

    }


    }

1 个答案:

答案 0 :(得分:0)

添加像这样的Receipeints

Address[] TO = new Address[] {InternetAddress.parse("karthik@cherukuri.com"),
                                   InternetAddress.parse("karthik2@cherukuri.com"), 
                                   InternetAddress.parse("karthik3@cherukuri.com")}; 
message.addRecipients(Message.RecipientType.TO, TO);

否则,如果您想直接将收件人数组发送到方法,只需初始化您想要的电子邮件列表。

String Subject  = "testmailsubject";
String message = "simple test message";
String from ="test@abc.com";
String[] recipient = new String[]{"karthik@cherukuri.com","karthik2@cherukuri.com", "karthik3@cherukuri.com"};
a1.postmail(recipient,subject,from,message);