Java-sending-email
Java-メールの送信
Javaアプリケーションを使用して電子メールを送信するのは非常に簡単ですが、最初に JavaMail API と* Java Activation Framework(JAF)*をマシンにインストールする必要があります。
- Javaの標準Webサイトからhttps://java.sun.com/products/javamail/[JavaMail(Version 1.2)]の最新バージョンをダウンロードできます。
- 最新バージョンのhttps://java.sun.com/products/javabeans/glasgow/jafl[JAF(Version 1.1.1)]は、Javaの標準Webサイトからダウンロードできます。
これらのファイルをダウンロードして解凍すると、新しく作成された最上位ディレクトリに、両方のアプリケーションの多数のjarファイルがあります。 CLASSPATHに mail.jar および activation.jar ファイルを追加する必要があります。
簡単なメールを送信する
以下は、マシンから簡単な電子メールを送信する例です。 localhost はインターネットに接続されており、電子メールを送信するのに十分な能力があると想定されています。
例
//File Name SendEmail.java
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail {
public static void main(String [] args) {
//Recipient's email ID needs to be mentioned.
String to = "[email protected]";
//Sender's email ID needs to be mentioned
String from = "[email protected]";
//Assuming you are sending email from localhost
String host = "localhost";
//Get system properties
Properties properties = System.getProperties();
//Setup mail server
properties.setProperty("mail.smtp.host", host);
//Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try {
//Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
//Set From: header field of the header.
message.setFrom(new InternetAddress(from));
//Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
//Set Subject: header field
message.setSubject("This is the Subject Line!");
//Now set the actual message
message.setText("This is actual message");
//Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
このプログラムをコンパイルして実行し、簡単な電子メールを送信します-
出力
$ java SendEmail
Sent message successfully....
複数の受信者に電子メールを送信する場合は、次の方法を使用して複数の電子メールIDを指定します-
void addRecipients(Message.RecipientType type, Address[] addresses)
throws MessagingException
ここにパラメータの説明があります-
- type -これは、TO、CC、またはBCCに設定されます。 ここで、CCはカーボンコピーを表し、BCCはブラックカーボンコピーを表します。 例:Message.RecipientType.TO
- アドレス-これは電子メールIDの配列です。 電子メールIDを指定するときにInternetAddress()メソッドを使用する必要があります。
HTML形式の電子メールを送信する
以下は、マシンからHTML形式の電子メールを送信する例です。 ここでは、 localhost がインターネットに接続されており、電子メールを送信するのに十分な能力があると想定されています。
この例は前の例と非常に似ていますが、ここではsetContent()メソッドを使用して、2番目の引数が「text/html」であるコンテンツを設定し、HTMLコンテンツがメッセージに含まれることを指定します。
この例を使用すると、好きな大きさのHTMLコンテンツを送信できます。
例
//File Name SendHTMLEmail.java
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendHTMLEmail {
public static void main(String [] args) {
//Recipient's email ID needs to be mentioned.
String to = "[email protected]";
//Sender's email ID needs to be mentioned
String from = "[email protected]";
//Assuming you are sending email from localhost
String host = "localhost";
//Get system properties
Properties properties = System.getProperties();
//Setup mail server
properties.setProperty("mail.smtp.host", host);
//Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try {
//Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
//Set From: header field of the header.
message.setFrom(new InternetAddress(from));
//Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
//Set Subject: header field
message.setSubject("This is the Subject Line!");
//Send the actual HTML message, as big as you like
message.setContent("<h1>This is actual message</h1>", "text/html");
//Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
このプログラムをコンパイルして実行し、HTML電子メールを送信します-
出力
$ java SendHTMLEmail
Sent message successfully....
添付ファイルを電子メールで送信する
以下は、マシンから添付ファイル付きの電子メールを送信する例です。 ここでは、 localhost がインターネットに接続されており、電子メールを送信するのに十分な能力があると想定されています。
例
//File Name SendFileEmail.java
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendFileEmail {
public static void main(String [] args) {
//Recipient's email ID needs to be mentioned.
String to = "[email protected]";
//Sender's email ID needs to be mentioned
String from = "[email protected]";
//Assuming you are sending email from localhost
String host = "localhost";
//Get system properties
Properties properties = System.getProperties();
//Setup mail server
properties.setProperty("mail.smtp.host", host);
//Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try {
//Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
//Set From: header field of the header.
message.setFrom(new InternetAddress(from));
//Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
//Set Subject: header field
message.setSubject("This is the Subject Line!");
//Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
//Fill the message
messageBodyPart.setText("This is message body");
//Create a multipar message
Multipart multipart = new MimeMultipart();
//Set text message part
multipart.addBodyPart(messageBodyPart);
//Part two is attachment
messageBodyPart = new MimeBodyPart();
String filename = "file.txt";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
//Send the complete message parts
message.setContent(multipart );
//Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
このプログラムをコンパイルして実行し、HTML電子メールを送信します-
出力
$ java SendFileEmail
Sent message successfully....
ユーザー認証部
認証のためにユーザーIDとパスワードを電子メールサーバーに提供する必要がある場合は、次のようにこれらのプロパティを設定できます-
props.setProperty("mail.user", "myuser");
props.setProperty("mail.password", "mypwd");
電子メール送信メカニズムの残りの部分は、上記で説明したように残ります。