android - how to send automatic email with Attachment -


i used this send automatic emails in app. tried add attachment code app not working! how send automatic email attachment ?

gmailsender.java

package mypack; 

public class gmailsender extends javax.mail.authenticator {

private string   mailhost = "smtp.gmail.com"; private string   user; private string   password; private session  session; public multipart _multipart;  static {     security.addprovider(new hossin.asaadi.mailsender.jsseprovider()); }   public gmailsender(string user, string password) {     this.user = user;     this.password = password;      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");      session = session.getdefaultinstance(props, this);  }   @override protected passwordauthentication getpasswordauthentication() {     return new passwordauthentication(user, password); }   public synchronized void sendmail(string subject, string body, string sender, string recipients) 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));          //   bodypart messagebodypart = new mimebodypart();         //            _multipart.addbodypart(messagebodypart);          // put parts in message          //  message.setcontent(_multipart);          transport.send(message);     }     catch (exception e) {      } }   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;     }       @override     public string getcontenttype() {         if (type == null)             return "application/octet-stream";         else             return type;     }       @override     public inputstream getinputstream() throws ioexception {         return new bytearrayinputstream(data);     }       @override     public string getname() {         return "bytearraydatasource";     }       @override     public outputstream getoutputstream() throws ioexception {         throw new ioexception("not supported");     } }   public void addattachment(string filename) throws exception {     bodypart messagebodypart = new mimebodypart();     datasource source = new filedatasource(filename);     messagebodypart.setdatahandler(new datahandler(source));     messagebodypart.setfilename(filename);      _multipart.addbodypart(messagebodypart); }} 

jsseprovider.java

public final class jsseprovider extends provider {

public jsseprovider() {     super("harmonyjsse", 1.0, "harmony jsse provider");     accesscontroller.doprivileged(new java.security.privilegedaction<void>() {          @override         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;         }     }); }} 

in activity :

            btn3.setonclicklistener(new onclicklistener() {              @override             public void onclick(view arg0) {                 // todo auto-generated method stub                 try {                      gmailsender sender = new gmailsender("mysenderemail", "pass");                     sender.sendmail("title",                             "this body",                             "user@gmail.com",                             "user@gmail.com");                     sender.addattachment("/sdcard/image.jpg");                      toast.maketext(getapplicationcontext(), "2 sucess", 0).show();                  }                 catch (exception e) {                     log.e("sendmail", e.getmessage(), e);                 }             }         }); 


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -