-
Notifications
You must be signed in to change notification settings - Fork 1
JavaMail API
The JavaMail™ API provides classes that model a mail system. The
javax.mailpackage defines classes that are common to all mail systems. Thejavax.mail.internetpackage defines classes that are specific to mail systems based on internet standards such as MIME, SMTP, POP3, and IMAP. The JavaMail API includes thejavax.mailpackage and subpackages.
Add the library to use SMTP. You can receive the necessary files through the link below.
https://code.google.com/archive/p/javamail-android/downloads
Download the following three library and add them to Android Studio.
Afterwards, Android Studio should allow permission to access the Internet.
Add user-permission to Manifest as follows:
<uses-permission android:name="android.permission.INTERNET"/>
</mainfest>
Go to mail-related Activity and allow permission to use the internet in protected void onCreate()
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mail);
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.permitDiskReads()
.permitDiskWrites()
.PermitNetwork().build());
.....
}
-
Class name: GMailSender
-
Import list:
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
It receives smtp information supported by Google and delivers it to MimeMessage objects.
public GMailSender(String user, String password) {
this.user = user;
this.password = password;
emailCode = createEmailCode();
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");
//구글에서 지원하는 smtp 정보를 받아와 MimeMessage 객체에 전달해준다.
session = Session.getDefaultInstance(props, this);
}
The mail is sent using the code below.
public synchronized void sendMail(String subject, String body, String recipients) throws Exception {
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain")); //본문 내용을 byte단위로 쪼개어 전달
message.setSender(new InternetAddress(user)); //본인 이메일 설정
message.setSubject(subject); //해당 이메일의 본문 설정
Log.d("Tag11","=>"+subject+user+password);
message.setDataHandler(handler);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message); //메시지 전달
}
If you want to see the full code associated with the mail, look at the GMailSender.java code on the repository