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
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Programatically detect the Email Configuarations.

HI all,

 

In my app, i need to send an email. i did all the necessary coding and all the stuff, and in simulator it is working fine, when i installed it in my device the app, gets stuck on clicking send email button.

 

later i found that email configuarations are not set on the device.

so i want to show an dialog alert, to the user if the email configuarations are not done.

 

how to detect whether email settings are configured or not in the BB device, programatically?

 

pls provide me the necessary references for this.

Thanks and Regards,
PraveenGoparaju.
Developer
Posts: 17,019
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Programatically detect the Email Configuarations.

Session.getDefaultInstance() returns null if no mail is available.

----------------------------------------------------------
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
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Re: Programatically detect the Email Configuarations.

so if there are no email configurations set, it returns null right??

Thanks and Regards,
PraveenGoparaju.
Developer
Posts: 17,019
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Programatically detect the Email Configuarations.

according to the API, yes

----------------------------------------------------------
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
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Re: Programatically detect the Email Configuarations.

    Session s = Session.getDefaultInstance();

 

Hi simon. i checked in simulator without launching ESS, so i guess it should return null for 's', but it is not returning null, and the email sending block is exceuting...

 

the following is my code:

Session s = Session.getDefaultInstance();
				if(s == null)
				{
					String errMsg = "Unable to send email message.\n";
					Dialog.alert(errMsg);
					bCanSend = false;
					UiApplication.getUiApplication().invokeLater(new Runnable()
	                {
	                    public void run()
	                    {
	                        Dialog.alert(BasicUI._resources.getString(SOSResourceFilesResource.ERR_SENDING_MAIL));
	                    }
	                });
				}
				else
				{
					bCanSend = true;
					
					_emailTo = emailTo;
					_emailBody = emailBody;
					
					emailTransport = Session.waitForDefaultSession().getTransport();
					msgStore = Session.waitForDefaultSession().getStore();
					folderList = msgStore.list(Folder.SENT);
					outFolder = folderList[0];
					msg = new Message(outFolder);
				}

 

 

am i doing anything wrong?

 

 

Thanks and Regards,
PraveenGoparaju.
Developer
Posts: 17,019
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Programatically detect the Email Configuarations.

the simulator has an email address configured. not starting ESS is like running your blackberry in a cave - you cannot send mails but it won't stop it trying.

----------------------------------------------------------
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
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Re: Programatically detect the Email Configuarations.

Hi simon and all,

 

reply is after a long time....!!! I  tested the app, on the device and while debugging i found that...

 

          

Address[] addresses = new Address[1];
addresses[0] = new Address(_emailTo, _emailTo);

msg.addRecipients(Message.RecipientType.TO, addresses);
msg.setSubject(BasicUI._resources.getString(SOSResourceFilesResource.HEADER_TITLE));
msg.setContent(mp);

//email
Transport.send(msg);

 

it is throwing the error... at the line in RED. i found that session object is not null... ( I can assure that no email configurarion are there on my device. When i tried to send email from the device it prompts me to open the Email set up wizard.)

 

 

i found that "msg" is null.. that is the outbox folder is not creating... and the error is "EMail API: No ServiceBook Entry" (something of this sort... not exactly the same).

 

So please tell me, is this the right way to detect the Email Configuarations?? Basically, I need to send an email programatically... from the device...

 

Help!!!

Thanks and Regards,
PraveenGoparaju.
Developer
Posts: 1,055
Registered: ‎01-25-2009
My Device: BlackBerry Bold 9650/BlackBerry Torch 9800

Re: Programatically detect the Email Configuarations.

[ Edited ]
ServiceBook sb = ServiceBook.getSB();
ServiceRecord[] srs = sb.findRecordsByCid("CMIME");

boolean hasEmailConfig = ( srs != null && srs.length > 0 );

 

I guess you can use the above to see if there are any e-mails configured on the device and if hasEmailConfig then try sending.

Jerome Carty - Follow jcarty on Twitter@jcarty | #webworks-dev / #BlackberryDev on irc.freenode.net | My Apps
Click "Accept as Solution" if post solved your original issue.. Give like/thumbs up if you feel post is helpful
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Programatically detect the Email Configuarations.

We use code similar to the following to get the device's default email address - it we get null back, the device has no email set up.  Obviously we don't find many such devices in the wild, but we have few in testing and this works on all and in all OS's. 

 

This code is based on Simon's solution. 

 

    public static String getOurEmailAddress() {
        // Shared return to get this BlackBerry's email address
        String emailAddress;
        try {
            Session ourSession = Session.getDefaultInstance();
            // This returns null if BB does not have a Message Service - which means next
            // instruction will get a null pointer exception.
            emailAddress = ourSession.getServiceConfiguration().getEmailAddress();
        } catch ( Exception e ) {
            emailAddress = null;
        }
        return emailAddress;
    }

 

Developer
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Re: Programatically detect the Email Configuarations.

Hi peter,

 

thanks for your code snippet tooo.... basically when i debug it in device am not receiving 'null' for the statement...

 

 

Session ourSession = Session.getDefaultInstance();

but am there are no eamil configs set for my device. how this can be happen??

 

could you please explain me the reason? anyhow i wll test with your code also.. and i just want to know the reason for my question.

 

expecting a solution...

Thanks and Regards,
PraveenGoparaju.