How to send email with html table output in email - android

I am working on ArrayList, i want to send email type of html format with arraylist content,
i am trying to write array content in BufferedWriter Like..
mOrderList = db.getOrderList();
// getting all arrayList content
for (int i = 0; i < mOrderList.size(); i++) {
no = mOrderList.get(i).getId();
itemName = mOrderList.get(i).getOrderItemName();
unit = mOrderList.get(i).getOrderUnit();
qty = mOrderList.get(i).getOrderQTY();
rate = mOrderList.get(i).getOrderRate();
amt = mOrderList.get(i).getOrderAmount();
try {
mbufferWriter =new BufferedWriter(new FileWriter("/data/data/com.sample.category/abc.txt"));
mbufferWriter.write("<html><h4>Customer Order List</h4>" + "<body> <table>"
+ "<tr><th>Item No</th>" + "<th>Item Name</th>"
+ "<th>Unit</th>" + "<th>QTY</th>" + "<th>Item Rate</th>"
+ "<th>Amount</th></tr>");
mbufferWriter.append("<tr><td>" +no+"</td>");
mbufferWriter.append("<td>"+itemName+ "<td>");
mbufferWriter.append("<td>"+unit+ "<td>");
mbufferWriter.append("<td>"+qty+ "<td>");
mbufferWriter.append("<td>"+rate+ "<td>");
mbufferWriter.append("<td>"+amt+ "<td></tr>");
mbufferWriter.append("</body></table></html>");
mbufferWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}//for
and trying to send an email like this but it gives error...
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL,
new String[] { "abc#gmail.com" });
i.putExtra(Intent.EXTRA_SUBJECT, "sample email sending");
i.putExtra(Intent.EXTRA_TEXT,Html.toHtml((Spanned) mbufferWriter));
try {
startActivity(Intent.createChooser(i, "Send mail..."));
// startActivity(i);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(EmailActivity.this,
"There are no email clients installed.",
Toast.LENGTH_SHORT).show();
}
can any one help me fix this problem ..
Thanks in advance..

Html.toHtml doesn't support <table>. It only supports basic tags like <b><u><i>...

Use a pre tag to create a table:
mbufferWriter.write("<html><h4>Customer Order List</h4>" + "<body>" +
"<pre> " +
" Item No Item Name Unit QTY Item Rate Amount" +
"</pre>");

Related

xamrin android visual studio 2019 community version for WhatsApp chat with button click on specific number

I want to add button in my app. when I click on the button this will redirect me to WhatsApp chat having that specific number and chat with that number. I have the following code but its not working :(
public void Button1_Click(object sender, System.EventArgs e)
{
string phoneNumberWithCountryCode = "+9233623xxx";
string message = "Hallo";
StartActivity(new Intent(
Intent.ActionView, Android.Net.Uri.Parse("https://api.whatsapp.com/send?phone=" + phoneNumberWithCountryCode + "&text=" + message)));
}
Button button1 = FindViewById<Button>(Resource.Id.watsapp_op);
button1.Click += (sender, e) => {
Button1_Click(sender, e);
};
I saw the working sample , it sets Package in Intent , so try the following code .
private void OpenWhatsApp(Activity activity, string number, string message)
{
try
{
PackageManager packageManager = activity.PackageManager;
Intent i = new Intent(Intent.ActionView);
String url = "https://api.whatsapp.com/send?phone=" + number + "&text=" + URLEncoder.Encode(message, "UTF-8");
i.SetPackage("com.whatsapp");
i.SetData(Uri.Parse(url));
if (i.ResolveActivity(packageManager) != null)
{
activity.StartActivity(i);
}
else
{
//Error message
}
}
catch (Exception e)
{
// exception
}
}
Update
Does the button on the current content view or not ?
You can see SetContentView(Resource.Layout.activity_main) in OnCreate method .
You have to ensure the button is placed in activity_main.xml , if the button is in another layout , you have to find the view first .
var view = LayoutInflater.Inflate(Resource.Layout.anotherlayout,null);
var button = view.FindViewById<Button>(Resource.Id.button);

Is it possible to use Javamail to receive mails in Android

I'm learning Javamail these days following this website:
http://www.tutorialspoint.com/javamail_api/
I test the sending & added some extra stuff because it was on Android & it worked!
But things have changed completely when I tried to follow receiving email Tutorial which makes me wonder..
Is it possible to make this code :
https://www.tutorialspoint.com/javamail_api/javamail_api_fetching_emails.htm
works on android but using XML interface?!
You can use the following code:
public static void receiveEmail(String pop3Host, String storeType, user, String password) {
try {
//1) get the session object
Properties properties = new Properties();
properties.put("mail.pop3.host", pop3Host);
Session emailSession = Session.getDefaultInstance(properties);
//2) create the POP3 store object and connect with the pop server
POP3Store emailStore = (POP3Store) emailSession.getStore(storeType);
emailStore.connect(user, password);
//3) create the folder object and open it
Folder emailFolder = emailStore.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
//4) retrieve the messages from the folder in an array and print it
Message[] messages = emailFolder.getMessages();
for (int i = 0; i < messages.length; i++) {
Message message = messages[i];
System.out.println("---------------------------------");
System.out.println("Email Number " + (i + 1));
System.out.println("Subject: " + message.getSubject());
System.out.println("From: " + message.getFrom()[0]);
System.out.println("Text: " + message.getContent().toString());
}
//5) close the store and folder objects
emailFolder.close(false);
emailStore.close();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
call your method passing some parameters:
String host = "pop.gmail.com";//change accordingly
String mailStoreType = "pop3";
String username= "example#gmail.com";
String password= "xxxxx";//change accordingly
receiveEmail(host, mailStoreType, username, password);
Source: Steps for receiving the email using JavaMail API

Sending Mail in Android Intent Without Pressing Send Button [duplicate]

This question already has answers here:
send data to email in background
(3 answers)
Closed 6 years ago.
i was creating Android Intent Mail. i am getting all the subject, to , mail body in email. is there is any possibility to send the mail without pressing send button .
My code is :
public void Sendmail(HashMap s) {
HashMap<String, String> sss = s;
String[] toppings = new String[sss.size()];
int size1 = 0;
for (String key : sss.keySet()) {
toppings[size1] = key + "\n" + sss.get(key) + "\n";
System.out.println("key: " + key + " value: " + sss.get(key));
size1++;
}
StringBuilder builder = new StringBuilder();
for (String s3 : toppings) {
builder.append(s3);
}
String mbody = builder.toString();
Intent i = new Intent(android.content.Intent.ACTION_SEND);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setType("plain/text");
i.putExtra(android.content.Intent.EXTRA_SUBJECT, "Task Activity");
i.putExtra(android.content.Intent.EXTRA_TEXT, mbody);
i.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {"hari.andoidsaiss#gmail.com"});
try {
startActivity(i);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(Main2Activity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}
You can't do it. A user should know what he is sending. Also a user should be aware about his actions.
You can do it using API and sending Email from server.
You can't do it with Share Intent of Android as it will populate installed app from your device which can process your data.
You can achieve this by following ways:
Implement mail client to send emails from your server side. ex: mailgun.
Integrate API's like Javamail or Gmail to your android app to get your work done.

How to send SIP messages with custom header using pjsua2 android

With reference of that enter link description here
I have send message successfully but I want to send custom header as well because of get status which particular message has send to update user
public void sendInstantMessage(String number, String msgBody) {
String sipServer = "aaa.ggg.net";
String buddy_uri = "<sip:" + number + "#" + sipServer + ">";
BuddyConfig bCfg = new BuddyConfig();
bCfg.setUri(buddy_uri);
bCfg.setSubscribe(false);
MyBuddy myBuddy = new MyBuddy(bCfg);
SendInstantMessageParam prm = new SendInstantMessageParam();
prm.setContent(msgBody);
// prm.setUserData(value)
try {
myBuddy.create(account, bCfg);
myBuddy.sendInstantMessage(prm);
myBuddy.delete();
} catch (Exception e) {
e.printStackTrace();
return;
}
}
By using `Token pj::SendInstantMessageParam::userData for link enter link description here
I want to send the userdata header, but how to sent that header??
Thanks
Finally I got the solution for sending SMS with custom header using pjsip-2.4
Here is the code
String msgBody = "sending message";
SendInstantMessageParam prm = new SendInstantMessageParam();
prm.setContent(msgBody);
SipHeader hName = new SipHeader();
hName.setHName("name");
hName.setHValue(uniqueId);
SipHeaderVector headerVector = new SipHeaderVector();
headerVector.add(hName);
SipTxOption option = new SipTxOption();
option.setHeaders(headerVector);
prm.setTxOption(option);
try {
myBuddy.sendInstantMessage(prm);
} catch (Exception e) {
e.printStackTrace();
}

gData: get account(self contact) first and last name

I want to get first and last name of a google account using gdata library. I have the auth token(I take it from android device - send it to my java servlet- then should add an insert into a mysql db with first, last, display name and provider_uid(provider_uid is the form of https://www.google.com/accounts/o8/id?id=AItOawmyn...)).
I used Contacts feed like this(without success):
public String tryGoogleAuthentication(String auth_token){
ContactsService contactsService = new ContactsService("...");
contactsService.setUserToken(auth_token);
//contactsService.setAuthSubToken(auth_token);
ContactFeed feed = null;
try {
feed = contactsService.getFeed(new URL("https://www.google.com/m8/feeds/contacts/" + "someEmail#gmail.com" + "/full?max-results=10000"), ContactFeed.class);
} catch (IOException e) {
e.printStackTrace();
return CONST.GOOGLE_AUTH_INVALID_TOKEN;
} catch (ServiceException e) {
e.printStackTrace();
return CONST.GOOGLE_AUTH_INVALID_TOKEN;
} catch (NullPointerException e) {
e.printStackTrace();
return CONST.GOOGLE_AUTH_INVALID_TOKEN;
}
if (feed == null)
return "";
String externalId = feed.getId();
Person person = feed.getAuthors().get(0);
String email = person.getEmail();
String name = person.getName();
String nameLang = person.getNameLang();
String extensionLocalName = person.getExtensionLocalName();
String uri = person.getUri();
System.out.println("externalId: " + externalId);
System.out.println("email: " + email);
System.out.println("name: " + name);
System.out.println("nameLang: " + nameLang);
System.out.println("extension local name: " + extensionLocalName);
System.out.println("URI: " + uri);
System.out.println(feed.getSelf().getEntries().get(0).getTitle().getPlainText());
return CONST.STATUS_OK;
}
Also,
System.out.println("ID: " + feed.getSelf().getEntries().get(0).getId());
will output:
ID: http://www.google.com/m8/feeds/contacts/someEmail%40gmail.com/base/c....
but I want something like this:
https://www.google.com/accounts/o8/id?id=AItOawmyn...
I need this to insert into an existing data base.
Please note that I want the info only for the account, not for it's contacts.
Thanks,
Alex
Please see this answer from google groups for resolution. The problem is that I cannot access user profile with the auth_token taken from the android because it's a Client Login token, and Client Login does not have a scope for accessing user's profile. I integrated OAUTH login in android like this and with the token returned, I can access user's profile.
Alex.

Categories

Resources