JavaMail and Android - Little excursus

Tutorials with advanced 'difficulty' and more Lines of Code.

JavaMail and Android - Little excursus

Postby lucabelluccini » Thu Oct 09, 2008 12:13 pm

Can find it Here
lucabelluccini
Freshman
Freshman
 
Posts: 6
Joined: Thu May 22, 2008 3:58 pm
Location: Turin

Top

Postby ninor » Thu Oct 09, 2008 5:30 pm

Thanks! I'll transcript here for easier reference (with links and credit!)

lucabelluccini wrote:Due to the huge number of people interested on using JavaMail API in Android, I'm publishing this "tutorial".

There are some things to keep in mind:
- There are few classes and interfaces to add: DataTransfer and Transferable (stripped down from Apache)
- Modify Sun JavaMail: remove "static files" dependency (some MIME-types are associated by using external files, which cannot be opened directly by Android, due to security policy), adding these associations programmatically; remove some awt.Image dependencies (I did not experienced any issue managing images).

I'm providing a file you can get at http://www.lucabelluccini.com/data/javamail_lbmod.jar containing the last version of JAF and JavaMail (modified), plus missing classes.

I hope licences will be respected. Contact me if some of your works will use this javamail source code or if there are improvements to be applied.

For mail fetching from POP3, IMAP... you can take a look at http://java.sun.com/products/javamail/ (...)
Remember that, in Android, the opening of a POP3 or IMAP session is a huge task. It is necessary to implement some kind of "run in background" or "multithreading".
Some code at http://www.lucabelluccini.com/data/retr ... ippet.java

Contact me in case of need or additional information.


Original post in Google Groups

Thanks a lot, luca! :)
Image AndDev: Your Android Development Community / Tutorials | Here's my Basic ToolKit
User avatar
ninor
Moderator
Moderator
 
Posts: 180
Joined: Thu Aug 14, 2008 6:30 pm
Location: Barcelona, Spain

Postby soniya » Mon Oct 27, 2008 3:31 pm

