Created
December 8, 2010 09:12
-
-
Save honjo2/733065 to your computer and use it in GitHub Desktop.
[java] メール送信
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.UnsupportedEncodingException; | |
import java.util.Properties; | |
import javax.mail.Message; | |
import javax.mail.MessagingException; | |
import javax.mail.Session; | |
import javax.mail.Transport; | |
import javax.mail.internet.InternetAddress; | |
import javax.mail.internet.MimeMessage; | |
public class Mail { | |
private static final String ENCODE = "ISO-2022-JP"; | |
private Session session; | |
private String smtp; | |
private String from; | |
private String fromAlias; | |
private Mail() { | |
} | |
private Mail(String smtp, String from, String fromAlias) { | |
this.smtp = smtp; | |
this.from = from; | |
this.fromAlias = fromAlias; | |
setSmtp(smtp); | |
} | |
public static Mail create(String smtp, String from, String fromAlias) { | |
return new Mail(smtp, from, fromAlias); | |
} | |
public void send(String to, String subject, String text) { | |
MimeMessage objMsg = new MimeMessage(session); | |
try { | |
objMsg.setRecipients(Message.RecipientType.TO, to); | |
final InternetAddress objFrm = new InternetAddress(from, fromAlias); | |
objMsg.setFrom(objFrm); | |
objMsg.setSubject(subject, ENCODE); | |
objMsg.setText(text, ENCODE); | |
Transport.send(objMsg); | |
} catch (UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
} catch (MessagingException e) { | |
e.printStackTrace(); | |
} | |
} | |
public String getFrom() { | |
return from; | |
} | |
public void setFrom(String from) { | |
this.from = from; | |
} | |
public String getFromAlias() { | |
return fromAlias; | |
} | |
public void setFromAlias(String fromAlias) { | |
this.fromAlias = fromAlias; | |
} | |
public String getSmtp() { | |
return smtp; | |
} | |
public void setSmtp(String smtp) { | |
Properties objPrp = new Properties(); | |
objPrp.put("mail.smtp.host", smtp); | |
session = Session.getDefaultInstance(objPrp, null); | |
} | |
public static void main(String[] args) { | |
Mail mail1 = Mail.create("smtp.***.jp", "info@***.jp", "インフォメーション"); | |
mail1.send("honjo@***.com", "あいうえお", "かきくけこ"); | |
mail1.send("hiroaki@***.jp", "abc", "def"); | |
Mail mail2 = Mail.create("smtp2.***.com", "info@***.com", "カスタマー"); | |
mail2.send("honjo2@***.com", "さしすせそ", "たちつてと"); | |
mail2.send("hiroaki2@***.jp", "ghi", "jkl"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>javax.activation</groupId> | |
<artifactId>activation</artifactId> | |
<version>1.1.1</version> | |
<scope>compile</scope> | |
</dependency> | |
<dependency> | |
<groupId>javax.mail</groupId> | |
<artifactId>mail</artifactId> | |
<version>1.4.1</version> | |
<scope>compile</scope> | |
</dependency> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment