2013-11-24 217 views
0

我使用下面的代碼通過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("[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); 
} 
+1

可能重複http://stackoverflow.com/questions/6814681/send-a-file-as-一個附着式-java的) – Adarsh

回答

2

嘗試:

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(); 
    } 
的[發送文件作爲Java中的附件(