im using example
http://davanum.wordpress.com/2007/12/22 ... a-gmail-...
with 1.0
and found Following is
the exception.
E/OSNetworkSystem( 448): unknown socket error -1
E/SendMail( 448): Could not connect to SMTP host: smtp.gmail.com,
port: 465
E/SendMail( 448): javax.mail.MessagingException: Could not connect to
SMTP host
: smtp.gmail.com, port: 465;
E/SendMail( 448): nested exception is:
E/SendMail( 448): java.net.SocketException: unknown error
E/SendMail( 448): at
com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransp
ort.java:1391)
E/SendMail( 448): at
com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPT
ransport.java:412)
E/SendMail( 448): at javax.mail.Service.connect(Service.java:
310)
E/SendMail( 448): at javax.mail.Service.connect(Service.java:
169)
E/SendMail( 448): at javax.mail.Service.connect(Service.java:
118)
E/SendMail( 448): at javax.mail.Transport.send0(Transport.java:
188)
E/SendMail( 448): at javax.mail.Transport.send(Transport.java:
118)
E/SendMail( 448): at
org.apache.android.mail.GMailSender.sendMail(GMailSen
der.java:60)
E/SendMail( 448): at org.apache.android.mail.SendMail
$1.onClick(SendMail.j
ava:35)
E/SendMail( 448): at android.view.View.performClick(View.java:
2109)
E/SendMail( 448): at android.view.View.onTouchEvent(View.java:
3523)
E/SendMail( 448): at
android.widget.TextView.onTouchEvent(TextView.java:44
10)
E/SendMail( 448): at
android.view.View.dispatchTouchEvent(View.java:3178)
E/SendMail( 448): at
android.view.ViewGroup.dispatchTouchEvent(ViewGroup.j
ava:857)
E/SendMail( 448): at
android.view.ViewGroup.dispatchTouchEvent(ViewGroup.j
ava:857)
E/SendMail( 448): at
android.view.ViewGroup.dispatchTouchEvent(ViewGroup.j
ava:857)
E/SendMail( 448): at
android.view.ViewGroup.dispatchTouchEvent(ViewGroup.j
ava:857)
E/SendMail( 448): at com.android.internal.policy.impl.PhoneWindow
$DecorVie
w.superDispatchTouchEvent(PhoneWindow.java:1561)
E/SendMail( 448): at
com.android.internal.policy.impl.PhoneWindow.superDis
patchTouchEvent(PhoneWindow.java:1085)
E/SendMail( 448): at
android.app.Activity.dispatchTouchEvent(Activity.java
:1873)
E/SendMail( 448): at com.android.internal.policy.impl.PhoneWindow
$DecorVie
w.dispatchTouchEvent(PhoneWindow.java:1545)
E/SendMail( 448): at
android.view.ViewRoot.handleMessage(ViewRoot.java:114
0)
E/SendMail( 448): at
android.os.Handler.dispatchMessage(Handler.java:88)
E/SendMail( 448): at android.os.Looper.loop(Looper.java:123)
E/SendMail( 448): at
android.app.ActivityThread.main(ActivityThread.java:3
742)
E/SendMail( 448): at
java.lang.reflect.Method.invokeNative(Native Method)
E/SendMail( 448): at java.lang.reflect.Method.invoke(Method.java:
515)
E/SendMail( 448): at com.android.internal.os.ZygoteInit
$MethodAndArgsCalle
r.run(ZygoteInit.java:739)
E/SendMail( 448): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.ja
va:497)
E/SendMail( 448): at dalvik.system.NativeStart.main(Native
Method)
E/SendMail( 448): Caused by: java.net.SocketException: unknown error
E/SendMail( 448): at
org.apache.harmony.luni.platform.OSNetworkSystem.crea
teSocketImpl(Native Method)
E/SendMail( 448): at
org.apache.harmony.luni.platform.OSNetworkSystem.crea
teSocket(OSNetworkSystem.java:79)
E/SendMail( 448): at
org.apache.harmony.luni.net.PlainSocketImpl2.create(P
lainSocketImpl2.java:59)
E/SendMail( 448): at
java.net.Socket.checkClosedAndCreate(Socket.java:763)

E/SendMail( 448): at java.net.Socket.connect(Socket.java:910)
E/SendMail( 448): at java.net.Socket.connect(Socket.java:888)
E/SendMail( 448): at
com.sun.mail.util.SocketFetcher.createSocket(SocketFe
tcher.java:233)
E/SendMail( 448): at
com.sun.mail.util.SocketFetcher.getSocket(SocketFetch
er.java:163)
E/SendMail( 448): at
com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransp
ort.java:1360)
E/SendMail( 448): ... 29 more
soniya
Senior Developer
Senior Developer
 
Posts: 111
Joined: Mon Jan 14, 2008 1:52 pm
Location: india

Postby soniya » Mon Oct 27, 2008 3:32 pm

soniya wrote:im using example
http://davanum.wordpress.com/2007/12/22 ... a-gmail-...
with 1.0
and found Following is
the exception.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. E/OSNetworkSystem(  448): unknown socket error -1
  2. E/SendMail(  448): Could not connect to SMTP host: smtp.gmail.com,
  3. port: 465
  4. E/SendMail(  448): javax.mail.MessagingException: Could not connect to
  5. SMTP host
  6. : smtp.gmail.com, port: 465;
  7. E/SendMail(  448):   nested exception is:
  8. E/SendMail(  448):      java.net.SocketException: unknown error
  9. E/SendMail(  448):      at
  10. com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransp
  11. ort.java:1391)
  12. E/SendMail(  448):      at
  13. com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPT
  14. ransport.java:412)
  15. E/SendMail(  448):      at javax.mail.Service.connect(Service.java:
  16. 310)
  17. E/SendMail(  448):      at javax.mail.Service.connect(Service.java:
  18. 169)
  19. E/SendMail(  448):      at javax.mail.Service.connect(Service.java:
  20. 118)
  21. E/SendMail(  448):      at javax.mail.Transport.send0(Transport.java:
  22. 188)
  23. E/SendMail(  448):      at javax.mail.Transport.send(Transport.java:
  24. 118)
  25. E/SendMail(  448):      at
  26. org.apache.android.mail.GMailSender.sendMail(GMailSen
  27. der.java:60)
  28. E/SendMail(  448):      at org.apache.android.mail.SendMail
  29. $1.onClick(SendMail.j
  30. ava:35)
  31. E/SendMail(  448):      at android.view.View.performClick(View.java:
  32. 2109)
  33. E/SendMail(  448):      at android.view.View.onTouchEvent(View.java:
  34. 3523)
  35. E/SendMail(  448):      at
  36. android.widget.TextView.onTouchEvent(TextView.java:44
  37. 10)
  38. E/SendMail(  448):      at
  39. android.view.View.dispatchTouchEvent(View.java:3178)
  40. E/SendMail(  448):      at
  41. android.view.ViewGroup.dispatchTouchEvent(ViewGroup.j
  42. ava:857)
  43. E/SendMail(  448):      at
  44. android.view.ViewGroup.dispatchTouchEvent(ViewGroup.j
  45. ava:857)
  46. E/SendMail(  448):      at
  47. android.view.ViewGroup.dispatchTouchEvent(ViewGroup.j
  48. ava:857)
  49. E/SendMail(  448):      at
  50. android.view.ViewGroup.dispatchTouchEvent(ViewGroup.j
  51. ava:857)
  52. E/SendMail(  448):      at com.android.internal.policy.impl.PhoneWindow
  53. $DecorVie
  54. w.superDispatchTouchEvent(PhoneWindow.java:1561)
  55. E/SendMail(  448):      at
  56. com.android.internal.policy.impl.PhoneWindow.superDis
  57. patchTouchEvent(PhoneWindow.java:1085)
  58. E/SendMail(  448):      at
  59. android.app.Activity.dispatchTouchEvent(Activity.java
  60. :1873)
  61. E/SendMail(  448):      at com.android.internal.policy.impl.PhoneWindow
  62. $DecorVie
  63. w.dispatchTouchEvent(PhoneWindow.java:1545)
  64. E/SendMail(  448):      at
  65. android.view.ViewRoot.handleMessage(ViewRoot.java:114
  66. 0)
  67. E/SendMail(  448):      at
  68. android.os.Handler.dispatchMessage(Handler.java:88)
  69. E/SendMail(  448):      at android.os.Looper.loop(Looper.java:123)
  70. E/SendMail(  448):      at
  71. android.app.ActivityThread.main(ActivityThread.java:3
  72. 742)
  73. E/SendMail(  448):      at
  74. java.lang.reflect.Method.invokeNative(Native Method)
  75. E/SendMail(  448):      at java.lang.reflect.Method.invoke(Method.java:
  76. 515)
  77. E/SendMail(  448):      at com.android.internal.os.ZygoteInit
  78. $MethodAndArgsCalle
  79. r.run(ZygoteInit.java:739)
  80. E/SendMail(  448):      at
  81. com.android.internal.os.ZygoteInit.main(ZygoteInit.ja
  82. va:497)
  83. E/SendMail(  448):      at dalvik.system.NativeStart.main(Native
  84. Method)
  85. E/SendMail(  448): Caused by: java.net.SocketException: unknown error
  86. E/SendMail(  448):      at
  87. org.apache.harmony.luni.platform.OSNetworkSystem.crea
  88. teSocketImpl(Native Method)
  89. E/SendMail(  448):      at
  90. org.apache.harmony.luni.platform.OSNetworkSystem.crea
  91. teSocket(OSNetworkSystem.java:79)
  92. E/SendMail(  448):      at
  93. org.apache.harmony.luni.net.PlainSocketImpl2.create(P
  94. lainSocketImpl2.java:59)
  95. E/SendMail(  448):      at
  96. java.net.Socket.checkClosedAndCreate(Socket.java:763)
  97.  
  98. E/SendMail(  448):      at java.net.Socket.connect(Socket.java:910)
  99. E/SendMail(  448):      at java.net.Socket.connect(Socket.java:888)
  100. E/SendMail(  448):      at
  101. com.sun.mail.util.SocketFetcher.createSocket(SocketFe
  102. tcher.java:233)
  103. E/SendMail(  448):      at
  104. com.sun.mail.util.SocketFetcher.getSocket(SocketFetch
  105. er.java:163)
  106. E/SendMail(  448):      at
  107. com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransp
  108. ort.java:1360)
  109. E/SendMail(  448):      ... 29 more
