Dabei habe ich doch
a,) In der AndroidManifest.xml:
und
b.) Im App:
Gefragt werde ich aber nicht mit "Zulassen" vs. "Nicht zulasssen", in einem anderen App funktioniert das bei der Kamera gut:
(allerdings geht's an dieser Stelle um die Kamera und nicht um den Internetzugriff)
Also, hier der Code des SMTP-Client - läuft wie gesagt auf PC nciht aber auf Android:
Ausgabe auf PC mit Junit-Test:
Wieder mal ziemlich schräg, was Ganze...!!
Irgendwie sollte ich mal herausfinden wie man Android-Apps debuggt, aber die Sache über die "Developer Tools" von Chrome scheint irgendwie furchtbar komplex zu sein.
a,) In der AndroidManifest.xml:
Code:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
und
b.) Im App:
Code:
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.INTERNET}, 1);
Gefragt werde ich aber nicht mit "Zulassen" vs. "Nicht zulasssen", in einem anderen App funktioniert das bei der Kamera gut:
Code:
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
(allerdings geht's an dieser Stelle um die Kamera und nicht um den Internetzugriff)
Also, hier der Code des SMTP-Client - läuft wie gesagt auf PC nciht aber auf Android:
Code:
public class SMTPClient
{
private static final DateFormat cmDateFormat;
private static final SMTPClient cmInstance;
static
{
cmInstance = new SMTPClient();
cmDateFormat = DateFormat.getDateInstance(DateFormat.FULL, Locale.GERMAN);
}
private SMTPClient()
{
super();
}
public int send(final String caHostname, final String caUsername, final String caPassword, final String caMessage, final String caMailFromAddr, final String caMailFromName, final String caMailToAddr, final String caMailToName, final String caSubject, final Attachment caAttachment, final boolean caUseSSL)
{
final long clStartTime = System.currentTimeMillis();
String lUsername = caUsername;
String lPassword = caPassword;
boolean lUseAuth = true;
if (((caUsername == null) || (caUsername.trim().intern() == "")) || (caPassword == null))
{
lUseAuth = !lUseAuth;
}
else
{
lUsername = Base64Class.encode(caUsername);
lPassword = Base64Class.encode(caPassword);
}
SSLSocket lSSLSocket = null;
if (caUseSSL)
{
try
{
lSSLSocket = (SSLSocket) ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket(caHostname, 465);
}
catch (final Exception caException)
{
throw new RuntimeException(caException);
}
}
Socket lSocket = null;
final Date clDate = new Date();
DataInputStream lDataInputStream = null;
DataOutputStream lDataOutputStream = null;
int lStatusCode = -1;
boolean lFileIsBinary = false;
if (caAttachment != null)
{
lFileIsBinary = caAttachment.getFileIsBinary().booleanValue();
}
try
{
if (caUseSSL)
{
lSocket = lSSLSocket;
}
else
{
lSocket = SMTPClient.getSocket(caHostname);
}
final BufferedReader clBufferedReaderForSSLConnection = new BufferedReader(new InputStreamReader(lSocket.getInputStream()));
(new Thread(new Runnable() {
@Override
public void run()
{
try
{
String line = null;
while ((line = clBufferedReaderForSSLConnection.readLine()) != null)
{
System.out.println("SERVER: " + line);
}
}
catch (final Exception clException)
{
throw new RuntimeException(clException);
}
}
})).start();
lDataInputStream = new DataInputStream(lSocket.getInputStream());
lDataOutputStream = new DataOutputStream(lSocket.getOutputStream());
final int clSleepTime = 80;
lDataOutputStream.writeBytes("EHLO " + caHostname + "\r\n");
this.sleep(clSleepTime);
lDataOutputStream.writeBytes("AUTH LOGIN" + "\r\n");
this.sleep(clSleepTime);
lDataOutputStream.writeBytes(lUsername + "\r\n");
this.sleep(clSleepTime);
lDataOutputStream.writeBytes(lPassword + "\r\n");
this.sleep(clSleepTime);
lDataOutputStream.writeBytes("MAIL FROM:<" + caMailFromAddr + ">\r\n");
this.sleep(clSleepTime);
lDataOutputStream.writeBytes("RCPT TO:<" + caMailToAddr + ">\r\n");
this.sleep(clSleepTime);
lDataOutputStream.writeBytes("DATA\r\n");
this.sleep(clSleepTime);
lDataOutputStream.writeBytes("Subject: " + caSubject + "\r\n");
this.sleep(clSleepTime);
lDataOutputStream.writeBytes("X-MAILER: smtp-client\r\n");
this.sleep(clSleepTime);
lDataOutputStream.writeBytes("DATE: " + SMTPClient.cmDateFormat.format(clDate) + "\r\n");
this.sleep(clSleepTime);
lDataOutputStream.writeBytes("FROM: " + caMailFromName + " <" + caMailFromAddr + ">\r\n");
this.sleep(clSleepTime);
lDataOutputStream.writeBytes("TO: " + caMailToName + " <" + caMailToAddr + ">\r\n");
this.sleep(clSleepTime);
lDataOutputStream.writeBytes("MIME-Version: 1.0\r\n");
this.sleep(clSleepTime);
final UUID clFrontierUUID = UUID.randomUUID();
long lBoundaryUUID = clFrontierUUID.getLeastSignificantBits();
if (lBoundaryUUID < 0)
{
lBoundaryUUID = 0 + -lBoundaryUUID;
}
final String clBoundary = "B_" + lBoundaryUUID + "_BE";
if (caAttachment != null)
{
lDataOutputStream.writeBytes("Content-Type: multipart/mixed; boundary=\"" + clBoundary + "\"\r\n\r\n");
this.sleep(clSleepTime);
lDataOutputStream.writeBytes("\r\n");
this.sleep(clSleepTime);
lDataOutputStream.writeBytes("--" + clBoundary + "\r\n");
this.sleep(clSleepTime);
}
lDataOutputStream.writeBytes("Content-Type: text/html\r\n\r\n");
this.sleep(clSleepTime);
lDataOutputStream.writeBytes(caMessage + "\r\n");
this.sleep(clSleepTime);
if (caAttachment != null)
{
lDataOutputStream.writeBytes("--" + clBoundary + "\r\n");
if (lFileIsBinary)
{
lDataOutputStream.writeBytes("Content-Type: application/octet-stream\r\n");
this.sleep(clSleepTime);
}
else
{
lDataOutputStream.writeBytes("Content-Type: text/plain\r\n");
this.sleep(clSleepTime);
}
lDataOutputStream.writeBytes("Content-Disposition: attachment; filename=\" " + caAttachment.getAttachmentFileName() + " \"\r\n");
this.sleep(clSleepTime);
lDataOutputStream.writeBytes("Content-Transfer-Encoding: base64\r\n\r\n");
this.sleep(clSleepTime);
final InputStream clInputStream = caAttachment.getAttachmentFileContent();
String lFileContent = IOConverter.convertInputStreamToString1(clInputStream);
lFileContent = Base64Class.encode(lFileContent);
lDataOutputStream.writeBytes(new String(lFileContent.getBytes("UTF-8")));
this.sleep(clSleepTime);
lDataOutputStream.writeBytes("\r\n");
this.sleep(clSleepTime);
lDataOutputStream.writeBytes("--" + clBoundary + "\r\n");
this.sleep(clSleepTime);
}
lDataOutputStream.writeBytes("\r\n.\r\n");
this.sleep(clSleepTime);
lDataOutputStream.writeBytes("QUIT\r\n");
String lResponseLine = null;
final InputStreamReader clInputStreamReader = new InputStreamReader(new BufferedInputStream(lDataInputStream));
final BufferedReader clBufferedReader = new BufferedReader(clInputStreamReader);
final long clEndTime = System.currentTimeMillis();
final long clDifference = clEndTime - clStartTime;
while ((lResponseLine = clBufferedReader.readLine()) != null)
{
System.out.println(lResponseLine);
if (lResponseLine.toLowerCase().indexOf("ok") != lStatusCode)
{
lStatusCode = Math.abs(lStatusCode);
long lWaitingTime = 48 - clDifference;
if (lWaitingTime < 0)
{
lWaitingTime = 0 - lWaitingTime;
}
Thread.sleep(lWaitingTime);
ResourceManager.close(lDataInputStream);
break;
}
}
}
catch (final Exception clException)
{
System.out.println(clException.getClass().getCanonicalName() + ": " + clException.getMessage());
}
finally
{
ResourceManager.close(lDataOutputStream);
ResourceManager.close(lSocket);
}
return lStatusCode;
}
public static synchronized SMTPClient createInstance()
{
return SMTPClient.cmInstance;
}
public final class Attachment
{
private final String cmAttachmentFileName;
private final InputStream cmAttachmentFileContent;
private Boolean cmFileIsBinary = Boolean.valueOf(false);
public Attachment(final String caAttachmentFileName, final InputStream caAttachmentFileContent)
{
this.cmAttachmentFileName = caAttachmentFileName;
this.cmAttachmentFileContent = caAttachmentFileContent;
}
public void setFileIsBinary(final Boolean caFileIsBinary)
{
this.cmFileIsBinary = caFileIsBinary;
}
public Boolean getFileIsBinary()
{
return this.cmFileIsBinary;
}
public String getAttachmentFileName()
{
return this.cmAttachmentFileName;
}
public InputStream getAttachmentFileContent()
{
return this.cmAttachmentFileContent;
}
}
private void sleep(final int caMilliSeconds)
{
try
{
Thread.sleep(caMilliSeconds);
}
catch (Exception lException)
{
lException = null;
}
finally
{
final Runtime clRuntime = Runtime.getRuntime();
clRuntime.gc();
}
}
private static Socket getSocket(final String caHostname) throws Exception
{
Socket lSocket = null;
final String clExceptionMessage = "Can't connect to ports 25 and 587...";
try
{
lSocket = new Socket(caHostname, 25);
}
catch (final Exception clSMTPExceptionPort25)
{
try
{
lSocket = new Socket(caHostname, 587);
}
catch (final Exception clSMTPExceptionPort587)
{
throw new RuntimeException(clExceptionMessage);
}
}
return lSocket;
}
}
Ausgabe auf PC mit Junit-Test:
Code:
SERVER: 220 smtp.gmail.com ESMTP o21sm12901005wmh.18 - gsmtp
SERVER: 250-smtp.gmail.com at your service, [x.x.x.x]
SERVER: 250-SIZE 35882577
SERVER: 250-8BITMIME
SERVER: 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
SERVER: 250-ENHANCEDSTATUSCODES
SERVER: 250-PIPELINING
SERVER: 250-CHUNKING
SERVER: 250 SMTPUTF8
SERVER: 334 VXNlcm5hbWU6
SERVER: 334 UGFzc3dvcmQ6
SERVER: 235 2.7.0 Accepted
SERVER: 250 2.1.0 OK o21sm12901005wmh.18 - gsmtp
SERVER: 250 2.1.5 OK o21sm12901005wmh.18 - gsmtp
SERVER: 354 Go ahead o21sm12901005wmh.18 - gsmtp
SERVER: 250 2.0.0 OK 1594984524 o21sm12901005wmh.18 - gsmtp
221 2.0.0 closing connection o21sm12901005wmh.18 - gsmtp
Wieder mal ziemlich schräg, was Ganze...!!
Irgendwie sollte ich mal herausfinden wie man Android-Apps debuggt, aber die Sache über die "Developer Tools" von Chrome scheint irgendwie furchtbar komplex zu sein.