'store.connect(imap.gmail.com, email, pass) can't connect to host, port imap.gmail.com, 993; timeout -1;
I'm trying to create an app to send and receive emails, but whenever I run it in my emulator I get this error: System.err: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: imap.gmail.com, 993; timeout -1; Using socket factory class javax.net.ssl.SSLSocketFactory;
I have checked my firewall and it doesn't block any ports as far as I can tell. This code is not in the main activity and the user email and password are transfered in extras. Any help would be appreciated.
private Adapter adapter = new Adapter(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout Activity_Email = findViewById(R.id.LinearLayout_ActivityEmail);
final LinearLayout Activity_Main = findViewById(R.id.LinearLayout_ActivityMain);
final Button Button_NewEmail = findViewById(R.id.Button_NewEmail);
final Button Button_UserProfile = findViewById(R.id.Button_UserProfile);
final Button Button_Contacts = findViewById(R.id.Button_Contacts);
final Button Button_ProfileFolders = findViewById(R.id.Button_ProfileFolders);
final Button Button_Settings = findViewById(R.id.Button_Settings);
ListView ListView_Emails = findViewById(R.id.ListView_EmailList);
final EditText EditText_Filter = findViewById(R.id.EditText_EmailFilter);
final Bundle extras = getIntent().getExtras();
Properties properties = System.getProperties();
properties.put("mail.store.protocol", "imaps");
properties.put("mail.imaps.port", "993");
properties.setProperty("mail.imaps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.imaps.socketFactory.fallback", "false");
Session imapSession = Session.getInstance(properties);
Thread thread = new Thread()
{
@Override
public void run()
{
try {
System.out.println("\nGetting store\n");
Folder inbox;
System.out.println("\nConnecting store\n");
Store store = imapSession.getStore("imaps");
System.out.println("\nGetting folder\n");
store.connect("imap.gmail.com", extras.getString("USER_EMAIL"), extras.getString("PASSWORD"));
inbox=store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
Message messages[] = inbox.getMessages();
int numberOfEmails = messages.length;
for(int i = 0; i < numberOfEmails; i++)
{
List_Email new_email = new List_Email(Arrays.toString(messages[i].getFrom()), extras.getString("USER_EMAIL"), messages[i].getSubject(), messages[i].getContent().toString());
adapter.addList_Email(new_email);
}
} catch (MessagingException e) {
System.out.println("Prekid ovde Message\n");
e.printStackTrace();
} catch (IOException e) {
System.out.println("Prekid ovde IO\n");
e.printStackTrace();
}
}
};
thread.run();
ListView_Emails.setAdapter(adapter);
ListView_Emails.setOnItemClickListener(new MyOnItemClickListener());
Button_NewEmail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Emails_Activity.this, NewEmailActivity.class);
intent.putExtra("USER_EMAIL", extras.getString("USER_EMAIL"));
intent.putExtra("PASSWORD", extras.getString("PASSWORD"));
startActivity(intent);
}
});
Button_ProfileFolders.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Emails_Activity.this, FoldersActivity.class);
startActivity(intent);
}
});
}
private class MyOnItemClickListener implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Bundle extras =getIntent().getExtras();
List_Email mail = (List_Email) adapter.getItem(position);
Intent intent = new Intent(Emails_Activity.this, EmailActivity.class);
String senderEmail = mail.Sender;
String title = mail.Title;
String content = mail.Content;
intent.putExtra("Sender", senderEmail);
intent.putExtra("Title", title);
intent.putExtra("Content", content);
startActivity(intent);
}
}
}
In case it is needed, here is the AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyEmailApp2">
<activity android:name=".Emails_Activity">
</activity>
<activity android:name=".EmailActivity">
</activity>
<activity android:name=".NewEmailActivity">
</activity>
<activity android:name=".LoginActivity">
</activity>
<activity android:name=".FoldersActivity">
</activity>
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Here is the output in the Run tab when I get to the error while running the app on the emulator:
D/EGL_emulation: eglMakeCurrent: 0xda01a360: ver 3 1 (tinfo 0xda00f800)
I/chatty: uid=10135(com.example.myemailapp2) RenderThread identical 3 lines
D/EGL_emulation: eglMakeCurrent: 0xda01a360: ver 3 1 (tinfo 0xda00f800)
D/OpenGLRenderer: endAllActiveAnimators on 0xc299ab80 (RippleDrawable) with handle 0xda00fa50
D/EGL_emulation: eglMakeCurrent: 0xda01a360: ver 3 1 (tinfo 0xda00f800)
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@8318084
I/System.out: Getting store
I/System.out: Connecting store
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
I/System.out: Prekid ovde Message
W/System.err: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: imap.gmail.com, 993; timeout -1; Using socket factory class javax.net.ssl.SSLSocketFactory;
nested exception is:
android.os.NetworkOnMainThreadException
W/System.err: at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:735)
at javax.mail.Service.connect(Service.java:366)
at javax.mail.Service.connect(Service.java:246)
at com.example.myemailapp2.Emails_Activity$1.run(Emails_Activity.java:79)
at com.example.myemailapp2.Emails_Activity.onCreate(Emails_Activity.java:107)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
W/System.err: Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:115)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)
at java.net.InetAddress.getByName(InetAddress.java:1106)
at java.net.InetSocketAddress.<init>(InetSocketAddress.java:235)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:352)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:217)
at com.sun.mail.iap.Protocol.<init>(Protocol.java:124)
at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:128)
at com.sun.mail.imap.IMAPStore.newIMAPProtocol(IMAPStore.java:758)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:693)
... 19 more
D/EGL_emulation: eglMakeCurrent: 0xda01a360: ver 3 1 (tinfo 0xda00f800)
D/EGL_emulation: eglMakeCurrent: 0xda01a360: ver 3 1 (tinfo 0xda00f800)
Solution 1:[1]
Your full stack trace shows the problem:
nested exception is:
android.os.NetworkOnMainThreadException
You may not do any networking on the main thread of your android application. You must create a worker thread or similar and do it there.
The documentation for the NetworkOnMainThread exception further describes the problem and links to a document about why and how to deal with it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