Parsed in 0.044 seconds, using GeSHi 1.0.8.4

plz help me out...

[mod]Fixed some tag errors, now the layout is back to normal :)
soniya
Senior Developer
Senior Developer
 
Posts: 111
Joined: Mon Jan 14, 2008 1:52 pm
Location: india

Postby lucabelluccini » Mon Oct 27, 2008 3:37 pm

Maybe a little explanation on what is your application doing could be useful.
It seems to be an uncaught exception during connection to SMTP host.
I never tested SMTP connections but notice that if you are using SSL protocol, it is necessary, in some cases (expecially when the Certification Authority of SSL Certificate is not a valid organization), to write a "dummy" SSLSocketFactory Class.
lucabelluccini
Freshman
Freshman
 
Posts: 6
Joined: Thu May 22, 2008 3:58 pm
Location: Turin

Postby Steph » Thu Nov 06, 2008 11:44 am

Do you know how to sucess a dummy SSLSocketFactory class?

I need hel to do that.

I use apache library and nothing work

I m' going to let it done.
Steph
Experienced Developer
Experienced Developer
 
Posts: 90
Joined: Mon Oct 20, 2008 1:48 pm
Location: France

Top

DummyTrustmanager and DummySSLSocketFactory

Postby lucabelluccini » Thu Nov 06, 2008 1:02 pm

You need to implement:
DummyTrustManager
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package YOURPACKAGE;
  3.  
  4.  
  5.  
  6. import javax.net.ssl.X509TrustManager;
  7.  
  8.  
  9.  
  10. import java.security.cert.CertificateException;
  11.  
  12. import java.security.cert.X509Certificate;
  13.  
  14.  
  15.  
  16. public class DummyTrustManager implements X509TrustManager {
  17.  
  18.  
  19.  
  20.     public void checkClientTrusted(X509Certificate[] cert, String authType) throws CertificateException {
  21.  
  22.         // everything is trusted
  23.  
  24.     }
  25.  
  26.  
  27.  
  28.     public void checkServerTrusted(X509Certificate[] cert, String authType) throws CertificateException {
  29.  
  30.         // everything is trusted
  31.  
  32.     }
  33.  
  34.  
  35.  
  36.     public X509Certificate[] getAcceptedIssuers() {
  37.  
  38.         return new X509Certificate[0];
  39.  
  40.     }
  41.  
  42. }
  43.  
  44.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


