メール送信コードの例
6245 ワード
public class Authen extends Authenticator {
String userName=null;
String password=null;
public Authen(){
}
public Authen(String username, String password) {
this.userName = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(userName, password);
}
}
public class SenderInfo {
private String mailServerHost;
private String mailServerPort = "25";
//
private String fromAddress;
//
private String toAddress;
//
private String userName;
private String password;
//
private boolean validate = true;
//
private String subject;
//
private String content;
//
private String[] attachFileNames;
/**
*
*/
public Properties getProperties(){
Properties p = new Properties();
p.put("mail.smtp.host", this.mailServerHost);
p.put("mail.smtp.port", this.mailServerPort);
p.put("mail.smtp.auth", validate ? "true" : "false");
return p;
}
public String getMailServerHost() {
return mailServerHost;
}
public void setMailServerHost(String mailServerHost) {
this.mailServerHost = mailServerHost;
}
public String getMailServerPort() {
return mailServerPort;
}
public void setMailServerPort(String mailServerPort) {
this.mailServerPort = mailServerPort;
}
public boolean isValidate() {
return validate;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
public String[] getAttachFileNames() {
return attachFileNames;
}
public void setAttachFileNames(String[] fileNames) {
this.attachFileNames = fileNames;
}
public String getFromAddress() {
return fromAddress;
}
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getToAddress() {
return toAddress;
}
public void setToAddress(String toAddress) {
this.toAddress = toAddress;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String textContent) {
this.content = textContent;
}
}
/**
* ( )
*/
public class SimpleMailSender {
/**
*
*
* @param mailInfo
*
*/
public boolean sendTextMail(SenderInfo mailInfo) {
//
Authen authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
// ,
authenticator = new Authen(mailInfo.getUserName(), mailInfo.getPassword());
}
// session
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
try {
// session
Message mailMessage = new MimeMessage(sendMailSession);
//
Address from = new InternetAddress(mailInfo.getFromAddress());
//
mailMessage.setFrom(from);
// ,
Address to = new InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO, to);
//
mailMessage.setSubject(mailInfo.getSubject());
//
mailMessage.setSentDate(new Date());
//
String mailContent = mailInfo.getContent() ;
mailMessage.setText(mailContent);
//
Transport.send(mailMessage);
Log.i("sendMail", "sendMail ");
return true;
} catch (MessagingException ex) {
Log.e("sendMail", ex.getMessage());
}
return false;
}
/**
* HTML
*
* @param mailInfo
*
*/
public static boolean sendHtmlMail(SenderInfo mailInfo) {
//
Authen authenticator = null;
Properties pro = mailInfo.getProperties();
// ,
if (mailInfo.isValidate()) {
authenticator = new Authen(mailInfo.getUserName(), mailInfo.getPassword());
}
// session
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
try {
// session
Message mailMessage = new MimeMessage(sendMailSession);
//
Address from = new InternetAddress(mailInfo.getFromAddress());
//
mailMessage.setFrom(from);
// ,
Address to = new InternetAddress(mailInfo.getToAddress());
// Message.RecipientType.TO TO
mailMessage.setRecipient(Message.RecipientType.TO, to);
//
mailMessage.setSubject(mailInfo.getSubject());
//
mailMessage.setSentDate(new Date());
// MiniMultipart , MimeBodyPart
Multipart mainPart = new MimeMultipart();
// HTML MimeBodyPart
BodyPart html = new MimeBodyPart();
// HTML
html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
// MiniMultipart
mailMessage.setContent(mainPart);
//
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
}