2012-07-12 15 views
17

Deseo enviar un correo electrónico a través de mi aplicación. Necesito enviar correo electrónico basado en HTML solo a través de G-Mail. Encontré las siguientes soluciones que cada uno de ellos tiene ventajas y desventajas.Android, ¿Cómo enviar un correo electrónico HTML y forzar a Android a enviarlo a través de G-Mail y no a otras aplicaciones?

1) Uso de Intent (Intent.ACTION_SEND). Esta es una forma muy simple y puedo ver mi cuerpo en formato HTML, pero el problema es cuando hago clic en el botón "Enviar correo electrónico", tantas aplicaciones como Facebook y Google+ aparecen inútiles y no debería mostrarlo en esa lista . Este es su código:

String html = "<!DOCTYPE html><html><body><a href=\"http://www.w3schools.com\" target=\"_blank\">Visit W3Schools.com!</a>" + "<p>If you set the target attribute to \"_blank\", the link will open in a new browser window/tab.</p></body></html>"; 

Intent intent = new Intent(Intent.ACTION_SEND); 
intent.setType("text/plain"); 
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"MY EMAIL ADDRESS"}); 
intent.putExtra(Intent.EXTRA_SUBJECT, "subject here"); 
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(html)); 
startActivity(Intent.createChooser(intent, "Send email...")); 

enter image description hereenter image description here

2) Uso de Intención (Intent.ACTION_SENDTO). De esta forma Filtra aplicaciones inútiles y me muestra solo clientes de correo. Pero no muestra mi correo electrónico en formato HTML en el cliente de gmail. Cuando envío el correo, algunos clientes muestran el cuerpo en formato HTML, mientras que otros no identifican HTML y mi enlace se comporta como texto sin formato. Este código es como:

String html = "<!DOCTYPE html><html><body><a href=\"http://www.w3schools.com\" target=\"_blank\">Visit W3Schools.com!</a>" + "<p>If you set the target attribute to \"_blank\", the link will open in a new browser window/tab.</p></body></html>"; 
Intent send = new Intent(Intent.ACTION_SENDTO); 
String uriText = "mailto:MY EMAIL ADDRESS" + "?subject=subject here" + "&body=" + html; 
uriText = uriText.replace(" ", "%20"); 
Uri uri = Uri.parse(uriText); 
send.setData(uri); 
startActivity(Intent.createChooser(send, "Send mail...")); 

enter image description hereenter image description here

3) El envío de correo utilizando JavaMail API que añade tanto la complejidad de la aplicación y no he probado hasta ahora.

¿Cuál es su sugerencia? Necesito una forma de tener ventajas de primera y segunda manera. Necesito cuando el usuario haga clic en el botón que muestra el cliente de Gmail y puedo mostrar su contenido html en la parte del cuerpo del cliente.

cualquier sugerencia sería apreciada. Gracias

======================

actualización

Algo sobre el código 2 es incorrecta. El código es así:

String html = "<!DOCTYPE html><html><body><a href=\"http://www.w3schools.com\" target=\"_blank\">Visit W3Schools.com!</a>" + "<p>If you set the target attribute to \"_blank\", the link will open in a new browser window/tab.</p></body></html>"; 
Intent send = new Intent(Intent.ACTION_SENDTO); 
String uriText = "mailto:MY EMAIL ADDRESS" + "?subject=subject here" + "&body=" + Html.fromHtml(html); 
uriText = uriText.replace(" ", "%20"); 
Uri uri = Uri.parse(uriText); 
send.setData(uri); 
startActivity(Intent.createChooser(send, "Send mail...")); 

enter image description here

+0

Html.fromHtml (html)? ¿No está funcionando? – Merve

+0

Como puede ver en mi última captura de pantalla, elimina todas las etiquetas HTML pero el enlace (primera línea) se muestra como texto sin formato. compáralo con la segunda imagen por favor. Gracias. – Hesam

+0

@Hesam No puedo lograr el primero.Intenté pegar tu código completo (1º) y los enlaces se muestran como texto sin formato. ¿Alguna idea? – SKP

Respuesta

2

Si desea sólo una aplicación para manejar su intento entonces es necesario eliminar Intent.createChooser(), en lugar JST usar startActivity() ---> se enviar el correo utilizando el cliente de correo por defecto, si no se establece a continuación, le pedirá a hacerlo ... tat se puede cambiar en cualquier momento

+0

Gracias, probé tu sugerencia. Aunque abre el cliente de correo, no tiene mis datos asignados como a y cuerpo. ambos están limpios. – Hesam

2

probar este código: se seleccionará proveedores de correo electrónico solamente, no facebook, etc

String body="<!DOCTYPE html><html><body><a href=\"http://www.w3schools.com\" target=\"_blank\">Visit W3Schools.com!</a>" + 
         "<p>If you set the target attribute to \"_blank\", the link will open in a new browser window/tab.</p></body></html>"; 
      final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
      emailIntent.setType("text/html");   
      emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);  
      emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(body)); 
      startActivity(Intent.createChooser(emailIntent, "Email:")); 
+0

Gracias querida Manikandan, probé tu código. Aunque eliminó tantas aplicaciones inútiles, como dijo, todavía tiene Bluetooth, Drive, Skype y Wi-Fi Direct en la lista. Gracias por tu sugerencia. – Hesam

5

intente lo siguiente -

Intent shareIntent = new Intent(Intent.ACTION_SENDTO,Uri.parse("mailto:")); 
shareIntent.putExtra(Intent.EXTRA_TEXT,Html.fromHtml(body)); 
shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject); 
startActivity(shareIntent); 

esto sólo presentes aplicaciones de correo electrónico.

+0

Encontré que esta solución no funciona, aunque filtra las aplicaciones solo para tipos de correo electrónico, termina con el mismo problema establecido originalmente, Gmail no formatea correctamente el texto en html. – gnichola

2

Para obtener aplicaciones de correo electrónico solamente, usar Intent.setType ("message/rfc822")

Cuestiones relacionadas