10-12-2010 12:00 PM
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.
10-12-2010 10:32 PM
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;
}
}
10-13-2010 03:22 AM
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....
10-13-2010 04:26 AM
you have imported the wrong Session class.
this is the correct one:
http://www.blackberry.com/developers/docs/5.0.0api
10-13-2010 04:49 AM
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...
10-13-2010 05:31 AM
Hi friends,
Can any send me code snippet for sending an email....
10-13-2010 05:44 AM
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);
10-13-2010 08:53 AM
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);
}
}
}
10-13-2010 08:57 AM
well, if you invoke the mail application instead of sending the message...
10-13-2010 09:06 AM
I tried without using that invoke statement also..but it wont work