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