eMail with attachment using java mail api in android

后端 未结 2 993
刺人心
刺人心 2020-12-06 15:09

I am beginning with android and this is my first program.

This is what my code looks like. I am able to send an email (in background) using this code but unable to a

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-06 16:04

    package com.mail.example;
    
    import java.util.Properties;
    
    import javax.activation.CommandMap;
    import javax.activation.DataHandler;
    import javax.activation.DataSource;
    import javax.activation.FileDataSource;
    import javax.activation.MailcapCommandMap;
    import javax.mail.BodyPart;
    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 SendFileEmail
    {
     public void send(String filepath)
     {
    
      // Recipient's email ID needs to be mentioned.
      String to = "abc@gmail.com";
    
      // Sender's email ID needs to be mentioned
      final String from = "xyz@gmail.com";
     // final String username = "xyz";
      final String pass = "xyz123";
      // Assuming you are sending email from localhost
      String host = "smtp.gmail.com";
    
      // Get system properties
      Properties properties = System.getProperties();
    
      // Setup mail server
      properties.setProperty("mail.smtp.host", host);
      properties.put("mail.smtp.user", from);
      properties.put("mail.smtp.password", pass);
      properties.put("mail.smtp.port", "587");
      properties.put("mail.smtp.starttls.enable","true");
      properties.put("mail.smtp.auth", "true");
    
      //Read more: http://mrbool.com/how-to-work-with-java-mail-api-in-android/27800#ixzz3E2T8ZbpJ
    
    
      // Get the default Session object.
      //Session session = Session.getDefaultInstance(properties);
    
    
      Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(from, pass);
            }
        });
    
    
      try{
         // Create a default MimeMessage object.
         MimeMessage message = new MimeMessage(session);
    
         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));
    
         // Set To: header field of the header.
         message.addRecipient(Message.RecipientType.TO,
                                  new InternetAddress(to));
    
         // Set Subject: header field
         message.setSubject("This is the Subject Line!");
    
         // Create the message part 
         BodyPart messageBodyPart = new MimeBodyPart();
    
         // Fill the message
         messageBodyPart.setText("This is message body");
    
         // Create a multipar message
         Multipart multipart = new MimeMultipart();
    
         // Set text message part
         multipart.addBodyPart(messageBodyPart);
    
         // Part two is attachment
         messageBodyPart = new MimeBodyPart();
         DataSource source = new FileDataSource(filepath);
         messageBodyPart.setDataHandler(new DataHandler(source));
         messageBodyPart.setFileName(filepath);
         multipart.addBodyPart(messageBodyPart);
    
    
         MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap(); 
         mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html"); 
         mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml"); 
         mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain"); 
         mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed"); 
         mc.addMailcap("message/rfc822;; x-java-content- handler=com.sun.mail.handlers.message_rfc822");
    
         // Send the complete message parts
         message.setContent(multipart);
         // Send message
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
      }
    }
     }
    

    call the method like this

    public class SendMail {
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        new SendFileEmail().send("sample.xlsx");
    }
    
    }
    

    If the file is in asset folder example test.txt

    File f = new File(getCacheDir()+"/test.txt");
    if (!f.exists()) try {
    
    InputStream is = getAssets().open("test.txt");
    int size = is.available();
    byte[] buffer = new byte[size];
    is.read(buffer);
    is.close();
    
    
    FileOutputStream fos = new FileOutputStream(f);
    fos.write(buffer);
    fos.close();
     } catch (Exception e) { throw new RuntimeException(e); }
    
    new SendFileEmail().send(f.getPath());
    

提交回复
热议问题