今天这个附件是通过URL来获取,

前期工作请参考

 

主要代码如下:

import org.apache.commons.mail.DefaultAuthenticator;import org.apache.commons.mail.EmailAttachment;import org.apache.commons.mail.EmailException;import org.apache.commons.mail.MultiPartEmail;public class AttachmentLocalTest {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stub// Create the p_w_uploadEmailAttachment p_w_upload = new EmailAttachment();//附件地址p_w_upload.setPath("E:/test.txt");p_w_upload.setDisposition(EmailAttachment.ATTACHMENT);//附件说明p_w_upload.setDescription("Picture of John");//附件名称//这里需要加上文件的后缀名p_w_upload.setName("test2.txt");// Create the email messageMultiPartEmail email = new MultiPartEmail();//邮件服务器email.setHostName("smtp.126.com");//端口号email.setSmtpPort(25);//用户名、密码email.setAuthenticator(new DefaultAuthenticator("yuke198907@126.com", "密码你懂的"));email.setSSLOnConnect(true);try {//收件人email.addTo("yuke@iisant.com", "yuke");//发件人email.setFrom("yuke198907@126.com", "yuke198907");//标题email.setSubject("The picture");//内容email.setMsg("Here is the picture you wanted");// add the p_w_uploademail.attach(p_w_upload);// send the emailemail.send();} catch (EmailException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}