JavaMail API - YooEunseok/Graduation_Project_GARVIS GitHub Wiki
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