How to send mail with an image as an attachment in

2020-07-13 07:48发布

问题:

In my android application i need to send the mail with an image as a attachment.I am done with the sending the mail.but how to send the mail with an image as a attachment to the mail.Here i am posting the code for the sending the mail.Please help me to send the image as an attachment in following code.

Here is the code-

public class MailImageFile extends javax.mail.Authenticator {

        public MailImageFile(){}

 public void Mail(String user, String pass) {
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

    Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator()           {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("abc@gmail.com", "pqr123%");
        }
        });
    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("abc@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,     InternetAddress.parse("xyz@gmail.com"));
        message.setSubject("Testing Subject");
        message.setContent("Hi...", "text/html; charset=utf-8");

        Transport.send(message);

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }

}

}

回答1:

 public class MailImageFile extends javax.mail.Authenticator {

            public MailImageFile(){}

     public void Mail(String user, String pass) {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator()           {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("abc@gmail.com", "pqr123%");
            }
            });
        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("abc@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,     InternetAddress.parse("xyz@gmail.com"));
    message.setContent(_multipart);
            message.setSubject("Testing Subject");
            message.setContent("Hi...", "text/html; charset=utf-8");

            Transport.send(message);

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }

//Got this solution form here

    private Multipart _multipart; 
_multipart = new MimeMultipart(); 

public void addAttachment(String filename,String subject) 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(subject); 

    _multipart.addBodyPart(messageBodyPart2); 
} 


    }


回答2:

Try this code:

Intent myEmailIntent = new Intent(android.content.Intent.ACTION_SEND);
        myEmailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Email Subject");
        myEmailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{"sendersemailaddreass@gmail.com"});

        myEmailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"your email message");
        //message type
        myEmailIntent.setType("text/plain");
        File myAttachmentfile = getFileStreamPath("path of the file that you want to attach Eg: image");
        myEmailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        //attachment type
        myEmailIntent.setType("image/jpeg");
        myEmailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+myAttachmentfile.getAbsolutePath()));
        startActivityForResult(Intent.createChooser(myEmailIntent, "SENDER NAME"),101);

//This is how you attach image or any file to email

emailIntent.putExtra(Intent.EXTRA_STREAM, "your file path");

Let me know if it worked for you.



回答3:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailaddress);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
emailIntent.setType("image/png");

ArrayList<Uri> uris = new ArrayList<Uri>();

uris.add(Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.file1));
uris.add(Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.file2));

emailIntent.putExtra(Intent.EXTRA_STREAM, uris));

startActivity(emailIntent);


回答4:

This will surely help you to attach your image without using intent.

Multipart multipart = new MimeMultipart();

MimeBodyPart attachPart = new MimeBodyPart();
String attachFile = "/storage/emulated/0/pic.jpg";//picture location

DataSource source = new FileDataSource(attachFile);
attachPart.setDataHandler(new DataHandler(source));
attachPart.setFileName(new File(attachFile).getName());

//Trick is to add the content-id header here
attachPart.setHeader("Content-ID", "image_id");
multipart.addBodyPart(attachPart);

//third part for displaying image in the email body
attachPart = new MimeBodyPart();
attachPart.setContent("<h1>Attached Image</h1>" +
        "<img src='cid:image_id'>", "text/html");
multipart.addBodyPart(attachPart);
//Set the multipart message to the email message
message.setContent(multipart);

Transport.send(message);
System.out.println("Done");