たま

技術的なメモ置き場。主にJava。

JavaMailでhotmailの送信を行う

JavaMailでhotmailの送信を行う


Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.live.com");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");

Session session = Session.getDefaultInstance(props);

MimeMessage mimeMsg = new MimeMessage(session);
mimeMsg.setRecipients(Message.RecipientType.TO, "送信先メールアドレス");
InternetAddress fromHeader = new InternetAddress("アカウント", "送信者名");
mimeMsg.setFrom(fromHeader);
mimeMsg.setSubject("件名", "ISO-2022-JP");
mimeMsg.setSentDate(new Date());
mimeMsg.setText("内容", "ISO-2022-JP");

Transport trans = session.getTransport("smtp");
trans.connect("smtp.live.com", 25, "アカウント", "パスワード");
trans.sendMessage(mimeMsg, mimeMsg.getAllRecipients());
trans.close();