怎么用JavaMail发送附件(javamail,编程语言)

时间:2024-04-29 12:12:54 作者 : 石家庄SEO 分类 : 编程语言
  • TAG :

    %E6%80%8E%E4%B9%88%E7%94%A8JavaMail%E5%8F%91%E9%80%81%E9%99%84%E4%BB%B6

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发送附件的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:C语言中的赋值操作符 (=)指的是什么下一篇:

4 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18