千家信息网

怎么用JavaMail发送附件

发表于:2024-09-30 作者:千家信息网编辑
千家信息网最后更新 2024年09月30日,这篇文章主要介绍"怎么用JavaMail发送附件"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"怎么用JavaMail发送附件"文章能帮助大家解决问题。impo
千家信息网最后更新 2024年09月30日怎么用JavaMail发送附件

这篇文章主要介绍"怎么用JavaMail发送附件"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"怎么用JavaMail发送附件"文章能帮助大家解决问题。

import Java.util.Properties;
import javax.Mail.*;
import javax.mail.inte.NET.*;
import javax.activation.*;

public class AttachExample {
public static void main (String args[])
throws Exception {
String host = args[0];
String from = args[1];
String to = args[2];
String fileAttachment = args[3];

// Get system properties
Properties props = System.getProperties();

// Setup mail server
props.put("mail.smtp.host", host);

// Get session
Session session =
Session.getInstance(props, null);

// Define message
MimeMessage message =
new MimeMessage(session);
message.setFrom(
new InternetAddress(from));
message.addRecipient(
Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject(
"Hello JavaMail Attachment");

// create the message part
MimeBodyPart messageBodyPart =
new MimeBodyPart();

//fill message
messageBodyPart.setText("Hi");

Multipart multipart = new MimeMultipart();
multipart.addbodyPart(messageBodyPart);

// Part two is attachment
messageBodyPart = new MimeBodyPart();
Datasource source =
new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(
new DataHandler(source));
messageBodyPart.setFileName(fileAttachment);
multipart.addBodyPart(messageBodyPart);

// Put parts in message
message.setContent(multipart);

// Send the message
Transport.send( message );
}
}

关于"怎么用JavaMail发送附件"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注行业资讯频道,小编每天都会为大家更新不同的知识点。

0