'mail'에 해당되는 글 1건

  1. 2011.01.24 안드로이드에서 e-mail 보내기
posted by Zetty zetty 2011.01.24 10:23

  

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.search.SubjectTerm;
import android.util.Log;
public class GmailSender extends javax.mail.Authenticator {
 private String TAG = "GmailSender.class";
 private String mailhost = "smtp.gmail.com";
 private String user;
 private String password;
 private Session session;
 private String source_folder = "Inbox";
 private String target_folder = "StealthCam";
 private String attachFile;
 public GmailSender(String user, String password) {
  this.user = user;
  this.password = password;
  Properties props = new Properties();
 
  //for smtp
  props.setProperty("mail.transport.protocol", "smtp");
  props.setProperty("mail.host", mailhost);
  props.put("mail.smtp.value", "true");
  props.put("mail.smtp.auth", "true");
  props.put("mail.smtp.port", "465");
  props.put("mail.smtp.socketFactory.port", "465");
  props.put("mail.smtp.socketFactory.class",
    "javax.net.ssl.SSLSocketFactory");
  props.put("mail.smtp.socketFactory.fallback", "false");
  props.setProperty("mail.smtp.quitwait", "false");
  session = Session.getDefaultInstance(props, this);
 }
 protected PasswordAuthentication getPasswordAuthentication() {
  Log.e("google sender ", "getPasswordAuthentication");
  return new javax.mail.PasswordAuthentication(user, password);
 }
 public synchronized void sendMail(String subject, String body,
   String sender, String recipients, String label) throws Exception {
  MimeMessage message = new MimeMessage(session); 
  message.setSender(new InternetAddress(sender));
  message.setSubject(subject);
  message.setSentDate(new Date());
 
  //email text
  MimeBodyPart messagePart = new MimeBodyPart();
  messagePart.setText("test");
 
  //attach file
  MimeBodyPart attachmentPart = new MimeBodyPart();
  FileDataSource fds = new FileDataSource(attachFile){
   @Override
   public String getContentType(){
    return "application/octet-stream";
   }
  };
  attachmentPart.setDataHandler(new DataHandler(fds));
  attachmentPart.setFileName(fds.getName());
 
  Multipart multipart = new MimeMultipart();
  multipart.addBodyPart(messagePart);
  multipart.addBodyPart(attachmentPart);
 
  message.setContent(multipart);
 
 
  MimeMultipart mp = new MimeMultipart();
  mp.setSubType("related");
 
  MimeBodyPart mbp1= new MimeBodyPart();
  String html =
    " html>"+
    " head> title> /title> /head>"+
    " body>"+
    " b> see the following jpg : it is a car! /b> br>"+
    " a href=a.jsp>hello /a> br>"+
    " IMG SRC=cid:23abc@pc27 width=80% height=60%> br>"+
    " b> end of jpg /b>"+
    " /body>"+
    " /html>";
  mbp1.setContent(html,"text/html");
 
  MimeBodyPart mbp2 = new MimeBodyPart();
  FileDataSource fds = new FileDataSource(attachFile);
  mbp2.setFileName(fds.getName());
  mbp2.setText("This is a beautiful car !");
  mbp2.setDataHandler(new DataHandler(fds));
  mbp2.setHeader("Content-ID","<imag23abc@pc27");
  mbp1.setHeader("Content-Type", "image/jpeg; name=" + fds.getName());
  mp.addBodyPart(mbp1);
  mp.addBodyPart(mbp2);
  message.setContent(mp);

  message.setSentDate(new Date());
  target_folder = label;
  if (recipients.indexOf(',') > 0) {
   message.setRecipients(Message.RecipientType.TO, InternetAddress
     .parse(recipients));
  } else {
   message.setRecipient(Message.RecipientType.TO, new InternetAddress(
     recipients));
  }
  Log.e("google sender ", "getPasswordAuthentication");
//  message.setHeader("content-type", "text/html;charset=UTF-8");
  //sending mail
  Transport.send(message);
 
  //gmail일 경우에만  폴더 이동
  if (!recipients.substring(recipients.indexOf("@")+1).equals("gmail.com")){
   return;
  }
  //move folder used imap
  Store store = null;
  // store = session.getStore(urln);
  try {
   store = session.getStore("imaps");
   store.connect();
   Log.d("GmailSender", "connect store..." + store);
   Folder sourceFolder = store.getFolder(source_folder);
   Folder targetFolder = store.getFolder(target_folder);
  
   sourceFolder.open(Folder.READ_WRITE);
   if (!targetFolder.exists()) {
    targetFolder.create(Folder.HOLDS_MESSAGES);
    Log.d("GmailSender", "crate folder zetty");
   }
   if (targetFolder.exists()) {
    SubjectTerm term = new SubjectTerm("[StealthCam]");
    Message[] messages = sourceFolder.search(term);
    Log.d(TAG, "sourceFolder count" + sourceFolder.getMessageCount());
    Log.d(TAG, "search count" + messages.length);
    sourceFolder.setFlags(messages, new Flags(Flags.Flag.DELETED),true);
    sourceFolder.copyMessages(messages, targetFolder);   
   }
   sourceFolder.close(false);
  } catch (NoSuchProviderException e) {
   e.printStackTrace();
  } catch (MessagingException e) {
   e.printStackTrace();
  } catch (Exception e) {
   e.printStackTrace();
  }
  store.close();
 }
 public void setAttachFile(String attachFile){
  this.attachFile = attachFile;
 }
 public class ByteArrayDataSource implements DataSource {
  private byte[] data;
  private String type;
  public ByteArrayDataSource(byte[] data, String type) {
   super();
   this.data = data;
   this.type = type;
  }
  public ByteArrayDataSource(byte[] data) {
   super();
   this.data = data;
  }
  public void setType(String type) {
   this.type = type;
  }
  @Override
  public String getContentType() {
   if (type == null) {
    return "application/octet-stream";
   } else {
    return type;
   }
  }
  @Override
  public InputStream getInputStream() throws IOException {
   return new ByteArrayInputStream(data);
  }
  @Override
  public String getName() {
   return "ByteArrayDataSource";
  }
  @Override
  public OutputStream getOutputStream() throws IOException {
   throw new IOException("Not Supported");
  }
 }
}
신고

티스토리 툴바