I have a contact information section on a VF page. In this section, 'Email' is a field, and in another section I have a custom template. When I submit the page(via a Save button) I need to get the input of the 'Email' field and send an email with the custom template as the body.

Here is my code:

public void sendEmail(){ 
    Messaging.singleEmailMessage email1 = new Messaging.singleEmailMessage(); 
    String userEmail = new String(); 
    userEmail = Billing.Billingemail; 
    Messaging.sendEmailResult res = 
        Messaging.sendEmail(new Messaging.singleEmailMessage[] {email1}); 

Getting error

Compile Error: Type cannot be constructed: String at line 771 column 27

Any ideas?

Note: I get the custom template through xml from another object.

  • The error is because you have: String userEmail=new String(); You don't construct strings in that way, just instantiate it and you can assign to it: String userEmail=Billing.Billingemail; Oct 30, 2012 at 14:37
  • Method does not exist or incorrect signature: [Messaging.SingleEmailMessage].setToAddresses(String) email1.setToAddresses(userEmail);
    – Eagerin Sf
    Oct 30, 2012 at 14:59
  • settoaddresses takes an array of strings - e.g. email1.setToAddresses(new String[]{userEmail}); Oct 30, 2012 at 15:01
  • Can u give me an example Bob...I'm fed up with this....My requirement is I need to pass value from Billing.billingemail into settoaddress...
    – Eagerin Sf
    Oct 30, 2012 at 15:14
  • 1
    You can reduce it down to: email1.setToAddresses(new String[]{Billing.billingEmail}); Oct 30, 2012 at 15:19

1 Answer 1


You can't construct a string in that way - you can simply declare it and assign a value:

String userEmail=Billing.Billingemail;

however, you can reduce this further and simply pass the Billing.BillingEmail to the setToAddresses method of SingleEmailMessage as long as you wrap it up in an array:

email1.setToAddresses(new String[]{Billing.billingEmail});

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .