안녕하세요 기존 자바 프로젝트에서 사용하던 소스를 활용하여
안드로이드에 적용시키는데
아래와 같이 IOExection 이 발생하면서 발송이 안되는데요..;;
자바 프로젝트로 다시 테스트해봤지만 잘 되길레 문의 올립니다..ㅡㅡ;
대략 소스는
<!--------------------------MailTest.java ---------------------------->
public class MailTest {
public void sendEmail(String from, String to, String cc, String subject,
String content, String file , String fileName) throws Exception {
Properties props = new Properties();
// G-Mail SMTP ?ъ슜??
props.put("mail.transport.protocol", "smtp");//
props.put("mail.smtp.host", "smtp.gmail.com");// gmail SMTP
props.put("mail.smtp.starttls.enable", "true");
props.setProperty("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator();
Session mailSession = Session.getDefaultInstance(props, auth);
// create a message
Message msg = new MimeMessage(mailSession);
// set the from and to address
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
msg.setSentDate(new Date());
MimeBodyPart mbp01 = new MimeBodyPart();
mbp01.setText(content);
// <!-- 파일 첨부하기 -->
MimeBodyPart attachPart = new MimeBodyPart();
// attachPart.setDataHandler(new DataHandler(attachment, "text/xml"));
// attachPart.setFileName("XBRL INSTANCE"); // 파일명
// //// <!-- 만약 파일시스템의 파일을 첨부한다면
// ///////////////////////////////////////////////////////////////
attachPart.setDataHandler(new DataHandler(new FileDataSource(new File(
file))));
attachPart.setFileName(fileName);
// Setting the Subject and Content Type
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mbp01);
multipart.addBodyPart(attachPart);
msg.setContent(multipart);
Transport.send(msg);
}
public class SMTPAuthenticator extends Authenticator {
protected PasswordAuthentication getPasswordAuthentication() {
String username = user@gmail.com;
String password = "1234"; ;
return new PasswordAuthentication(username, password);
}
}
MainActivity.java
... 중략
public void runMailI(){
String from = "user@gmail.com"; // 메일 보내는 사람
String to = "ksy@d2.co.kr; // 메일 보낼사람
String ch = "gest@gmail.com";
String subject = "잘가는지 테스트 중~~~~~~~~~~";// 제목
String content = "안녕하세요";
try {
MailTest mt = new MailTest();
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyyMMdd_HHmmss");
Date date = new Date(System.currentTimeMillis());
String currentTime = dateFormat.format(date);
String currentDate = currentTime.substring(0, 8);
String path = Environment.getExternalStorageDirectory()
.toString(); //sd카드 경로
String file = path + "/" + "LOG_"+currentDate + ".txt"; //sd 카드의 파일 경로
// 메일보내기
mt.sendEmail(from, to, ch, subject, content,
file, currentDate);
// mt.sendEmail(from, to, "", subject, str, str);
// 메일보내기
Log.v("mylog2","success");
} catch (MessagingException me) {
Log.v("mylog2",me.getMessage());
// System.out.println("메일 전송에 실패하였습니다.");
// System.out.println("실패 이유 : " + me.getMessage());
} catch (Exception e) {
Log.v("mylog2",e.getMessage());
// System.out.println("메일 전송에 실패하였습니다.");
// System.out.println("실패 이유 : " + e.getMessage());
}
}
=====================================================================
퍼미션은 아래와 같이 주 었구요
android.permission.INTERNET
android.permission.WRITE_EXTERNAL_STORAGE
android.permission.READ_EXTERNAL_STORAGE
메일이 발송이 안되고 로그를 확인해보면 아래와 같습니다../.
03-23 16:54:43.140: VERBOSE/mylog2(12927): IOException while sending message
퍼미션 뭐 따로 줘야되는게 있나요???
아니면 다른 문제가 있는건지..답답하네요..ㅡㅡ;;
도와주세요..ㅠ