Javamail附加多个文件 [英] Javamail attach multiple files
本文介绍了Javamail附加多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码也使用Javamail API通过gmail smtp服务器发送邮件和附件.
The following code sends mail and attachments as well via gmail smtp server using javamail api.
public void doSendGmail(){
from = txtFrom.getText();
password= new String(txtPassword.getPassword());
to = txtTo.getText();
cc = txtCC.getText();
bcc = txtBCC.getText();
subject = txtSubject.getText();
message_body = jtaMessage.getText();
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,null);
try {
//message definition
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
if(!cc.equals("")){
message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
}
if(!bcc.equals("")){
message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc));
}
message.setSubject(subject);
if(!filename.equals("")){// if a file has been attached...
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(message_body);// actual message
Multipart multipart = new MimeMultipart();// create multipart message
// add the text message to the multipart
multipart.addBodyPart(messageBodyPart);
// attachment part
messageBodyPart = new MimeBodyPart();
String attachment = fileAbsolutePath;
DataSource source = new FileDataSource(attachment);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);//add the attachment to the multipart message
// combine text and attachment
message.setContent(multipart);
// send the complete message
Transport.send(message, from, password);
}
else{// if no file has been attached
message.setText(message_body);
Transport.send(message, from, password);
}
JOptionPane.showMessageDialog(this, "Message Sent!","Sent",JOptionPane.INFORMATION_MESSAGE);
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.toString());
}
}
此代码一次只能附加和发送一个文件.如何启用它来附加多个文件并将其作为一封电子邮件发送?
This code can only attach and send one file at a time. How do you enable it to attach multiple files and send them as one email?
使用JFileChooser附加文件,如下所示:
The file is attached using a JFileChooser as shown below:
public void doAttachFile(){
try {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File file = chooser.getSelectedFile();
filename = file.getName();// get name of selected file
lblFileName.setText(filename);// display name of selected file
fileAbsolutePath= file.getAbsolutePath();
System.out.println("File name: "+filename+"\n"+"Absolute path: "+fileAbsolutePath);
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "No file was attached");
}
}
推荐答案
在附件部分周围使用"for"循环.
Use a "for" loop around the attachment part.
这篇关于Javamail附加多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文