`
=死神=
  • 浏览: 56823 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Java mail 发送邮件附件

    博客分类:
  • Java
阅读更多
public class SendFile {
	public static void main(String args[]) throws Exception {
		String host = "smtp.sohu.com";
		String from = "*******";
		String to = "*******";
		String fileAttachment = "d:\\FileName";

		Properties properties = System.getProperties();

		Session session = Session.getInstance(properties, null);

		MimeMessage message = new MimeMessage(session);
		message.setFrom(new InternetAddress(from));
		message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
		message.setSubject("SendFile");

		MimeBodyPart messageBodyPart = new MimeBodyPart();

		messageBodyPart.setText("Hi");

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

		messageBodyPart = new MimeBodyPart();
		DataSource source = new FileDataSource(fileAttachment);
		messageBodyPart.setDataHandler(new DataHandler(source));
		messageBodyPart.setFileName(fileAttachment);
		multipart.addBodyPart(messageBodyPart);

		message.setContent(multipart);

		Transport transport = session.getTransport("smtp");
		transport.connect(host, "", "");

		transport.sendMessage(message, message.getAllRecipients());

		transport.close();
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics