Java Mail, sending multiple attachments not working

后端 未结 3 1423
陌清茗
陌清茗 2021-01-01 19:02

I looked at many entries on the internet without having luck.

Here is my code:

import java.io.IOException;
import java.util.Properties;

import javax         


        
相关标签:
3条回答
  • 2021-01-01 19:37

    Just change the gmailUsername and gmailPass with actual gmail credentials. And also change the to and from address with your desired one. Here we have used Gmail SSL smtp port for sending mail. You can change it with your own mailing server details.

    import java.util.*;
    
    import javax.mail.*;
    
    import javax.mail.internet.*;
    
    import javax.activation.*;
    
    /**
    
    Author Mridul Chatterjee
    
     */
    
    import java.util.Properties;
    
    import javax.mail.Message;
    
    import javax.mail.MessagingException;
    
    import javax.mail.PasswordAuthentication;
    
    import javax.mail.Session;
    
    import javax.mail.Transport;
    
    import javax.mail.internet.InternetAddress;
    
    import javax.mail.internet.MimeMessage;
    
    
    
    public class SendMail {
    
    public static void main(String[] args) {
    
    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″);
    
    ArrayList fileNames = new ArrayList();
    
    fileNames.add(”C:/Write.txt”);
    
    fileNames.add(”C:/Write1.txt”);
    
    fileNames.add(”C:/Write2.txt”);
    
    fileNames.add(”C:/Write3.txt”);
    
    fileNames.add(”C:/25148.jpg”);
    
    
    
    Session session = Session.getDefaultInstance(props,
    
    new javax.mail.Authenticator() {
    
    protected PasswordAuthentication getPasswordAuthentication() {
    
    return new PasswordAuthentication(”gmailUsername”,”gmailPass”);
    
    }
    
    });
    
    
    
    try {
    
    
    
    Message message = new MimeMessage(session);
    
    message.setFrom(new InternetAddress(”from@no-spam.com”));
    
    message.setRecipients(Message.RecipientType.TO,
    
    InternetAddress.parse(”tomail@mail.com”));
    
    message.setSubject(”Testing Subject”);
    
    message.setText(”Dear Mail Crawler,” +
    
    “nn No spam to my email, please!”);
    
         //  multipart.addBodyPart(messageBodyPart);
    
    
    
         //  DataSource source = new FileDataSource(filename);
    
        //   messageBodyPart.setDataHandler(new DataHandler(source));
    
        //   messageBodyPart.setFileName(filename);
    
           System.out.println(fileNames.size());
    
           Multipart multipart = new MimeMultipart();
    
           BodyPart messageBodyPart = new MimeBodyPart();
    
           for(int i=0;i            {
    
                    System.out.println(fileNames.get(i));
    
    
    
    
    
                    messageBodyPart = new MimeBodyPart();
    
                    DataSource source = new FileDataSource((String)fileNames.get(i));
    
                    messageBodyPart.setDataHandler(new DataHandler(source));
    
                    messageBodyPart.setFileName((String)fileNames.get(i));
    
                    multipart.addBodyPart(messageBodyPart);
    
                    //message.setContent(multipart);
    
                }
    
    
    
           //multipart.addBodyPart(messageBodyPart);
    
           message.setContent(multipart);
    
    
    
    Transport.send(message);
    
    
    
    System.out.println(”Mail Sent Successfully….”);
    
    
    
    } catch (MessagingException e) {
    
    throw new RuntimeException(e);
    
    }
    
    }
    
    }
    
    0 讨论(0)
  • 2021-01-01 19:39
        try
        {
            String host = "smtp.gmail.com";
            String from = "sender gmail id";
            String pass = "sender password";
            Properties props = System.getProperties();
            props.put("mail.smtp.starttls.enable", "true"); // added this line
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.user", from);
            props.put("mail.smtp.password", pass);
            props.put("mail.smtp.port", "587");
            props.put("mail.smtp.auth", "true");
            String[] to = {"receiver ids"}; // In this array you can add more ids
            Session session = Session.getDefaultInstance(props, null);
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            InternetAddress[] toAddress = new InternetAddress[to.length];
            // To get the array of addresses
            for( int i=0; i < to.length; i++ )
            { // changed from a while loop
                toAddress[i] = new InternetAddress(to[i]);
            }
            System.out.println(Message.RecipientType.TO);
            for( int i=0; i < toAddress.length; i++) 
            {
            // changed from a while loop
                message.addRecipient(Message.RecipientType.TO, toAddress[i]);
            }
            message.setSubject("sending in a group");
            message.setText("Welcome to JavaMail");
            // set ur text message
            BodyPart messageBodyPart1 = new MimeBodyPart();
            messageBodyPart1.setText("This is message body");
            //file path that you want to attatch
            // you are able to send multipule file simultaneously
            String filename[] ={"C:\\Documents and Settings\\admin\\Desktop\\imp data\\emil id.txt" ,"C:\\Documents and Settings\\admin\\Desktop\\imp data\\emil id.txt"};//change accordingly
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart1);
            for(int i=0 ; i<filename.length ; i++)
            {
                MimeBodyPart messageBodyPart2 = new MimeBodyPart();
                DataSource source = new FileDataSource(filename[i]);
                messageBodyPart2.setDataHandler(new DataHandler(source));
                messageBodyPart2.setFileName(filename[i]);
                multipart.addBodyPart(messageBodyPart2);
            }
            // both part add into maulti part 
            // set message content
            message.setContent(multipart);
            // Trans port the message 
            Transport transport = session.getTransport("smtp");
            transport.connect(host, from, pass);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        }
        catch(Exception ex)
        {
        ex.printStackTrace();
        }
    
    0 讨论(0)
  • 2021-01-01 19:51
    Multipart multipart = new MimeMultipart("mixed");
    for (String str : attachment_PathList) {
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        DataSource source = new FileDataSource(str);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(source.getName());
        multipart.addBodyPart(messageBodyPart);
    }
    msg.setContent(multipart);
    Transport.send(msg);
    
    0 讨论(0)
提交回复
热议问题