Java - 電子メールの送信
Java アプリケーションを使用して電子メールを送信するのは簡単ですが、最初に JavaMail API が必要です。 および Java Activation Framework (JAF)
- <リ>
Java の標準 Web サイトから JavaMail の最新バージョン (バージョン 1.2) をダウンロードできます。
<リ>最新バージョンの JAF (バージョン 1.1.1) は、Java の標準 Web サイトからダウンロードできます。
これらのファイルをダウンロードして解凍します。新しく作成された最上位ディレクトリに、両方のアプリケーション用の多数の jar ファイルがあります。 mail.jar を追加する必要があります および activation.jar CLASSPATH 内のファイル。
シンプルなメールを送る
これは、マシンから簡単な電子メールを送信する例です。 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 = "abcd@gmail.com";
// Sender's email ID needs to be mentioned
String from = "web@gmail.com";
// 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
パラメータの説明は次のとおりです-
- <リ>
タイプ − これは、TO、CC、または BCC に設定されます。ここで、CC は Carbon Copy を表し、BCC は Black Carbon Copy を表します。例: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 = "abcd@gmail.com";
// Sender's email ID needs to be mentioned
String from = "web@gmail.com";
// 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 = "abcd@gmail.com";
// Sender's email ID needs to be mentioned
String from = "web@gmail.com";
// 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");
電子メール送信メカニズムの残りの部分は、上で説明したとおりです。
Java