I'm building a ImageHtmlEmail in order to download and embed all the images from given HTML into a multipart email. I need to store that email for sending later.
Problem is, I can get the resulting email text and content-type, but I see no means to construct an ImageHtmlEmail back from a text and a content-type. Is it possible at all? Or should I go with raw javax.mail for actual sending?
I was able to create javax.mail.internet.MimeMessage out of String representation of an email (ASCII dump as one one get on downloading original email from gmail). However not been very successful in constructing Email subclasses like HtmlEmail from it yet.
MimeMessage mimeMessage = MimeMessageUtils.createMimeMessage(Session.getDefaultInstance(new Properties()), new ByteArrayInputStream(oneEmail.toString().getBytes()));
It does give me most of the things I can think of as useful using its getter methods.
Not sure what you mean with "building HtmlEmail from string", but constructing an ImageHtmlMail should be fairly easy, see the sample at http://commons.apache.org/email/userguide.html, all you need to provide is some html-text via setHtmlMsg()
import org.apache.commons.mail.HtmlEmail;
...
// load your HTML email template
String htmlEmailTemplate = ....
// define you base URL to resolve relative resource locations
URL url = new URL("http://www.apache.org");
// create the email message
HtmlEmail email = new ImageHtmlEmail();
email.setDataSourceResolver(new DataSourceResolverImpl(url));
email.setHostName("mail.myserver.com");
email.addTo("jdoe#somewhere.org", "John Doe");
email.setFrom("me#apache.org", "Me");
email.setSubject("Test email with inline image");
// set the html message
email.setHtmlMsg(htmlEmailTemplate);
// set the alternative message
email.setTextMsg("Your email client does not support HTML messages");
// send the email
email.send();
Related
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
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 would like to know if there is a way to send an email with attachment file with GWT.
I managed to send a simple email without attachment, but I am having a problem when I try to add a file.
The problem is that "FileUpload" don't give the fullpath of the file
it seems for safety reasons it is impossible to retrieve the full path of the file from the client.
Is there another way keeping the logical server in gwt client?
My code
Client side:
FileUpload upload = new FileUpload();
// cannot retrieve the full path
String fileAttachment = upload.getName();
Server side:
public void sendMail(String sender, String[] recipients, String subject, String message, String fileAttachment) {
try {
...(init)
// Part two is attachment
messageBodyPart = new MimeBodyPart();
// => fileAttachment need full path
DataSource source =
new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(
new DataHandler(source));
messageBodyPart.setFileName(fileAttachment);
multipart.addBodyPart(messageBodyPart);
// Put parts in message
msg.setContent(multipart);
// Send
Transport.send(msg);
}
Thanks for your help
You have to actually upload the file to the server.
The easiest way in GWT is to put your FileUpload (and all your form input widgets) in a FormPanel; it has the drawback of making error handling (and response handling from the server) more difficult though.
An alternative, in recent browsers, is to get the File object (not a java.io.File, a JS object) out of the FileUpload and upload it using XMLHttpRequest (possibly coupled with FormData to also send the other form values). In GWT, that means using JSNI (it might be possible to use the Elemental library too), and really know the innards of what you're doing.
In any case, you won't be able to use GWT-RPC to talk to your server and send the file at the same time.
I am a beginner of using Desktop.mail(URI) class, so I am looking for a way to add to, cc and subject to the mail when triggered from the program.
String mailTo = "test#domain.com";
String cc = "test2#domain.com";
String subject = "firstEmail";
String body = "the java message";
URI uriMailTo = new URI(mailTo,cc,subject,body);
Desktop desktop;
desktop = Desktop.getDesktop();
desktop.mail(uriMailTo);
can any one suggest any tutorials to learn this process, because I am looking for even more functions like receiving the data back from the outlook to the Java program.
Thanks in advance for help!
The Desktop.mail() function is a utility method for launching whatever mail program may exist in the users system (if any). You have (very) limited capability of controlling the actual mail message to be (eventually) sent, and once the mail client is displayed you're pretty much done - aka you wont be getting any feedback on what message was actually sent or wether it succeeded.
If you need this level of control then you should be using the JavaMail API, which does a lot of what you seem to need.
If you are stuck with using the Desktop mail client, then you might want to read up on RFC 2368. It describes all the fields that can be included in a mailto URI. So, you will be able to populate the message, but you won't get feedback on wether it was successfully sent or not:
mailto:joe#example.com?cc=bob#example.com&body=hello+world
A code example of constructing your URI (which is incorrect btw):
final String mailURIStr = String.format("mailto:%s?subject=%s&cc=%s&body=%s",
mailTo, subject, cc, body);
final URI mailURI = new URI(mailURIStr);
Where the substituted should be URL encoded if necessary.
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");