2013-11-24 168 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中的附件(