I found some sources to read mails from gmail like: https://javahowtos.com/guides/127-libraries/366-how-to-read-gmail-using-java-and-javax-mail.html
It works super but I want to add label when I read the mail. You know that we can add labels in gmail https://support.google.com/mail/answer/118708?hl=en&co=GENIE.Platform%3DDesktop
Is it possible with javax mail?
Used gmap to solve it: https://mvnrepository.com/artifact/com.sun.mail/gimap
GmailMessage gmsg = (GmailMessage) message;
gmsg.setLabels(new String[]{"Label1"}, true);
Related
I tried to use Java mail API to send a gmail with starred. I read in many places and I added code as follows:-
if (mailHighPriority) {
msg.setHeader("X-Priority", "1");
msg.setHeader("Importance", "high");
msg.setHeader("priority", "Urgent");
msg.setHeader("x-msmail-priority", "high");
}
However, I am not able to set the Gmail as starred. Is there any other option for this? Thanks
STARRED is a label used by gmail. You can't set a label when sending a mail.
You need to set up a filtering rule or use the gmail api to apply labels to messages.
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.
I am using SendGrid for my transactional mails. When I just send the mail without using template using SendGrid JAVA classes, it works fine. However when I try to send it using the sendgrid template, it does not send a mail. My code is,
SendGrid sendGrid = new SendGrid(sendGridAPIKey);
SendGrid.Email sendGridMail = new SendGrid.Email();
sendGridMail.setTo(new String[] {email});
sendGridMail.setFrom("info#from.com");
sendGridMail.setSubject("Welcome to our portal");
sendGridMail.setText("Welcome Text");
sendGridMail.addFilter("templates", "enable", "1");
sendGridMail.addFilter("templates", "template_id", "TEMPLATE_ID");
sendGridMail.addSubstitution(":firstName", new String[] { firstName });
sendGridMail.addSubstitution(":email", new String[] { email });
SendGrid.Response response = sendGrid.send(sendGridMail);
The SendGrid template (with id TEMPLATE_ID) content looks as below,
Dear -firstName-,
Welcome to our portal. Your username is -email- and password is xxxxx. Please login to our portal with these credentials.
Thanks.
I am not sure what is going wrong as there is no exception thrown, the calls succeed, but the mail is not received.
Is there any way to track the failures in SendGrid admin console?
Is there any setting I need to do to get this working?
Is there anything wrong in the code I am using?
Any help would be greatly appreciated.
Thanks in advance,
Kari...
I was reading about what is the best way about sending email through play framework (java). I have found this: https://github.com/playframework/play-mailer and I followed the instructions. I use gmail.
Here is what I added into application.conf:
play.mailer.host="smtp.gmail.com"
play.mailer.port=587
play.mailer.ssl=yes
play.mailer.tls=no
play.mailer.user="blabla#gmail.com"
play.mailer.password="blabla"
and here is my controller:
Email email = new Email();
email.setSubject("Confirmation");
email.setFrom("Mister FROM <test#gmail.com>");
email.addTo("Miss TO <test2#gmail.com>");
email.setBodyText("A text message");
email.setBodyHtml("<html><body><p>An <b>html</b> message</p></body></html>");
mailerClient.send(email);
Everything compiles with no problems, but when I run it I get this exception:
[EmailException: Sending the email to the following server failed : smtp.gmail.com:587] at mailerClient.send(email);
You have to change the change the gmail configuration also .
like-
Go to your gmail account then in setting section go to the "Forwarding and POP/IMAP" tab then enable "Enable IMAP".
Hope this will help if you are sending the correct credential.
i need to implement the email signature with image.As of now we only support the text in email signature which is already working.i need to provide the functionality
where i can insert the image inside mail signature. i can send the email to user within myapplication and also to user on external mail domain like gmail,yahoo etc. When
mail is sent to some user with in my application system, system makes entryt o DB and when receiver receives in inbox (which internally read the mail from db). Now if user
send the mail to external user on gmail it makes use of javax mail api . Similary i can receive the email from external mail domains(gmail,yahoo etc) Now i have
few questions based on tis requirement:-
1)Is there any standard for how the external mail domains like gmail send the image inside signature to another domains like (my application mail domain)?
Another point related to it gmail user can have two images ,one for signature and another image inside body. How will i determine which image belongs to
signature? Is there any defined property for that?
2)Also not able to make out what is the best/consistent approach to send(whether to internal application user or external mail domain user ) the email signature containing
image so that it renders correctly when user receives it?
what I had in my mind for point 2:- i earlier thought i can use solution suggested at How to display an image in jsp?. where
with tag <.img src="/getImage.action?imageId=123">, i can fetch the image from db in action class or servlet and return. But keeping in mind
once i send the mail to the user on gmail , he will not be able to access the servlet.So this approach does not seems to fit in requirement.
Then i came across the another great stackoverflow link base64 encoded images in email signatures where
solution by Tim Medora looked great but again the comment below the solution Gmail doesn't seem to support it again ended my Folks
really i think i should be done if mail domain like gmail,yahoo support the solution suggested by because in that case i can send image as base64 string instead
of image as attachment.
Folks would be really grateful if you can provide me some pointer/approach regarding both points 1 and 2
To include images in the email message, first you have to include the images as MIME attachments in the email. Each of these attachments must have a "Content-ID" header.
--f46d0444ea0d6991ba04b91c92e6
Content-Type: image/gif; name="theImage.gif"
Content-Transfer-Encoding: base64
Content-ID: <theImage#abcd>
[base64 string]
--f46d0444ea0d6991ba04b91c92e6--
2) Then, in the email message, include the Content-ID in the src attribute of the <img> tag.
<img src="cid:theImage#abcd" />
For Gmail to see the embedded image from byte array, I posted an answer on another similar question which is to use ByteArrayDataSource and embed it to the HtmlEmail. Here's the code snippet:
import javax.mail.util.ByteArrayDataSource;
import org.apache.commons.mail.ImageHtmlEmail;
...
ImageHtmlEmail email = new ImageHtmlEmail();
byte[] qrImageBytes = createQRCode(); // get your image byte array
ByteArrayDataSource qrImageDataSource = new ByteArrayDataSource(qrImageBytes, "image/png");
String contentId = email.embed(qrImageDataSource, "QR Image");