JLink
JLink

Reputation: 307

Send email through with attach files

I used the following code to send mail through gmail. I got that code from this site itself. I want to attach a file to the mail that I send. Can I do that with this code? what part of this code should I edit? Can I send sql backup file(xxx.sql) through email?

    try{

    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com"); // for gmail use smtp.gmail.com
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "true"); 
    props.put("mail.smtp.starttls.enable", "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");

    Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() {

        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("[email protected]", "creative12533");
        }
    });

    mailSession.setDebug(true); // Enable the debug mode

    Message msg = new MimeMessage( mailSession );

    //--[ Set the FROM, TO, DATE and SUBJECT fields
    msg.setFrom( new InternetAddress( "[email protected]" ) );
    msg.setRecipients( Message.RecipientType.TO,InternetAddress.parse("[email protected]") );
    msg.setSentDate( new Date(232323));
    msg.setSubject( "Hello World!" );

    //--[ Create the body of the mail
    msg.setText( "Hello from my first e-mail sent with JavaMail" );

    //--[ Ask the Transport class to send our mail message
    Transport.send( msg );

}catch(Exception E){
    System.out.println( "Oops something has gone pearshaped!");
    System.out.println( E );
}

Upvotes: 0

Views: 432

Answers (1)

Vipul Paralikar
Vipul Paralikar

Reputation: 1568

try this:

    String to="[email protected]";//change accordingly  
    final String user="[email protected]"; //change accordingly  
    final String password="xxxxx";//change accordingly  

    //1) get the session object     
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com"); // for gmail use smtp.gmail.com
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "true"); 
    props.put("mail.smtp.starttls.enable", "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");

    Session session = Session.getDefaultInstance(props,  
            new javax.mail.Authenticator() {  
        protected javax.mail.PasswordAuthentication getPasswordAuthentication() {  
            return new javax.mail.PasswordAuthentication(user,password);  
        }  
    });  

    //2) compose message     
    try{  
        MimeMessage message = new MimeMessage(session);  
        message.setFrom(new InternetAddress(user));  
        message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
        message.setSubject("Message Alert");  

        //3) create MimeBodyPart object and set your message text     
        BodyPart messageBodyPart1 = new MimeBodyPart();  
        messageBodyPart1.setText("This is message body");  

        //4) create new MimeBodyPart object and set DataHandler object to this object      
        MimeBodyPart messageBodyPart2 = new MimeBodyPart();  

        String filename = "SendAttachment.doc";//change accordingly  
        DataSource source = new FileDataSource(filename);  
        messageBodyPart2.setDataHandler(new DataHandler(source));  
        messageBodyPart2.setFileName(filename);  


        //5) create Multipart object and add MimeBodyPart objects to this object      
        Multipart multipart = new MimeMultipart();  
        multipart.addBodyPart(messageBodyPart1);  
        multipart.addBodyPart(messageBodyPart2);  

        //6) set the multipart object to the message object  
        message.setContent(multipart );  

        //7) send message  
        Transport.send(message);  

        System.out.println("message sent....");  
    }catch (MessagingException ex) {
        ex.printStackTrace();
    }

Upvotes: 2

Related Questions