티스토리 뷰
(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,
}
}
}
반응형
'SWDesk > App' 카테고리의 다른 글
[BPM] App source for V1.1 (0) | 2020.05.08 |
---|---|
[Android] 쓰레드(Thread)간 통신을 위한 핸들러(Handler) 사용 예제 (0) | 2020.04.19 |
[Android] 암시적 인텐트(Intent)를 이용한 메일 전송 (0) | 2020.04.07 |
[Android] 핸들러(Handler)와 루퍼(Looper) 테스트 (0) | 2020.03.23 |
[Android] TCP/IP 소켓 통신 (0) | 2020.03.16 |
반응형
250x250
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 혁신
- 심심풀이치매방지기
- arduino
- Innovations&Hurdles
- 전압
- BSC
- Innovation&Hurdles
- badp
- 치매
- Decorator
- ServantClock
- 오블완
- 전압전류모니터링
- DYOV
- 둎
- 전류
- Video
- 허들
- 배프
- 심심풀이
- 절연형
- bilient
- Hurdles
- 티스토리챌린지
- 빌리언트
- image
- Innovations
- 혁신과허들
- 아두이노
- 치매방지
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
글 보관함