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..."));
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..."));
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..."));
Html.fromHtml (html)? ¿No está funcionando? – Merve
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
@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