| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.dev.mail; |
| 7 |
import java.util.Properties; |
| 8 |
import javax.mail.Session; |
| 9 |
import javax.mail.Transport; |
| 10 |
import javax.mail.Message.RecipientType; |
| 11 |
import javax.mail.internet.MimeMessage; |
| 12 |
|
| 13 |
/** |
| 14 |
* Utility class to send a mail over a fixed gmail account. |
| 15 |
*/ |
| 16 |
public class SendMail { |
| 17 |
// http://repo2.maven.org/maven2/javax/mail/mail/1.4.1/mail-1.4.1.jar |
| 18 |
// http://repo2.maven.org/maven2/javax/activation/activation/1.1/activation-1.1.jar |
| 19 |
|
| 20 |
public static void main(String[] args) throws Exception { |
| 21 |
String to = "thomas.tom.mueller" + "@" + "gmail.com"; |
| 22 |
sendMailOverGmail("", to, "Test", "Test Mail"); |
| 23 |
} |
| 24 |
|
| 25 |
static void sendMailOverGmail(String password, String to, String subject, String body) throws Exception { |
| 26 |
String username = "testing1212123" + "@" + "gmail.com"; |
| 27 |
String host = "smtp.gmail.com"; |
| 28 |
Properties prop = new Properties(); |
| 29 |
prop.put("mail.smtps.auth", "true"); |
| 30 |
Session session = Session.getDefaultInstance(prop); |
| 31 |
session.setProtocolForAddress("rfc822", "smtps"); |
| 32 |
session.setDebug(true); |
| 33 |
MimeMessage msg = new MimeMessage(session); |
| 34 |
msg.setRecipients(RecipientType.TO, to); |
| 35 |
msg.setSubject(subject); |
| 36 |
msg.setText(body); |
| 37 |
Transport t = session.getTransport("smtps"); |
| 38 |
try { |
| 39 |
t.connect(host, username, password); |
| 40 |
t.sendMessage(msg, msg.getAllRecipients()); |
| 41 |
} finally { |
| 42 |
t.close(); |
| 43 |
} |
| 44 |
} |
| 45 |
} |