Send email via outlook with mail merge using Java - java

I have a set of contacts in my database. I want my application to build a custom email template for my clients.
My client can set a custom placeholders such as company name, address:
For example:
Dear <<name>>,
This is to inform you that our <<company name>>, located in <<address>> ...
Sincerely,
<<sender>>
After the template is setup I can then use this as a body to my email. Recipients are then fetched from the database.
I am aware of the java.awt.Desktop package which allows me to create a MAIL URI and open it using the user's default email client. The problem is how can I incorporate the mail merge into it? Can you please guide me on existing libraries or solutions to this?

Use the JavaMail library for sending mails. You will find plenty of examples if you search for "JavaMail example", among others: Sending email via Gmail SMTP example. Regarding the placeholders I would simply use the String.replace function.

You will need to control how the variables in the template are setup. I dont think you can parse an arbitrary string and find out if there are variables in it. Hence when a user is adding a variable, make sure that you insert a variable that your program will understand into the email body. Thats a no brainer but thought I'll add it for completeness.
You could save the email body as a velocity template, making sure the variables you added our velocity templating language compliant. Velocity would be easier than string.replace() if there are complicated templates that are being set up. If its a simple one then String.replace() would do.
http://velocity.apache.org/
Next use the java mail library to send it directly from your java program, or launch the default email client of the box using the Desktop class.
EDIT:
If you would like to open outlook then you will need to use Desktop.mail() API. You can pre-populate the to,cc, bcc, subject and body fields in the outlook send email window by constructing an appropriate URI and passing it to Desktop.mail()
mailto:duke#sun.com?SUBJECT=Happy New Year!&BODY=Happy New Year, Duke!
Have a look here for more info:
http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/desktop_api/
For multiple recipients, separating the email addresses with commas should work. If that doesn't, try with a semi colon. Outlook uses semicolon..

Related

Why am I not able to read the complete email body using Exchange Web Service Java API?

I am using EWS Java API to read and process emails. One such email contains few conversation and a MS Teams meeting information at the end. While reading such an email, the EmailMessage.getBody() returns only the MS Teams meeting information and all the other contents of the email body are ommitted. Sample code below:
EmailMessage message = EmailMessage.bind(service, new ItemId(item.get(nMessagePos).getId().getUniqueId()));
String emailBody = message.getBody().toString()
I tried setting the BodyType property to both HTML and Text and then fetched the body of the email but it still returns only the Meeting invite details.
Is there any specific reason for this and is there a way for me to get the complete email body?
I would try to enable tracing https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-trace-requests-responses-to-troubleshoot-ews-managed-api-applications or look at the actually soap responses your getting it could be a parsing issue at the client side (eg bug in the library). You could also try getting the Mimecontent of the Message instead and then parse back the body from that content. Something like EWSEditor might be useful for trying to diagnose what is going on it will show you what the responses look like and allow you to test mimecontent etc without needing to write any code https://github.com/dseph/EwsEditor/releases.

Sending embedded survey email using MonkeySurvey API

What should be the body-text or body-html when doing a POST request using this API to create messages?
API - https://developer.surveymonkey.com/api/v3/#collectors-id-messages
When I am inserting survey link in the body, I am not getting a clickable embedded question in the mail body like we get when using email collector from console.
Building that format is not currently supported in the API, you can only create with a custom HTML.
That said you can copy a previous message that is in the right format through the API. From the link you specified: https://developer.surveymonkey.com/api/v3/#collectors-id-messages
It shows you can set a from collector/message to copy:
POST /v3/collectors/<collector_id>/messages
{
"from_collector_id": "<any_collector_id>",
"from_message_id": "<message_id_from_provided_collector_id>"
}
That will take a copy of a message you already have created as a template and create a new message. Hopefully that could be a good workaround for your use case.

How to change web script to a set response format instead of allowing negotiation?

I just created a web script to get the ticket of Alfresco Share.
Created getticket.get.desc.xml
<webscript>
<shortname>Get User Ticket</shortname>
<description>Personalized greeting</description>
<url>/getticket</url>
<authentication>user</authentication>
<negotiate accept="text/html">html</negotiate>
<negotiate accept="application/json">json</negotiate>
</webscript>
Created getticket.get.html.ftl (Plain Text)
${session.getTicket()}
I am trying to access to it from a Java page. The request is missing some parameters that the web script requires to negotiate the response format. So I need to change my web script to a set response format instead of allowing negotiation. How can I do this?
You can set response format by replacing your negotiation tag lines with following.
extension
There are various response types allowed you can mention one of them json,html,atom are few examples.

Add or request a user to add email id to contact list. Is this possible automatically using javamail ?

I have written a java program to send email via SMTP. I would like to know how I can request the user to add my email Id to his address-book/Contact list in his client automatically/manually. Is there a way for me to do it from within my java code. I would like this feature as my emails will not get listed as spam/junk. I am using javamail API.
Thank you,
Nagarajan
NOT POSSIBLE!! You can't do that ,Email will be just mail.Listing your emailid and other things(spam,junk mail) to be done by receiver only.Who receive they have to configure.

Is there any way of changing gmail password programmatically using java?

Is there any way of changing gmail password programmatically using java?
The google provisioning API guide has updateUser method which takes a UserEntry object. You can use it to update the password I suppose, you will have to try it out. Check the javadocs for definition of UserEntry
I would have been very surprised if you could, and it doesn't look like it.
This page shows you the list of settings you can change in the Google Apps "Email Settings API", and change-password isn't there.
http://code.google.com/googleapps/domain/email_settings/developers_guide_protocol.html#GA_email_settings_api_ops
HTH
I think yes. You can record all operation with web pages by Selenium, test if all ok, and after export to java code - only problem can arise if Google will use CAPCHA
Uh, just use an http client to post this web page: https://www.google.com/accounts/b/0/EditPasswd. Use http://hc.apache.org/httpclient-3.x/ or something similar. You'll need to keep track of cookies properly, so google thinks you are logged in when you load the page. But yeah, obviously it is possible. If your browser can do it, you can do it programmatically through sending http requests. If you want to be double careful, you can use something like tamperdata in firefox to sniff exactly what your browser sends when you request a password change, so you don't miss any silent fields or whatnot.
Using Google Provisioning API, to change the password, you have to set it to login attribute of UserEntry object:
import sample.appsforyourdomain.AppsForYourDomainClient;
...
AppsForYourDomainClient client = new AppsForYourDomainClient(email, password, domain);
UserEntry user = client.retrieveUser("username");
user.getLogin().setPassword("newpassword");
client.updateUser("username", user);

Categories