怎么用JavaMail发送附件-创新互联

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

成都创新互联长期为上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为大石桥企业提供专业的成都网站设计、网站制作、外贸营销网站建设大石桥网站改版等技术服务。拥有10多年丰富建站经验和众多成功案例,为您定制开发。

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发送附件”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注创新互联-成都网站建设公司行业资讯频道,小编每天都会为大家更新不同的知识点。


分享名称:怎么用JavaMail发送附件-创新互联
当前网址:http://myzitong.com/article/cecdod.html