JavaMail API - YooEunseok/Graduation_Project_GARVIS GitHub Wiki

JavaMail API

The JavaMailโ„ข API provides classes that model a mail system. The javax.mail package defines classes that are common to all mail systems. The javax.mail.internet package defines classes that are specific to mail systems based on internet standards such as MIME, SMTP, POP3, and IMAP. The JavaMail API includes the javax.mail package and subpackages.

Setting

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());
    
    .....
}

Code

  • 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

โš ๏ธ **GitHub.com Fallback** โš ๏ธ