怎么用JavaMail发送附件
导读:本文共932字符,通常情况下阅读需要3分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: 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 Exce... ...
目录
(为您整理了一些要点),点击可以直达。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发送附件的详细内容,希望对您有所帮助,信息来源于网络。