티스토리 뷰

SWDesk/App

[Android] 메일(Mail) 전송

bizmaker 2020. 4. 8. 10:40

(2015.11.27)

구글메일(GMail)을 이용하여 메일을 전송하는 앱

소스코드 링크

설명 자료 첨부 

 darkhorizon_tistory_com_324.pdf

// Reference-link : http://darkhorizon.tistory.com/324
public class MainActivity extends Activity {
 
 TextView hTextView;
 String ExceptionString0="None";
 

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  hTextView = (TextView)findViewById(R.id.mainvalue);
  
  Button send = (Button) findViewById(R.id.send);
  
  send.setOnClickListener(new Button.OnClickListener(){
   public void onClick(View view){
    hTextView.setText(ExceptionString0);
    MailThread thread = new MailThread(hTextView);
    thread.setDaemon(true);
    thread.start();
   }
  });
 }
 
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // Handle action bar item clicks here. The action bar will
  // automatically handle clicks on the Home/Up button, so long
  // as you specify a parent activity in AndroidManifest.xml.
  int id = item.getItemId();
  if (id == R.id.action_settings) {
   return true;
  }
  return super.onOptionsItemSelected(item);
 }
 
 class MailThread extends Thread{
  TextView hTextView;
  
  MailThread(TextView tTextView){
   hTextView = tTextView;
  }

  public void run(){
   GMailSender sender = new GMailSender(ID, 2nd-password);
   try{
    sender.sendMail("Mail Subject",  "--- Mail Body--- " + Math.random()*10000, sender_mail_address, receiver_mail_address);
    ExceptionString0 = "[None]";
   }catch(Exception e){
    //hTextView.setText(e.toString());
    ExceptionString0 = e.toString();
    ;
   }

  }
 }


 class GMailSender extends javax.mail.Authenticator{
  private String mailhost = "smtp.gmail.com";
  private String user;
  private String password;
  private Session session;
  
  GMailSender(){}
  
  GMailSender(final String user, final String password){
   this.user = user;
   this.password = password;
   Properties props = new Properties();
         props.setProperty("mail.transport.protocol", "smtp");
         props.setProperty("mail.host", mailhost);
         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, new GMailAuthenticator(user, password));
  }
  
  public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception{
   Message msg = new MimeMessage(session);
   msg.setFrom(new InternetAddress(sender));
   msg.setSubject(subject);
   msg.setContent(body, "text/html;charset=EUC-KR");
   msg.setSentDate(new Date());
   msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
   Transport.send(msg);
     } 
 }
 
 class GMailAuthenticator extends Authenticator {
      String user;
      String pw;
      public GMailAuthenticator (String username, String password)
      {
         super();
         this.user = username;
         this.pw = password;
      }
     public PasswordAuthentication getPasswordAuthentication()
     {
        return new PasswordAuthentication(ID, 2nd-pPassword);

// ID@gmail.com,
     }
 }

}
반응형