JavaMailを利用したメール送信プログラムです。いちおう添付ファイルを2つ添付できますが、ここの処理はあまり参考にしないほうがいいです。
- import java.util.*;
- import java.io.*;
- import javax.mail.*;
- import javax.mail.internet.*;
- import javax.activation.*;
-
- public class MailSender{
-
- public static final String JAPANESE_CODE = "ISO-2022-JP";
-
-
- private String to, from, host;
- private boolean debug;
-
-
- private String subject, text;
- private File file1, file2;
-
-
- public MailSender()
- {
- }
-
- public void setTo(String to)
- {
- this.to = to;
- }
-
- public String getTo()
- {
- return to;
- }
-
- public void setFrom(String from)
- {
- this.from = from;
- }
-
- public String getFrom()
- {
- return from;
- }
-
- public void setHost(String host)
- {
- this.host = host;
- }
-
- public String getHost()
- {
- return host;
- }
-
- public void setDebug(boolean b)
- {
- this.debug = b;
- }
-
- public boolean getDebug()
- {
- return debug;
- }
-
- public void setSubject(String subject)
- {
- this.subject = subject;
- }
-
- public String getSubject()
- {
- return subject;
- }
-
- public void setText(String text)
- {
- this.text = text;
- }
-
- public String getText()
- {
- return text;
- }
-
- public void setFile1(File file1)
- {
- this.file1 = file1;
- }
-
- public File getFile1()
- {
- return file1;
- }
-
- public void setFile2(File file2)
- {
- this.file2 = file2;
- }
-
- public File getFile2()
- {
- return file2;
- }
-
-
-
- public boolean send()
- {
- try
- {
- MimeMessage message = getMimeMessage();
- message.setContent( getMultipart() );
- Transport.send( message );
- return true;
- }
- catch(Exception e)
- {
- return false;
- }
- }
-
- public Properties getProperties()
- {
- Properties props = new Properties();
- props.put("mail.smtp.host", host);
- props.put("mail.host", host);
- props.put("mail.from", from);
-
- if(debug) props.put("mail.debug", debug);
-
- return props;
- }
-
- public Session getSession()
- {
- Properties props = getProperties();
- Session session = Session.getInstance(props);
- session.setDebug(debug);
- return session;
- }
-
- public MimeMessage getMimeMessage() throws Exception
- {
- MimeMessage message = new MimeMessage( getSession() );
-
- message.setFrom(new InternetAddress(from));
- InternetAddress[] address = InternetAddress.parse(to);
- message.setRecipients(Message.RecipientType.TO, address);
- message.setSubject(subject, JAPANESE_CODE);
- message.setSentDate(new Date());
-
- return message;
- }
-
- public Multipart getMultipart() throws Exception
- {
- Multipart mp = new MimeMultipart();
- mp.addBodyPart(getTextPart());
- mp.addBodyPart(getFilePart1());
- mp.addBodyPart(getFilePart2());
- return mp;
- }
-
- public MimeBodyPart getTextPart() throws Exception
- {
- MimeBodyPart part = new MimeBodyPart();
- part.setText(text, JAPANESE_CODE);
- return part;
- }
-
- public MimeBodyPart getFilePart1() throws Exception
- {
- MimeBodyPart part = new MimeBodyPart();
-
- FileDataSource fds = new FileDataSource( file1.getPath() );
- part.setDataHandler(new DataHandler( fds ));
- part.setFileName(MimeUtility.encodeWord( fds.getName() ));
-
- return part;
- }
-
- public MimeBodyPart getFilePart2() throws Exception
- {
- MimeBodyPart part = new MimeBodyPart();
-
- FileDataSource fds = new FileDataSource( file2.getPath() );
- part.setDataHandler(new DataHandler( fds ));
- part.setFileName(MimeUtility.encodeWord( fds.getName() ));
-
- return part;
- }
- }
-
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class MailSender{
//日本語の文字コード
public static final String JAPANESE_CODE = "ISO-2022-JP";
//通信情報
private String to, from, host;
private boolean debug;
//題名、本文、添付ファイル
private String subject, text;
private File file1, file2;
//Toと添付ファイル以外を初期化
public MailSender()
{
}
public void setTo(String to)
{
this.to = to;
}
public String getTo()
{
return to;
}
public void setFrom(String from)
{
this.from = from;
}
public String getFrom()
{
return from;
}
public void setHost(String host)
{
this.host = host;
}
public String getHost()
{
return host;
}
public void setDebug(boolean b)
{
this.debug = b;
}
public boolean getDebug()
{
return debug;
}
public void setSubject(String subject)
{
this.subject = subject;
}
public String getSubject()
{
return subject;
}
public void setText(String text)
{
this.text = text;
}
public String getText()
{
return text;
}
public void setFile1(File file1)
{
this.file1 = file1;
}
public File getFile1()
{
return file1;
}
public void setFile2(File file2)
{
this.file2 = file2;
}
public File getFile2()
{
return file2;
}
//メールを送信する
//戻り値:正常に送信された場合は true、そうでない場合は false
public boolean send()
{
try
{
MimeMessage message = getMimeMessage();
message.setContent( getMultipart() );
Transport.send( message );
return true;
}
catch(Exception e)
{
return false;
}
}
public Properties getProperties()
{
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.host", host);
props.put("mail.from", from);
if(debug) props.put("mail.debug", debug);
return props;
}
public Session getSession()
{
Properties props = getProperties();
Session session = Session.getInstance(props);
session.setDebug(debug);
return session;
}
public MimeMessage getMimeMessage() throws Exception
{
MimeMessage message = new MimeMessage( getSession() );
message.setFrom(new InternetAddress(from));
InternetAddress[] address = InternetAddress.parse(to);
message.setRecipients(Message.RecipientType.TO, address);
message.setSubject(subject, JAPANESE_CODE);
message.setSentDate(new Date());
return message;
}
public Multipart getMultipart() throws Exception
{
Multipart mp = new MimeMultipart();
mp.addBodyPart(getTextPart());
mp.addBodyPart(getFilePart1());
mp.addBodyPart(getFilePart2());
return mp;
}
public MimeBodyPart getTextPart() throws Exception
{
MimeBodyPart part = new MimeBodyPart();
part.setText(text, JAPANESE_CODE);
return part;
}
public MimeBodyPart getFilePart1() throws Exception
{
MimeBodyPart part = new MimeBodyPart();
FileDataSource fds = new FileDataSource( file1.getPath() );
part.setDataHandler(new DataHandler( fds ));
part.setFileName(MimeUtility.encodeWord( fds.getName() ));
return part;
}
public MimeBodyPart getFilePart2() throws Exception
{
MimeBodyPart part = new MimeBodyPart();
FileDataSource fds = new FileDataSource( file2.getPath() );
part.setDataHandler(new DataHandler( fds ));
part.setFileName(MimeUtility.encodeWord( fds.getName() ));
return part;
}
}
上記クラスの利用例です。
- public void test(){
- MailSender ms = new MailSender();
-
- ms.setFrom("");
- ms.setTo("");
- ms.setDebug(false);
- ms.setHost("");
- ms.setSubject("");
- ms.setText("");
-
- ms.setFile1( new File("") );
- ms.setFile2( new File("") );
-
- ms.send();
- }
public void test(){
MailSender ms = new MailSender();
//下記に適切なデータを記述
ms.setFrom("");
ms.setTo("");
ms.setDebug(false);
ms.setHost("");
ms.setSubject("");
ms.setText("");
//添付ファイルがある場合(任意)
ms.setFile1( new File("") );
ms.setFile2( new File("") );
//メール送信
ms.send();
}
0 件のコメント:
コメントを投稿