আমার android app এ user interaction ছাড়া ইমেইল পাঠাতে হবে। এটা কিভাবে পাঠাবো বুঝতে পারছি না। Android এর intent class ব্যবহার করলে সেটা android এর যে built in gmail app টা আছে সেখানে নিয়ে যাচ্ছে। কিন্তু আমি চাচ্ছি জিমেইল অ্যাপটা ব্যবহার না করে এবং ইউজার এর কোন interaction ছাড়াই ইমেইলটা পাঠাতে।

asked 01 Feb '15, 13:22

corei11's gravatar image

corei11
418113

edited 01 Feb '15, 13:23


JavaMail API দিয়ে Gmail authentication ব্যবহার করে android app দিয়ে mail send

এখানে ৩টা ক্লাস লাগবে যার মধ্যে দুইটা ক্লাস মেইন package এ এবং JSSEProvider ক্লাসটা অন্য একটা প্যাকেজে রাখতে হবে...

MailSenderActivity.java

Your Package

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MailSenderActivity extends Activity {

    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Button send = (Button) this.findViewById(R.id.send);
        final EditText et = (EditText) findViewById(R.id.editText1);
        send.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                try {   
                     GMailSender sender = new GMailSender("[email protected]", "password");
                    sender.sendMail("This is Subject",   
                            "This is Body",   
                            "[email protected]",   
                            "[email protected]il.com","/mnt/sdcard/data.png");   
                    et.setText("yes");
                } catch (Exception e) {   
                    Log.e("SendMail", e.getMessage(), e);   
                }

            }
        });

    }
}

GmailSender.java

Your Package

import javax.activation.DataHandler;   
import javax.activation.DataSource;   
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;   
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;

import java.io.ByteArrayInputStream;   
import java.io.IOException;   
import java.io.InputStream;   
import java.io.OutputStream;   
import java.security.Security;   
import java.util.Properties;

public class GMailSender extends javax.mail.Authenticator {   
    private String mailhost = "smtp.gmail.com";   
    private String user;   
    private String password;   
    private Session session;   
    private Multipart _multipart= new MimeMultipart() ;

    static {   
        Security.addProvider(new com.provider.JSSEProvider());   
    }

    public GMailSender(String user, String password) {   
        this.user = user;   
        this.password = password;

        Properties props = new Properties();   
        props.put("mail.transport.protocol", "smtp");   
        props.put("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.put("mail.smtp.quitwait", "false");

        session = Session.getDefaultInstance(props, this);   
    }

    protected PasswordAuthentication getPasswordAuthentication() {   
        return new PasswordAuthentication(user, password);   
    }

    public synchronized void sendMail(String subject, String body, String sender, String recipients,String source) throws Exception {   
        try{
        MimeMessage message = new MimeMessage(session);   
       // DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));   
        message.setSender(new InternetAddress(sender));   
        message.setSubject(subject);   
       // message.setDataHandler(handler);   
        if (recipients.indexOf(',') > 0)   
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));   
        else  
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));   
        addAttachment(source,body);
        message.setContent(_multipart);
        Transport.send(message);   
        }catch(Exception e){

        }
    }   
    public void addAttachment(String filename,String body) throws Exception { 
        BodyPart messageBodyPart = new MimeBodyPart(); 
        DataSource source = new FileDataSource(filename); 
        messageBodyPart.setDataHandler(new DataHandler(source)); 
        messageBodyPart.setFileName(filename); 
        _multipart.addBodyPart(messageBodyPart);

        BodyPart messageBodyPart2 = new MimeBodyPart(); 
        messageBodyPart2.setText(body);

        _multipart.addBodyPart(messageBodyPart2); 
    }



    public class ByteArrayDataSource implements DataSource {   
        private byte[] data;   
        private String type;

        public ByteArrayDataSource(byte[] data, String type) {   
            super();   
            this.data = data;   
            this.type = type;   
        }

        public ByteArrayDataSource(byte[] data) {   
            super();   
            this.data = data;   
        }

        public void setType(String type) {   
            this.type = type;   
        }

        public String getContentType() {   
            if (type == null)   
                return "application/octet-stream";   
            else  
                return type;   
        }

        public InputStream getInputStream() throws IOException {   
            return new ByteArrayInputStream(data);   
        }

        public String getName() {   
            return "ByteArrayDataSource";   
        }

        public OutputStream getOutputStream() throws IOException {   
            throw new IOException("Not Supported");   
        }   
    }   
}

JSSEProvider

Your Package

/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * @author Alexander Y. Kleymenov
 * @version $Revision$
 */


import java.security.AccessController;
import java.security.Provider;

public final class JSSEProvider extends Provider {

    public JSSEProvider() {
        super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
        AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
            public Void run() {
                put("SSLContext.TLS",
                        "org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
                put("Alg.Alias.SSLContext.TLSv1", "TLS");
                put("KeyManagerFactory.X509",
                        "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
                put("TrustManagerFactory.X509",
                        "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
                return null;
            }
        });
    }
}

এখানে কোডটা attachment সহ দেখানো হয়েছে। শুধু টেক্সট এর জন্য পাঠাতে চাইলে সেটাও করা যাবে। সেক্ষেত্রে source পাঠাতে হবে না। আর addAttachment মেথডটা কল না করে sendMail মেথড এর কমেন্ট লাইন দুটা উঠিয়ে দিতে হবে আর এর শেষে

addAttachment(source,body);
message.setContent(_multipart);

এই দুইটা লাইন কমেন্ট করে দিতে হবে।

এখানে যেহেতু JavaMail API ব্যবহার করা হয়েছে তাই

https://code.google.com/p/javamail-android/downloads/list

এখান থেকে তিনটা (mail.jar , activation.jar , additionnal.jar ) জার ফাইল ডাউনলোড করে app এর সাথে যোগ করতে হবে।

আর manifest file এ Internet permission এর জন্য

<uses-permission android:name="android.permission.INTERNET"/>

এটা যোগ করতে হবে।

permanent link

answered 05 Feb '15, 14:48

corei11's gravatar image

corei11
418113

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×8
×3
×2

question asked: 01 Feb '15, 13:22

question was seen: 3,048 times

last updated: 05 Feb '15, 14:48