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.
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.
........... 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 !!!)
only this! simple! Great! Congratulations！
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.
Apache Commons Great.