Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
abhay_401
Posts: 185
Registered: ‎09-02-2010
My Device: Not Specified

How to mail...

Hi friends,

 

                 How can we send a mail through program....if there is any articles/PDFs regarding this please post..it'l be very helpful to me.

Developer
StevenKader
Posts: 644
Registered: ‎02-03-2010
My Device: Z10 LE

Re: How to mail...

You can try this class :

 

public class SendEmail {
    public SendEmail() {
    }


    public boolean sendEmail(String emailAddress, String contents) {
     
        boolean retVal = true;
        try {
            Session session = Session.getDefaultInstance();
            Store store = null;
            Transport tp = null;

            if (session != null) {
                store = session.getStore();
                tp = session.getTransport();
            }

            if ((store != null) && (tp != null)) {
                Message email = new Message();
                if (emailAddress != null && !emailAddress.equals("")) {
                    // System.out.println("emailAddress===>"+emailAddress);
                    email.addRecipient(Message.RecipientType.TO, new Address(
                            emailAddress, emailAddress));
                }

        

                email.setSubject("Subject Here");
                email.setContent("Email Contents Here");
                tp.send(email);
            
            } else {
                retVal = false;
             
            }

        } catch (Exception e) {
            retVal = false;
          
        }

        return retVal;
    }
}


Steven Kader at JaredCo
   Follow me on Twitter    |     Mipixi.com :  Web App for Photo Sharing
Developer
abhay_401
Posts: 185
Registered: ‎09-02-2010
My Device: Not Specified

Re: How to mail...

Thanks for ur reply JaredCo ,

 

while im using this class it's showing error like dis,

The method getDefaultInstance() is undefined for the type Session....

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: How to mail...

you have imported the wrong Session class.

this is the correct one:

http://www.blackberry.com/developers/docs/5.0.0api/net/rim/blackberry/api/mail/Session.html

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
abhay_401
Posts: 185
Registered: ‎09-02-2010
My Device: Not Specified

Re: How to mail...

Hi simon,

I used the code like this...but it wont send email to dat particular mail address...

import net.rim.blackberry.api.mail.Address;
import net.rim.blackberry.api.mail.Message;
import net.rim.blackberry.api.mail.Session;
import net.rim.blackberry.api.mail.Store;
import net.rim.blackberry.api.mail.Transport;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.MainScreen;

public class SendingMail extends UiApplication{
	public static void main(String args[])
	{
	SendingMail sm=new SendingMail();
	sm.enterEventDispatcher();
		
	}
public SendingMail() {
   pushScreen(new SendEmail()) ;

}
}


class SendEmail extends MainScreen{
    public SendEmail() {
    
    	add(new RichTextField("Hello...."));
    	sendEmail("abhilashbaddam@gmail.com","hello");
    	
    }


    public boolean sendEmail(String emailAddress, String contents) {
      
        boolean retVal = true;
        try {
            Session session = Session.getDefaultInstance();
            Store store = null;
            Transport tp = null;

            if (session != null) {
                store = session.getStore();
                tp = session.getTransport();
            }

            if ((store != null) && (tp != null)) {
                Message email = new Message();
                if (emailAddress != null && !emailAddress.equals("")) {
                    // System.out.println("emailAddress===>"+emailAddress);
                    email.addRecipient(Message.RecipientType.TO, new Address(
                            emailAddress, emailAddress));
                }

         

                email.setSubject("Subject Here");
                email.setContent("Email Contents Here");
                tp.send(email);
             
            } else {
                retVal = false;
              
            }

        } catch (Exception e) {
            retVal = false;
           
        }

        return retVal;
    }
}

 Please help me...

 

 

Developer
abhay_401
Posts: 185
Registered: ‎09-02-2010
My Device: Not Specified

Re: How to mail...

Hi friends,

 

Can any send me code snippet for sending an email....

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: How to mail...

i am pretty sure you could copy it from the developer guides or the samples.

 

 

// Create a Message
Store store = Session.getDefaultInstance().getStore();
Folder[] folders = store.list(Folder.SENT);
Folder sentfolder = folders[0];
Message msg = new Message(sentfolder);
// Specify the message contents
Address toAddress = new Address(mailAddress, name);            
msg.addRecipient(Message.RecipientType.TO, toAddress);
msg.setSubject(subject);                
msg.setContent(messageText);   
// Send the message
Transport.send(msg);

 

 

 

 

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
abhay_401
Posts: 185
Registered: ‎09-02-2010
My Device: Not Specified

Re: How to mail...

Hi simon,

 

I did like this,but mail doesn't sent to that mail id ...what may b d pblm..

public class mailsending extends UiApplication{
	
	public static void main(String args[])
	{
		mailsending ms=new mailsending();
		ms.enterEventDispatcher();
		
	}
	public mailsending() {
		//pushScreen(new testScreen());
		new testScreen();
	}
}
class testScreen extends MainScreen
{
	public testScreen() {
	
		
		Store store = Session.getDefaultInstance().getStore();

		
		Folder[] folders = store.list(Folder.SENT);
		Folder sentfolder = folders[0];

		
		Message msg = new Message(sentfolder);
		Address recipients[] = new Address[1];

		try {
			
		    recipients[0]= new Address("abhilashbaddam@gmail.com","abhi" );

		    
		     msg.addRecipients(Message.RecipientType.TO, recipients);

		     msg.setSubject("Test email1");

		    
		     msg.setContent("This is a test email from my BlackBerry Wireless Handheld");

		     
		     msg.setPriority(Message.Priority.HIGH);
		     Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES, new MessageArguments(msg));
		     
		     Transport.send(msg);
		 }
		catch (Exception me) {
		     System.err.println(me);
		}
		
		}
	}

 

 

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: How to mail...

well, if you invoke the mail application instead of sending the message...

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
abhay_401
Posts: 185
Registered: ‎09-02-2010
My Device: Not Specified

Re: How to mail...

I tried without using that invoke statement also..but it wont work