Skip to content

Instantly share code, notes, and snippets.

@yusuke
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yusuke/9138030 to your computer and use it in GitHub Desktop.
Save yusuke/9138030 to your computer and use it in GitHub Desktop.
/*
* *
* Copyright 2014 Yusuke Yamamoto
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* Distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.security.Security;
import java.util.Date;
import java.util.Properties;
public class MailTest {
static final String USER = "user@domain.com";
static final String PASSWORD = "password";
public static void main(String[] args) throws Exception {
sendMail("main内", "main内", "yusuke@samuraism.com", "yusuke@mac.com", null, null);
new Object(){
@Override
protected void finalize() throws Throwable {
super.finalize();
sendMail("finalize内", "finalize内", "yusuke@samuraism.com", "yusuke@mac.com", null, null);
}
};
Thread.sleep(20000);
}
public static void sendMail(String subject, String body, String from, String to, String cc, String bcc) throws MessagingException, UnsupportedEncodingException {
String encoding = "UTF-8";
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Properties props = new Properties();
props.setProperty("mail.smtp.host", "smtp.gmail.com");
props.setProperty("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.debug", "true");
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USER, PASSWORD);
}
});
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setHeader("Content-Type", "text/plain; charset=" + encoding);
mimeMessage.setFrom(new InternetAddress(from, from, encoding));
mimeMessage.setRecipients(Message.RecipientType.TO, new Address[]{new InternetAddress(to)});
if (cc != null) {
mimeMessage.setRecipients(Message.RecipientType.CC
, new Address[]{new InternetAddress(cc)});
}
if (bcc != null) {
mimeMessage.setRecipients(Message.RecipientType.BCC
, new Address[]{new InternetAddress(bcc)});
}
mimeMessage.setSubject(subject, encoding);
mimeMessage.setText(body, encoding);
mimeMessage.setSentDate(new Date());
Transport.send(mimeMessage);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment