java mail 三个,Javamail附加多个文件

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?

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");

}

}

解决方案

Use a "for" loop around the attachment part.

JavaMail 是用于发送和接收电子邮件的 Java API。可以使用 JavaMail API 从多个发件人发送电子邮件,下面是一个示例: ```java import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class MultipleSenders { public static void main(String[] args) { // 发件人列表 String[] fromList = {"sender1@example.com", "sender2@example.com"}; // 收件人 String to = "recipient@example.com"; // SMTP 服务器地址 String host = "smtp.example.com"; // 邮件标题 String subject = "JavaMail Test"; // 邮件内容 String text = "This is a test email from JavaMail."; // 发件人用户名和密码 String username = "username"; String password = "password"; // 设置邮件会话属性 Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", host); props.put("mail.smtp.port", "587"); // 获取邮件会话 Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected javax.mail.PasswordAuthentication getPasswordAuthentication() { return new javax.mail.PasswordAuthentication(username, password); } }); try { // 创建邮件消息 Message message = new MimeMessage(session); message.setFrom(new InternetAddress(fromList[0])); for (int i = 1; i < fromList.length; i++) { message.addFrom(InternetAddress.parse(fromList[i])); } message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); message.setSubject(subject); message.setText(text); // 发送邮件 Transport.send(message); System.out.println("Email sent successfully."); } catch (MessagingException e) { e.printStackTrace(); } } } ``` 在上面的示例中,我们使用 `addFrom()` 方法将多个发件人添加到邮件消息中。注意,SMTP 服务器可能会对此进行限制,因此请确保您遵守 SMTP 服务器的规定。另外,也要确保您有权使用添加的所有发件人地址。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值