javamail 新浪容器报错 无法添加附件 本地是成功的服务器是失败的

元素真子 2016-07-15 02:37:52

java.io.FileNotFoundException: app/webapps/ROOT/WEB-INF/classes/MixedMail.eml




本地是成功的 服务器是失败的

public class SetMailObj {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) {
String host="smtp.163.com";

String user="**************";
String password="**********”;
String from="********@163.com";

String to="*****@qq.com";
String subject="JavaMail邮件发送测试";
String content="使用JavaMail创建的带附件的邮件";
try {
mymailObj(host,user,password,from,to,subject,content);
} catch (Exception e) {
e.printStackTrace();
System.out.println("发送失败");
}
}

/**
*
* @throws NoSuchProviderException
* @throws MessagingException
* @throws Exception
* @创建时间 2015年11月25日 上午11:24:37
* @作者 樊强
* @返回值 void
* @描述 TODO
*
*/
public static void mymailObj(String host, String user, String password,String from ,String to,String subject,String content) throws NoSuchProviderException,
MessagingException, Exception {
Properties prop = new Properties();
prop.setProperty("mail.host", host );
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//使用JavaMail发送邮件的5个步骤
//1、创建session
Session session = Session.getInstance(prop);
//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
//2、通过session得到transport对象
Transport ts = session.getTransport();
//3、连上邮件服务器
ts.connect(host, user, password);
//4、创建邮件
Message message = createAttachMail(session,from,to,subject,content);
//5、发送邮件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}

/**
* @Method: createAttachMail
* @Description: 创建一封带附件的邮件
*
*
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createAttachMail(Session session,String from,String to,String subject,String content) throws Exception{
MimeMessage message = new MimeMessage(session);

//设置邮件的基本信息
//发件人
message.setFrom(new InternetAddress(from));
//收件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
//邮件标题
message.setSubject(subject);

//创建邮件正文,为了避免邮件正文中文乱码问题,需要使用charset=UTF-8指明字符编码
MimeBodyPart text = new MimeBodyPart();
text.setContent(content, "text/html;charset=UTF-8");

//创建邮件附件
MimeBodyPart attach = new MimeBodyPart();
URL url=SetMailObj.class.getResource("/");
String urls= url.toString().replaceAll("file:/", "");
System.out.println("urls::::"+urls);
DataHandler dh = new DataHandler(new FileDataSource(urls+"/1.docx"));
attach.setDataHandler(dh);
attach.setFileName( MimeUtility.encodeText("附件1:")+dh.getName());

MimeBodyPart attach1 = new MimeBodyPart();
DataHandler dh1 = new DataHandler(new FileDataSource(urls+"/1.docx"));
attach1.setDataHandler(dh1);
attach1.setFileName(MimeUtility.encodeText("附件2")+dh.getName().replaceAll("1",""));

//创建容器描述数据关系
MimeMultipart mp = new MimeMultipart();
mp.addBodyPart(text);
mp.addBodyPart(attach);
mp.addBodyPart(attach1);
mp.setSubType("mixed");

message.setContent(mp);
message.saveChanges();
//将创建的Email写入到E盘存储
message.writeTo(new FileOutputStream(urls+"/MixedMail.eml"));
//返回生成的邮件
return message;
}
}
...全文
44 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
元素真子 2016-07-15
  • 打赏
  • 举报
回复
少了个/app 服务器上

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