I am trying to create application and the application will able to send email from the application from WebView android. the example of the code will be like
Send Email
the code working fine in browser, but in the WebView in android itself, it shown that the action is not supported.So I wonder, is it possible to send an email using HTML only in WebView?
Something like this will do the job in your webview client.
What it does :
First, parse the link, to detect if it's an mailto link or not.
Parse to get the arguments of the mailto link
Send the email in an intent, with the given argument of your mailto link
See below :
public boolean shouldOverrideUrlLoading (WebView view, String url){
String mailToRegexp = "mailto\\:([^?]+)\\?{0,1}((subject\\=([^&]+))|(body\\=([^&]+))|(bcc\\=([^&]+))|(cc\\=([^&]+)))*";
Pattern mailToPattern = Pattern.compile(mailToRegexp);
Matcher mailToMatcher = mailToPattern.matcher(url);
if(mailToMatcher.find()){
String email = mailToMatcher.group(1);
String subject = mailToMatcher.group(4);
String body = mailToMatcher.group(6);
String bcc = mailToMatcher.group(8);
String cc = mailToMatcher.group(10);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, email);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, body);
intent.putExtra(Intent.EXTRA_BCC, bcc);
intent.putExtra(Intent.EXTRA_CC, cc);
startActivity(Intent.createChooser(intent, "Send Email"));
return true;
}
}
You will need to override (in your webview class) shouldOverrideUrlLoading (http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading(android.webkit.WebView,%20java.lang.String)) to parse mailto links and send intent to mail app.
If you are using cordova/phonegap, you need to import inAppBrowser plugin and add target="_system" to your links so they will get directed as an intent
Related
Is it possible to send an email, using an intent, with an image in the body.
I'm using an image that is hosted...
public void sendEmail(View view){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_SUBJECT, "Image in body test");
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(getHtmlBody()));
startActivity(Intent.createChooser(intent, "Send Email"));
}
private String getHtmlBody() {
String html = "<h1><img width=\"100\" src=\"http://cdn2-www.dogtime.com/assets/uploads/gallery/30-impossibly-cute-puppies/impossibly-cute-puppy-8.jpg\"> Hello World </h1>";
return html;
}
I am able to do this using javaMail but that sends the email automatically without the user being able to see anything so I'm hoping I can use intents.
There is EXTRA_HTML_TEXT that allows you to supply HTML as alternative to the text in EXTRA_TEXT. However, there's no guarantee that the receiving app supports this extra (hence the requirement that EXTRA_TEXT must be present, too).
The code could look something like this:
public void sendEmail(View view){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Image in body test");
intent.putExtra(Intent.EXTRA_TEXT, getTextBody());
intent.putExtra(Intent.EXTRA_HTML_TEXT, getHtmlBody());
startActivity(Intent.createChooser(intent, "Send Email"));
}
private String getHtmlBody() {
return "<h1><img width=\"100\" src=\"http://cdn2-www.dogtime.com/assets/uploads/gallery/30-impossibly-cute-puppies/impossibly-cute-puppy-8.jpg\">"
+ getTextBody() + "</h1>";
}
private String getTextBody() {
return "Hello world";
}
Apps that don't support HTML will simply use the text from EXTRA_TEXT.
The answer by Arpit Garg in this post shares that and tags do not work in most email clients, so unfortunately your code wont work. I just tested your code with the Gmail app and a few others and it pulled in a placeholder but not the actual image. what you can do though is add an image as an attachment.
I tried to use this code to send email in an android app but I got this error
no apps can perform this action
code.java
private void send()
{
final String subject2 = subject.getText().toString().trim();
final String message2 = message.getText().toString().trim();
final String from2 = from.getText().toString().trim();
String[] TO = {"recipientadress#gmail.com"};
Uri uri = Uri.parse("mailto:"+from2)
.buildUpon()
.appendQueryParameter("subject", subject2)
.appendQueryParameter("body", message2)
.build();
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, uri);
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
Can you help me please thanks in advance
Using Intent android will try to look for an app to send the mail.
Alternatively, you can try javax-mail api to do that.
These links will be useful:
Adding a jar to Android Studio : Android Studio: Add jar as library?
Sending a mail using Javax mail
http://www.tutorialspoint.com/java/java_sending_email.htm
I'm trying to send a text message with a link from my android app to chat applications like Whatsapp or SMS message.
These apps don't accept text/html type as an Intent type and when I'm using text/plain type my message is being sent with the subject only and without the message's body.
I've seen apps that can share links via Whatsapp like Chrome and Dolphin Browser apps.
Here is my code:
#JavascriptInterface
public void sendMessage(String trip) {
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Trip from Voyajo");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("I've found a trip in Voyajo website that might be interested you, http://www.voyajo.com/viewTrip.aspx?trip=" + trip));
startActivity(Intent.createChooser(emailIntent, "Send to friend"));
}
#JavascriptInterface
public void sendMessage(String trip) {
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Trip from Voyajo");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("I've found a trip in Voyajo website that might be interested you, http://www.voyajo.com/viewTrip.aspx?trip=" + trip));
emailIntent.setType("text/plain");
startActivity(Intent.createChooser(emailIntent, "Send to friend"));
}
here i just change position of emailIntent.setType("text/plain"); this line and it works.
you get your link in messaging app body email app body.but here you can get subject text only in Mail apps not in messaging app but you can get your link in body so achive your goal...
Thats it...
In my app I have it so that the user can send an email (code below).
This works, but I would like to improve it. There are 2 possibilities:
1. Currently, the url appears as a String. Can it be made to appear as a link in the email?
2. Include an image in the email (mylogo.png) and clicking on the image would go to the url
Is either of these possible?
private void sendEmail() {
final UserInfo userInfo = UserInfo.getInstance();
final String highScore = userInfo.getCumulativeScore();
final Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
//intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(crashLogFile)); // attach a file
// EXTRA_EMAIL is the recipient, which in this case we don't know, so leave blank and let user fill in
//intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"Extra Email"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Super Quiz High Score");
String url = "https://play.google.com/store/apps/details?id=com.devname.appname";
intent.putExtra(Intent.EXTRA_TEXT, "My high score in the Super Quiz is now " + highScore + "!\n\n" + url);
startActivity(createEmailOnlyChooserIntent(intent, "Send via email"));
}
Depends completely on the mail client that the receiver uses. If it's gmail, you're out of luck, they don't parse links.
i have a document which is loaded in webViewActivity, in this document i have my email id. when users clicks my email id i want to open email app, please help me.
This is sample document.
This is text contained in document. if you have any queries please contact me at mailto#examplemail.com
Try this :
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//Check whether url contains email or not.
// To start Email Intent
String[] mailto = { "example.com" };
// Create a new Intent to send messages
Intent sendIntent = new Intent(Intent.ACTION_SEND);
// Add attributes to the intent
sendIntent.putExtra(Intent.EXTRA_EMAIL, mailto);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "This is sample document.");
sendIntent.setType("message/rfc822");
startActivity(sendIntent);
return true;
}
Hope this helps.