DummySSLSocketFactory
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package YOURPACKAGE;
  3.  
  4.  
  5.  
  6. import java.io.IOException;
  7.  
  8. import java.net.InetAddress;
  9.  
  10. import java.net.Socket;
  11.  
  12.  
  13.  
  14. import javax.net.SocketFactory;
  15.  
  16. import javax.net.ssl.*;
  17.  
  18.  
  19.  
  20.  
  21.  
  22. public class DummySSLSocketFactory extends SSLSocketFactory {
  23.  
  24.     private SSLSocketFactory factory;
  25.  
  26.  
  27.  
  28.     public DummySSLSocketFactory() {
  29.  
  30.         try {
  31.  
  32.             SSLContext sslcontext = SSLContext.getInstance("TLS");
  33.  
  34.             sslcontext.init(null,
  35.  
  36.                                  new TrustManager[] { new DummyTrustManager() },
  37.  
  38.                                  null);
  39.  
  40.             factory = (SSLSocketFactory)sslcontext.getSocketFactory();
  41.  
  42.         } catch(Exception ex) {
  43.  
  44.             // ignore
  45.  
  46.         }
  47.  
  48.     }
  49.  
  50.  
  51.  
  52.     public static SocketFactory getDefault() {
  53.  
  54.         return new DummySSLSocketFactory();
  55.  
  56.     }
  57.  
  58.  
  59.  
  60.     public Socket createSocket() throws IOException {
  61.  
  62.         return factory.createSocket();
  63.  
  64.     }
  65.  
  66.  
  67.  
  68.     public Socket createSocket(Socket socket, String s, int i, boolean flag)
  69.  
  70.                                 throws IOException {
  71.  
  72.         return factory.createSocket(socket, s, i, flag);
  73.  
  74.     }
  75.  
  76.  
  77.  
  78.     public Socket createSocket(InetAddress inaddr, int i,
  79.  
  80.                                 InetAddress inaddr1, int j) throws IOException {
  81.  
  82.         return factory.createSocket(inaddr, i, inaddr1, j);
  83.  
  84.     }
  85.  
  86.  
  87.  
  88.     public Socket createSocket(InetAddress inaddr, int i)
  89.  
  90.                                 throws IOException {
  91.  
  92.         return factory.createSocket(inaddr, i);
  93.  
  94.     }
  95.  
  96.  
  97.  
  98.     public Socket createSocket(String s, int i, InetAddress inaddr, int j)
  99.  
  100.                                 throws IOException {
  101.  
  102.         return factory.createSocket(s, i, inaddr, j);
  103.  
  104.     }
  105.  
  106.  
  107.  
  108.     public Socket createSocket(String s, int i) throws IOException {
  109.  
  110.         return factory.createSocket(s, i);
  111.  
  112.     }
  113.  
  114.  
  115.  
  116.     public String[] getDefaultCipherSuites() {
  117.  
  118.         return factory.getDefaultCipherSuites();
  119.  
  120.     }
  121.  
  122.  
  123.  
  124.     public String[] getSupportedCipherSuites() {
  125.  
  126.         return factory.getSupportedCipherSuites();
  127.  
  128.     }
  129.  
  130. }
  131.  
  132.  
Parsed in 0.042 seconds, using GeSHi 1.0.8.4


And remember to put this condition while defining connection properties (overrideSSLFactory is a boolean arg):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                 // new Empty Properties
  3.  
  4.                 Properties props = new Properties();
  5.  
  6.                 // SSL Stuff
  7.  
  8.                 if(this.useSSL){
  9.  
  10.                         if(overrideSSLFactory){
  11.  
  12.                                 props.setProperty("mail.pop3.socketFactory.class", "YOURPACKAGE.DummySSLSocketFactory" );
  13.  
  14.                         }else {
  15.  
  16.                                 props.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory" );
  17.  
  18.                         }
  19.  
  20.                         // Prevents to fall into NOT-secure connection
  21.  
  22.                         props.setProperty("mail.pop3.socketFactory.fallback", "false");
  23.  
  24.                 }
  25.  
  26.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
lucabelluccini
Freshman
Freshman
 
Posts: 6
Joined: Thu May 22, 2008 3:58 pm
Location: Turin

Postby Helios » Mon Feb 16, 2009 4:11 pm

Hi, i am interested in using JavaMail API but I can't open the links that appear on the first post of this thread. Someone has an alternative link? I hope some can help me.

Regards,
Helios
Helios
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sat Jan 17, 2009 7:11 am

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests