Using Gmail aliases to link mail replies to application content - java

I have the following use case in my app:
When a specific event happens in the app all interested users should be notified by email. Then if a user replies to the email, his reply should be shown in the event page in the app.
My initial idea was to create a temp mail alias of the main notification email every time when an event happens and send the notification email with that alias set in the Reply-To header. Then if someone replies to that mail by using the alias (let's say csa123423#mydomain.com) I can figure out which event this reply refers to.
It turned out that Spring's JavaMailSender doesn't provide a way to use aliases, so I tried with Gmail API. As far as I understood creating a Gmail alias means actually setting an already existing email in your domain as an alias for another already existing email in that domain. So the Java code to achieve this using Directory API and Gmail API would look like this:
User newUser = new User();
UserName userName = new UserName();
userName.setGivenName("xsd");
userName.setFamilyName("ewrewr");
newUser.setPrimaryEmail("bbb34262bb45#mydomain.com");
newUser.setPassword("12345");
newUser.setName(userName);
User result = directoryService.users().insert(newUser).execute();
SendAs sendAs = new SendAs().setSendAsEmail("bbb34262bb45#mydomain.com").setReplyToAddress("bbb34262bb45#mydomain.com").setDisplayName("My name").setTreatAsAlias(true);
SendAs sendAsResult = gmailService.users().settings().sendAs().create(user, sendAs).execute();
MimeMessage emailContent = createEmail("mymail#gmail.com", "bbb34262bb45#mydomain.com", "Test from app", "Test body");
Message message = createMessageWithEmail(emailContent);
message = gmailService.users().messages().send(user, message).execute();
But as far as I know there are some limits on the number of accounts you can create per domain/account and also Google would charge more for this.
Is there another easier way to create aliases in Gmail? Or is there another approach to achieve the desired functionality (linking mail replies to application content) without using mail aliases?

Try leveraging '+' functionality given by Gmail for creating temporary aliases.
The basic idea is if my email id is xyz#gmail.com, I can send/receive an email with xyz+1#gmail.com or xyz+anything_here#gmail.com and it will work like a charm.
You can utilize this by keeping the alias/unique-id after the '+' in the Gmail id and then parse this alias easily in your application.

Related

How do I customise the Unsubscribe link in SendGrid?

I wish customise the position of the default Unsubscribe link that is sent by Sendgrid.
I have configured Unsubscribe groups in sendgrid and I am sending groupId with ASM, I do see the Unsubscribe from this List| Manage Email Preferences link, but the thing is, this link is directly appended to the body of the email thus making it difficult for the receiver to understand whether it is a part of the email or not, is there a way to customise this link so that it at least looks as a separate part of the email content?
ASM asmInst = new ASM();
asmInst.setGroupId(mailGroupId);
mail.setASM(asmInst);
int[] groupsToDisplay = {mailGroupId};
asmInst.setGroupsToDisplay(groupsToDisplay);
mail.setTemplateId("d-
848281f0ab8f45a7bc0e1690442a8803");
}
above is my java code, here the mail object refers to instance of Mail from com.sendgrid.Mail and the object already has subject, recipient, email body, etc. If I set the template id as above, the email received by the recipient does not contain any subject, or email body, it only has the subscribe module that is configured in the dynamic templates. Here is a screenshot of the email received.
If I don't set the templateId, the email received is proper but the unsubscribe links are directly attached to the email body. Here is the email without template

Retrive a mail from gmail inbox for a particular subject

I have a scenario where I have to read a mail sent out by a X person with a specific subject, which i will be receiving on a daily basis.
Is there any JAVA Gmail APi provided by google to retrieve the recent mail that i have received.
And also is there a way to retrieve the mail for a given date?
Yes there's a Gmail API and it has a Java client library, you can check the Quickstart to get used to it.
Now, for retrieving a list of mails you will need to use the Users.messages: list endpoint ( There's also a Java example on how to use that endpoint). Answering your question about retrieving certain emails, you will need to use the q parameter and set the values there as if you would be searching an email in the gmail search bar. I will leave you an example that you can try using the Try this API:
List emails from certain user and date.
Notice
You will only get the email's IDs, for getting more info about an email, you will have to use the Users.messages: get endpoint.

Add news email to mailjet by the REST API

I making one application that can send different email, but the email address depends of the user. I'm using mailjet to do this, after reading the doc, it's seem that i have to add every email for have the right to send email from this address, but this address are not generate by me (they are gmail, toto, etc)
I already use the Java API of mailjet to add user, and this part is working fine
But my problem is when the validation email arrive, and the person follow the link, mailjet ask to login, but he do not know what to do, because normally is my own account, i only what to add their email address to have the right to send email with them.
So the question, is how i can add email address (from gmail, yahoo...) and activate the user, without the login part.
Thanks for having choosen Mailjet to power your email!
I believe the right setup for you would be to use the Sender header. It will allow you to send email from a unique (or multiple, depending of your setup) pre-validated sender email addresses while setting the From email header to the email you want to send the email from.
In your recipients Inbox, it will display as foobar#gmail.com via notifications#mycompany.com, indicating clearly to the recipient that you're sending on behalf of foobar#gmail.com. This way, you won't be forced to validate each email address, just ones you'll
This is a very common setup for resellers and platforms sending a lot of personalised email.
In order to achieve this, please contact our [support team](https://app.mailjet.com/support] with a reference to our discussion here so they know what we're talking about. They'll guide you to the implementation it.
Hope it helps,
Best.

Send Email from one address server to another server using Java Mail API

I have an online form that allows users to email a complaint to the company. To test it I have used gmail smtp as my host. I have no problem receiving the message to the designated email account when the sender is also a gmail but I want the "From" to not be limited to just gmail accounts. It appears that smtp is only good for sending emails from the same server?
Example: My form works great if the from is abc#gmail.com and the company email is company#gmail.com.
However if xyz#yahoo.com is entered for the sender, the receiver company#gmail.com never gets it.
Any help would be greatly appreciated. I can provide my code as well if that is needed.
Your problem is a common security restriction when using SMTP. Outgoing SMTP email can only contain a "mail from" address belonging to the sender. If you break this rule, your email may be considered SPAM.
The following will allow your recipient to reply to an alternate address.
Properties properties = new Properties();
props.put("mail.smtp.from", "abc#gmail.com");
Session session = Session.getDefaultInstance(props, null);
MimeMessage m = new MimeMessage(session);
m.addFrom(InternetAddress.parse("xyz#yahoo.com"));
m.setReplyTo(InternetAddress.parse("xyz#yahoo.com"));
See also
http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/
http://www.openspf.org/Best_Practices/Webgenerated
Well you will have to own the other email as well as set it to work with gmail,
Check here for more details.
It's probably better to send the message to your company's mail server using the identity of the user who owns the application on the server, and include the information that the customer provides in the online form as data in the message you send. The message won't look like it came from the customer, but then it really didn't come from the customer since it wasn't sent using the customer's mail server.

Get email address from OpenId using Jboss/Seam

I'm using the org.jboss.seam.security.openid.OpenId class to login user's to my seam webapp. Currently I'm saving the validatedId (openid.getValidatedId()) into the database, and asking the user to provide their own email address and first and last name after logging in. I'm using Google, Yahoo, AOL, and MyOpenID for the openId Providers.
Is there any way to retrieve the email address and or first/last name of the user without having them enter this in manually?
I had a quick glance at the OpenId class in Seam 2.2.0.GA and it already contains some tentative code for retrieving the user email address.
The code already ask for an email address when the user logs in.
protected String authRequest(String userSuppliedString, String returnToUrl)
throws IOException
{
...
// Attribute Exchange example: fetching the 'email' attribute
FetchRequest fetch = FetchRequest.createFetchRequest();
fetch.addAttribute("email",
"http://schema.openid.net/contact/email", // type URI
true); // required
And there's commented code for extracting that email from the response.
public String verifyResponse(HttpServletRequest httpReq)
{
...
// AuthSuccess authSuccess =
// (AuthSuccess) verification.getAuthResponse();
// if (authSuccess.hasExtension(AxMessage.OPENID_NS_AX)) {
// FetchResponse fetchResp = (FetchResponse) authSuccess
// .getExtension(AxMessage.OPENID_NS_AX);
//
// List emails = fetchResp.getAttributeValues("email");
// String email = (String) emails.get(0);
// }
In any case you can probably use that code as a starting point.
Edit:
I managed to write a small demo based on the Seam OpenID sample. I unfortunately had to copy/paste the code from the Seam OpenId component since the existing bits of attribute-exchange code were incomplete and there's no obvious way to extend it.
I don't know if copy/pasting LGPL code is acceptable in your project. In any case Seam's OpenID component is only a thin wrapper around the openid4java library and could be rewritten easily.
Google, Yahoo, AOL, and MyOpenID
I attempted to fetch the email address and personal name of users signing-in from the four providers you mentioned. Here are the result of my little experiment.
From Google I obtain:
Gmail email address
First name
Last name
From AOL:
Email (default to AOL email but the user can type-in another)
From Yahoo:
Yahoo email address
Full Name (all in one string)
From myOpenID:
Email (if the user has filed one in his profile)
Full name (if the user has filed one in his profile)
I had to include both the http://schema.openid.net/contact/email and http://axschema.org namspaces in the request to get a response from all the providers.

Categories

Resources