java.util.Date型を計算(1年後の日付、とか)
Dateを一旦Calenderにして計算し、そこからDateを得る。
import java.util.Calendar; import java.util.Date; public class AddDate { public static void main(String[] args) { Date now = new Date(); System.out.println(now); Calendar cal = Calendar.getInstance(); cal.setTime(now); cal.add(Calendar.YEAR, 1); Date afteryear = cal.getTime(); System.out.println(afteryear); } }
Powershellで環境変数を表示する
$env:環境変数名
Jakarta Mailを使ってメール送信(テキスト、HTML(画像なし)、HTML(画像あり))
/* * This Java source file was generated by the Gradle 'init' task. */ package htmlmail; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.BodyPart; import javax.mail.Message.RecipientType; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; public class App { public static void main(String[] args) { new App().sendHtmlMailWithImage(); } public void send() { // 送信先メールサーバの設定 Properties props = new Properties(); props.put("mail.smtp.host", "127.0.0.1"); props.put("mail.smtp.port", "2525"); Session session = Session.getInstance(props, null); // メール内容の準備 String from = "test_from@example.com"; String to = "test_to@example.com"; String subject = "テキストメッセージ"; String text = "テスト本文"; try { // メールの組み立て MimeMessage sendMessage = new MimeMessage(session); sendMessage.setFrom(new InternetAddress(from)); sendMessage.setRecipient(RecipientType.TO, new InternetAddress(to)); sendMessage.setSubject(subject, "UTF-8"); sendMessage.setText(text, "UTF-8"); // メール送信 Transport.send(sendMessage); } catch (MessagingException e) { e.printStackTrace(); } } public void sendHtmlMail() { // 送信先メールサーバの設定 Properties props = new Properties(); props.put("mail.smtp.host", "127.0.0.1"); props.put("mail.smtp.port", "2525"); Session session = Session.getInstance(props, null); // メール内容の準備 String from = "test_from@example.com"; String to = "test_to@example.com"; String subject = "HTMLメッセージ(画像なし)"; // String text = "テスト本文"; String html="<html><body><h1>Hello</h1>こんにちは</body></html>"; try { // メールの組み立て MimeMessage sendMessage = new MimeMessage(session); sendMessage.setFrom(new InternetAddress(from)); sendMessage.setRecipient(RecipientType.TO, new InternetAddress(to)); sendMessage.setSubject(subject, "UTF-8"); // sendMessage.setText(text, "UTF-8"); sendMessage.setContent(html, "text/html; charset=\"UTF-8\""); // メール送信 Transport.send(sendMessage); } catch (MessagingException e) { e.printStackTrace(); } } public void sendHtmlMailWithImage() { // 送信先メールサーバの設定 Properties props = new Properties(); props.put("mail.smtp.host", "127.0.0.1"); props.put("mail.smtp.port", "2525"); Session session = Session.getInstance(props, null); // メール内容の準備 String from = "test_from@example.com"; String to = "test_to@example.com"; String subject = "HTMLメッセージ(画像あり)"; String html="<html><body>" + "<h1>Hello</h1>こんにちは<br/>" + "<img src=\"cid:image1\"><br/>" + "<img src=\"cid:image2\"><br/>" + "</body></html>"; try { // メールの組み立て MimeMessage sendMessage = new MimeMessage(session); sendMessage.setFrom(new InternetAddress(from)); sendMessage.setRecipient(RecipientType.TO, new InternetAddress(to)); sendMessage.setSubject(subject, "UTF-8"); sendMessage.setContent(html, "text/html; charset=\"UTF-8\""); // マルチパート生成 MimeMultipart multipart = new MimeMultipart("related"); sendMessage.setContent(multipart); // ボディパート生成(本文) BodyPart msgBodyPart = new MimeBodyPart(); msgBodyPart.setContent(html, "text/html; charset=\"UTF-8\""); multipart.addBodyPart(msgBodyPart); // ボディパート生成(画像1) BodyPart image1BodyPart = new MimeBodyPart(); DataSource ds1 = new FileDataSource("/tmp/s1.jpg"); image1BodyPart.setDataHandler(new DataHandler(ds1)); image1BodyPart.setHeader("Content-ID", "<image1>"); multipart.addBodyPart(image1BodyPart); // ボディパート生成(画像2) BodyPart image2BodyPart = new MimeBodyPart(); DataSource ds2 = new FileDataSource("/tmp/s2.jpg"); image2BodyPart.setDataHandler(new DataHandler(ds2)); image2BodyPart.setHeader("Content-ID", "<image2>"); multipart.addBodyPart(image2BodyPart); // メール送信 Transport.send(sendMessage); } catch (MessagingException e) { e.printStackTrace(); } } }
GreenMailを使ってメール送信のテストを行う
GreenMail
メール送信機能の開発において、テストを実施するのに便利です。
greenmail-mail-test.github.io
build.gradle
dependencies { implementation 'com.sun.mail:jakarta.mail:1.6.7' testImplementation 'com.icegreen:greenmail:1.6.3' testImplementation 'com.icegreen:greenmail-junit5:1.6.3' testRuntimeOnly 'org.slf4j:slf4j-api:1.7.30' testRuntimeOnly 'org.slf4j:slf4j-simple:1.7.30' // This dependency is used by the application. implementation 'com.google.guava:guava:29.0-jre' testImplementation('junit:junit:4.13') }
JakartaMailTest.java
package jakarta_mail; import java.util.Properties; import javax.mail.Message.RecipientType; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import com.icegreen.greenmail.util.GreenMail; import com.icegreen.greenmail.util.ServerSetupTest; public class JakartaMailTest { private GreenMail greenMail; @Before public void before() { this.greenMail = new GreenMail(ServerSetupTest.SMTP); this.greenMail.start(); } @After public void after() { this.greenMail.stop(); } @Test public void testTransportSend() throws Exception { // 送信先メールサーバの設定 Properties props = new Properties(); props.put("mail.smtp.host", "127.0.0.1"); props.put("mail.smtp.port", "3025"); Session session = Session.getInstance(props, null); // メール内容の準備 String from = "test_from@example.com"; String to = "test_to@example.com"; String subject = "テスト件名"; String text = "テスト本文"; // メールの組み立て MimeMessage sendMessage = new MimeMessage(session); sendMessage.setFrom(new InternetAddress(from)); sendMessage.setRecipient(RecipientType.TO, new InternetAddress(to)); sendMessage.setSubject(subject, "UTF-8"); sendMessage.setText(text, "UTF-8"); // メール送信 Transport.send(sendMessage); // メール送信結果を取得 MimeMessage[] receivedMessages = this.greenMail.getReceivedMessages(); Assert.assertEquals(1, receivedMessages.length); // メール内容を確認 MimeMessage receivedMessage = receivedMessages[0]; Assert.assertEquals(1, receivedMessage.getFrom().length); Assert.assertEquals(from, receivedMessage.getFrom()[0].toString()); Assert.assertEquals(1, receivedMessage.getRecipients(RecipientType.TO).length); Assert.assertEquals(to, receivedMessage.getRecipients(RecipientType.TO)[0].toString()); Assert.assertEquals(subject, receivedMessage.getSubject()); Assert.assertEquals(text, receivedMessage.getContent().toString()); } }
Jakarta Mailを使ってメール送信
build.gradle
dependenciesに追加します。
implementation group: 'com.sun.mail', name: 'jakarta.mail', version: '2.0.1'
Javaソース
import java.util.Date; import java.util.Properties; import jakarta.mail.Message; import jakarta.mail.Session; import jakarta.mail.Transport; import jakarta.mail.internet.InternetAddress; import jakarta.mail.internet.MimeMessage; public class App { public static void main(String[] args) { Properties props = new Properties(); props.put("mail.smtp.host", "localhost"); props.put("mail.smtp.port", "2525"); // smtp4dev on docker Session session = Session.getInstance(props, null); try { MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress("example@example.com")); InternetAddress[] address = {new InternetAddress("example@example.com")}; msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject("Jakarta Mail APIs Test"); msg.setSentDate(new Date()); msg.setText("this is test message."); Transport.send(msg); } catch (Exception e) { e.printStackTrace(); } } }
開発やテスト用のダミーSMTPサーバ
smtp4dev
ダミーSMTPサーバです。
メール関連の開発、テストで便利ですね。
github.com
導入
下記情報を参考にしました。
Installation · rnwood/smtp4dev Wiki · GitHub
docker run --rm -it -p 3000:80 -p 2525:25 rnwood/smtp4dev:v3
メールを送ってみる
Power Shellでメールを送信してみます。
Send-MailMessage -To example@example.com -From example@example.com -Subject TEST -Body "This is test mail." -SmtpServer localhost -Port 2525
送ったメールの確認
ブラウザで下記URLを開きます。
http://localhost:3000
キャンバスのサイズをカメラに合わせる
キャンバスを選択し、インスペクタで以下を設定する。
- Render Modeを"Screen Space - Camera"に設定する。
- Render Cameraに対象のカメラを設定する。
- 価格: 2959 円
- 楽天で詳細を見る
たのしい2Dゲームの作り方 Unityではじめるゲーム開発入門
- 作者:STUDIO SHIN
- 発売日: 2021/02/17
- メディア: Kindle版