Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hiasm.hiasmproject;
- import android.os.Handler;
- import android.os.HandlerThread;
- import java.io.File;
- import java.util.List;
- import java.util.Properties;
- import javax.activation.DataHandler;
- import javax.activation.FileDataSource;
- import javax.mail.Authenticator;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.Multipart;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeBodyPart;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeMultipart;
- public class SmtpMailer {
- public static class Params {
- private String username;
- private String password;
- private String host;
- private int port;
- private String recipientAddress;
- private String subject;
- private String text;
- private List<String> attachmentFileNames;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getHost() {
- return host;
- }
- public void setHost(String host) {
- this.host = host;
- }
- public int getPort() {
- return port;
- }
- public void setPort(int port) {
- this.port = port;
- }
- public String getRecipientAddress() {
- return recipientAddress;
- }
- public void setRecipientAddress(String recipientAddress) {
- this.recipientAddress = recipientAddress;
- }
- public String getSubject() {
- return subject;
- }
- public void setSubject(String subject) {
- this.subject = subject;
- }
- public String getText() {
- return text;
- }
- public void setText(String text) {
- this.text = text;
- }
- public List<String> getAttachmentFileNames() {
- return attachmentFileNames;
- }
- public void setAttachmentFileNames(List<String> attachmentFileNames) {
- this.attachmentFileNames = attachmentFileNames;
- }
- }
- public static void sendMail(final Params params) {
- HandlerThread handlerThread = new HandlerThread("SmtpMailerHandlerThread");
- handlerThread.start();
- Handler handler = new Handler(handlerThread.getLooper());
- handler.post(new Runnable() {
- @Override
- public void run() {
- try {
- Properties props = new Properties();
- props.put("mail.smtp.auth", "true");
- props.put("mail.smtp.starttls.enable", "true");
- props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
- props.put("mail.smtp.host", params.getHost());
- props.put("mail.smtp.port", params.getPort());
- props.put("mail.smtp.socketFactory.port", params.getPort());
- Authenticator authenticator = new javax.mail.Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(params.getUsername(), params.getPassword());
- }
- };
- Session session = Session.getInstance(props, authenticator);
- Message message = new MimeMessage(session);
- message.setFrom(new InternetAddress(params.getUsername()));
- message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(params.getRecipientAddress()));
- message.setSubject(params.getSubject());
- message.setText(params.getText());
- if (params.getAttachmentFileNames() != null && params.getAttachmentFileNames().size() > 0) {
- addAttachments(message, params);
- }
- Transport.send(message);
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- private static void addAttachments(Message message, Params params) throws MessagingException {
- Multipart multipart = new MimeMultipart();
- for (String filePath : params.getAttachmentFileNames()) {
- MimeBodyPart messageBodyPart = new MimeBodyPart();
- messageBodyPart.setDataHandler(new DataHandler(new FileDataSource(filePath)));
- messageBodyPart.setFileName(new File(filePath).getName());
- multipart.addBodyPart(messageBodyPart);
- }
- message.setContent(multipart);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement