📄 sendfile.java
字号:
package ch08.section07;
import java.util.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SendFile {
public static void main(String[] args) {
String to = "wei@weijianxiang.com";
String subject = "test";
String from = "weijx@weijianxiang.com";
String host = "10.198.0.11";
String filename = "c:\\flooring.jpg";
boolean debug = false;
String msgText1 = "发送带附件的邮件的实例。\n";
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
Session session = Session.getInstance(props, null);
session.setDebug(debug);
try {
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {
new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(msgText1);
MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
msg.setContent(mp);
msg.setSentDate(new Date());
Transport.send(msg);
System.out.println("\n 发送成功。。。");
}
catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ( (ex = mex.getNextException()) != null) {
ex.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -