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);
}
可能重複http://stackoverflow.com/questions/6814681/send-a-file-as-一個附着式-java的) – Adarsh