通过附件发送电子邮件

时间:2013-11-24 04:50:06

标签: java email

我使用以下代码通过gmail发送邮件。我从这个网站获得了该代码。我想将文件附加到我发送的邮件中。我可以用这段代码吗?我应该编辑这段代码的哪一部分?我可以通过电子邮件发送sql备份文件(xxx.sql)吗?

    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("ravinduis34@gmail.com", "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( "ravinduis34@gmail.com" ) );
    msg.setRecipients( Message.RecipientType.TO,InternetAddress.parse("ravindusisira@gmail.com") );
    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 );
}

1 个答案:

答案 0 :(得分:2)

试试这个:

    String to="somebody@something.com";//change accordingly  
    final String user="me@gmail.com"; //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();
    }
相关问题