0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaでHTMLメール送信

Posted at
package hello.javax.mail;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class HelloSendHtmlMail {

	public static void main(String[] args) {
		try {
			Properties props = System.getProperties();
			props.put("mail.smtp.host", "localhost");
			props.put("mail.smtp.auth","true");
			Session session = Session.getDefaultInstance(props, null);
			MimeMessage mimeMessage = new MimeMessage(session);
			// x-windows-iso2022jp
			mimeMessage.setFrom(new InternetAddress("aaa@xxx.com", "Taro Yamada", "iso-2022-jp"));
			mimeMessage.setRecipients(Message.RecipientType.TO, "bbb@xxx.com");
			// x-windows-iso2022jp
			mimeMessage.setSubject("MAIL TITLE", "iso-2022-jp");
			// x-windows-iso2022jp
			mimeMessage.setHeader("Content-Type", "text/html; charset=iso-2022-jp");
			mimeMessage.setContent("<html><body><a href=\"http://www.ibm.com/\">ibm.com</a></body></html>", "text/html; charset=iso-2022-jp");
			mimeMessage.setSentDate(new Date());
			Transport tp = session.getTransport("smtp");
			tp.connect(null, "user", "user");
			tp.sendMessage(mimeMessage, mimeMessage.getFrom());
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?