Thursday, January 16, 2014

Send Emails Throw Vfpages

===========VFPage=============

<apex:page controller="mailcon">
  <style>
.right
{
position:absolute;
right:10px;
width:300px;
background-color:#b0e0e6;

}
.vijay{
background-color:lightblue;
font-size:20px;
color: #FF7C00;
content: "\f00c";
font-family: FontAwesome;
text-decoration: none;
}

</style>
 <apex:form >
 <apex:messages styleClass="vijay"/>
  <apex:pageBlock title="Send mails to user" id="pb">
   <apex:pageBlockSection columns="3">
    <apex:commandButton value="Send Mail" action="{!snd}" rendered="{!sndcon}"/>
    <apex:commandButton value="back" action="{!bck}"/>
    <apex:commandButton value="Send Mass Mail" action="{!sndmail}"/>
   
   </apex:pageBlockSection>
  </apex:pageBlock>
  <apex:pageBlock rendered="{!pbcon}" >
   <apex:pageblockSection >
   <apex:outputText rendered="{!Con}" style="color:blue"> Message Has been sent successfully</apex:outputText><br/>
    <apex:inputText value="{!name}" label="Email Single Address" size="100" style="color:green"/><br/>
   
    <apex:inputTextarea value="{!body}" label="Body"/>
   
      <div class="right">
  <apex:inputFile value="{!attach.body}" filename="{!attach.name}" contentType="appicationtype/ms.word"/><br/>
      </div>
    <apex:commandButton value="Send" action="{!snd1}"/>
   </apex:pageblockSection>
  </apex:pageBlock>
 </apex:form>
</apex:page>

===========Controller===============
public with sharing class mailcon {
   public Attachment attach {get;set;}
    public PageReference bck() {
        return page.mailTomultipulUsers;
        //return null;
    }


    public boolean pbcon { get; set; }

    public boolean sndcon { get; set; }
public mailcon (){
pbcon=false;
sndcon=true;
con=false;}

    public boolean Con { get; set; }

    public PageReference sndmail() {
   
        return null;
    }


    public String body { get; set; }

    public String name { get; set; }
   
 
 

    public  void snd1() {
   
    integer count=0;
        String str=name + ',';
        List<String> li=new List<String>();
        while(str.contains(',')){
       
          count++;
          String[] str1=str.split(',',2);
          system.debug('............'+str1);
              li.add(str1[0]);
          str=str1[1];

         
      }
     
     
       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

        String[] toAddresses = new String[] {};
           for(integer i=0;i<count;i++){
             toAddresses.add(li[i]);

        }
        String myString = 'StringToBlob';
Blob b = Blob.valueof(myString);
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('attachment.pdf');
efa.setBody(b);


        mail.setToAddresses(toAddresses);
        mail.setSubject('Email from VF page');
        mail.setPlainTextBody(body);
        mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
       
        try{
       Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        con=true;
    }
    catch (Exception e){
    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Sorry... Enter Proper Email Id'));
    }
      name=null;
      body=null;
   
   
     
       
    }


    public PageReference snd() {
     pbcon =true;
   // sndcon=false;
        return null;
    }

}

No comments:

Post a Comment