I want to send an email in Java.

I was looking for what to do ... Apache Commons Email looked good, so I decided to use it.

I also use Apache Commons Lang in my project I think this is more unified than putting in a strange library.

How to use ①

There is Sample code, so copy it for the time being.

Email email = new SimpleEmail();
email.setHostName("smtp.googlemail.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setSSLOnConnect(true);
email.setFrom("[email protected]");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("[email protected]");
email.send();

Looking at the sample code, it is sent using google's mail server. So, using your Google information, modify the following. --ʻUsername ........... User ID when logging in to Google --password ........... Password for logging in to Google --ʻ[email protected] ..... Google email address --" [email protected] " ...... Destination email address (* Don't use someone else's email address !!!)

And execute.

only this! simple! Great! Congratulations!

How to use ②: Attach attachment

Email email = new SimpleEmail();

If this is the case, the file cannot be attached. To attach a file ..... there is also a sample code.

// Create the attachment
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("mypictures/john.jpg ");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
attachment.setName("John");

// Create the email message
MultiPartEmail email = new MultiPartEmail();
email.setHostName("mail.myserver.com");
email.addTo("[email protected]", "John Doe");
email.setFrom("[email protected]", "Me");
email.setSubject("The picture");
email.setMsg("Here is the picture you wanted");

// add the attachment
email.attach(attachment);

// send the email
email.send();

A very simple configuration that just specifies the path where the file is located. Great.

However, with this, only one file can be attached. How can I attach multiple files? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? When I looked it up, I found an article like this. http://d.hatena.ne.jp/hiro_nemu/20091109/1257760767

It seems that you just need to new as much as you want to attach and ʻemail.attach (attachment)`. Great.

Conclusion

Apache Commons Great.

Recommended Posts

I want to send an email in Java.
I want to manually send an authorization email with Devise
rsync4j --I want to touch rsync in Java.
I want to ForEach an array with a Lambda expression in Java
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
I made an annotation in Java.
I want to build Java Applet without using an IDE
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to simplify the conditional if-else statement in Java
I want to return an object in CSV format with multi-line header & filter in Java
I want to make an ios.android app
[Java] Send an email using Amazon SES
I want to stop Java updates altogether
I want to use @Autowired in Servlet
[Ruby] I want to put an array in a variable. I want to convert to an array
[Java] I want to perform distinct with the key in the object
[Rails] I want to send data of different models in a form
Run R from Java I want to run rJava
I want to use arrow notation in Ruby
Send email using Amazon SES SMTP in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
I tried to implement deep learning in Java
I want to use java8 forEach with index
I want to pass APP_HOME to logback in Gradle
(Limited to Java 7 or later) I want you to compare objects in Objects.equals
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I tried to output multiplication table in Java
[Xcode] I want to manage images in folders
I want to get the IP address when connecting to Wi-Fi in Java
I want to be eventually even in kotlin
I want to display an error message when registering in the database
I want to write quickly from java to sqlite
How to solve an Expression Problem in Java
I tried to develop an application in 2 languages
I tried to create Alexa skill in Java
[Java Spring MVC] I want to use DI in my own class
I want to get the value in Ruby
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Javassist second decoction)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (black magic edition)
I want to use Combine in UIKit as well.
I want to use Clojure's convenient functions in Kotlin
[Java] I want to calculate the difference from the date
I tried to implement Firebase push notification in Java
I want to use fish shell in Laradock too! !!
I wrote about Java downcast in an easy-to-understand manner
# 2 [Note] I tried to calculate multiplication tables in Java.
I want to use a little icon in Rails
I tried to create a Clova skill in Java
I tried to make a login function in Java
I want to define a function in Rails Console
I want to transition screens with kotlin and java!
I tried using an extended for statement in Java
I want to stop snake case in table definition
When you want to dynamically replace Annotation in Java8
I want to click a GoogleMap pin in RSpec
I tried to implement the Euclidean algorithm in Java
~ I tried to learn functional programming in Java now ~
I want to get along with Map [Java beginner]
I tried to find out what changed in Java 9
I want to convert characters ...
I made roulette in Java.