Adding to, cc,subject in Desktop.mail(uri) - java

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.

Related

Using Gmail aliases to link mail replies to application content

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.

Missing email using JavaMail on Google App Engine

I have this really weird problem and I'm not able to find a solution... I hope you can help me...
I'm working in Google App Engine to build my App (lets call it "MyApp"), to test, I have a cloned app renamed as "sandbox-MyApp".
I need to allow my users send a mail with some data, so I have a form where they can fill some information that will be added to the message.
I've working with this scenario a long time ago, but now I'm having issues, because, for some reason, my out-coming mails are not being received by the recipients...
It's a really weird thing about this, because, I can send one or two mails without problem, but after that, they suddenly stop, and after some code-changes, they work again.
I'm using Java.Mail to do the work,
I'm trying to send a simple HTML,
My "from" address is something like "userName#sandbox-myApp.appspotmail.com"
My Subject its something like: "Hello userName! There is some important message for you"
The message it's really simple, includes an image logo (served by an https://sandbox-myApp.appspot.com/img/logo.png), an invitation text and a single link to my app URL... (https://sandbox-myApp.appspot.com/)
My code it's real simple, based on the Google Documentation.
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(senderAddress, MimeUtility.encodeText(senderLabel, "UTF-8", "B"), "UTF-8"));
msg.addRecipient(javax.mail.internet.MimeMessage.RecipientType.TO, new InternetAddress(receiverAddress, receiverLabel, "UTF-8"));
if(responseAddress != null && !responseAddress.trim().isEmpty()){
msg.setReplyTo(new Address[] {
new InternetAddress(responseAddress, MimeUtility.encodeText(senderLabel, "UTF-8", "B"))
});
}
msg.setSubject(MimeUtility.encodeText(subject, "UTF-8", "B"), "UTF-8");
msg.setContent(msgBody, "text/html;charset=UTF-8");
Transport.send(msg);
I've tried changing "from" to something like "app_admin#mydomain.com" and it works for a while, but after some mails (about 5 or 6), stop working too.
Most shocking thing: There aren't any error message on logs... The Cuota Viewer counts every sent mail (so I suppose it must being blocked somewhere else),
I've modiffied the message to omit any URL on the body and It works better, but I need to include it!.
Problem is tracked on https://code.google.com/p/googleappengine/issues/detail?id=12786
Workaround which worked for my application is to not use appspot.com domain.
Register custom domain for the application and then mails to the application using the custom domain work.

add footer/signature to mail in Java

In one of my Java applications I have to forward e-mails. So, I get e-mails (plain text or multipart) with any content (maybe also attachments). I edit their subject, from- and to-header and send them via SMTP.
I already implemented this using Apache James Mime4j and Apache Commons Net but now I also have to append a footer/signature to the content of each e-mail.
Can I achieve this with Mime4j too? Would be great! How? If not: is there another way?
EDIT: Details after Wolfgang Fahl's comment:
Maybe I'm using the library the wrong way, but I am parsing the message as follows:
MessageBuilder messageBuilder = new DefaultMessageBuilder();
InputStream in = ...
Message m = messageBuilder.parseMessage(in);
Now I have an instance of Message and I can set it's subject, sender, etc. But Message does not provide a method setText() or something like that. Ok, there is getBody() but then I don't know how to manipulate the Body.

Some questions related to implementation of image inside email signature?

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");

Java: Open default mail application and create new mail and populate To and Subject fields

Just wondering if anyone can help me with a problem I've come across in Java.
Is there functionality within Java to produce a section of code that will open the default email application on a user's PC? (I guess almost like a fancy mailto link...)
If there is - is it possible to populate fields such as the To and Subject fields?
Thanks,
Mike.
Desktop.mail(URI mailtoURI) is your friend!
Javadoc:
Launches the mail composing window of the user default mail client, filling the message fields specified by a mailto: URI.
A mailto: URI can specify message fields including "to", "cc", "subject", "body", etc. See The mailto URL scheme (RFC 2368) for the mailto: URI specification details.
Example Code:
Desktop desktop;
if (Desktop.isDesktopSupported()
&& (desktop = Desktop.getDesktop()).isSupported(Desktop.Action.MAIL)) {
URI mailto = new URI("mailto:john#example.com?subject=Hello%20World");
desktop.mail(mailto);
} else {
// TODO fallback to some Runtime.exec(..) voodoo?
throw new RuntimeException("desktop doesn't support mailto; mail is dead anyway ;)");
}

Categories

Resources