各位,大家好!问题如题,我写个main方法测试邮件没有任何问题,但是我在servlet中调用就出现问题,发送附件不知道发的是什么,请各位帮忙看看,解决给分,在线等,谢谢!
Properties properties = new Properties();
Authenticator auth = new PopupAuthenticator();
Session session = Session.getInstance(properties, auth);
properties.put("mail.smtp.host", SMTPServer);
properties.put("mail.smtp.auth", "true");
MimeMessage mimemessage = new MimeMessage(session);
mimemessage.setFrom(new InternetAddress(from));
mimemessage.setSubject(subject,"gb2312"); //标题
Multipart mm=new MimeMultipart();//新建一个MimeMultipart对象用来存放BodyPart对象(事实上可以存放多个)
// 给消息对象设置内容
BodyPart mdp=new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
mdp.setContent(content,"text/html;charset=gb2312");//给BodyPart对象设置内容和格式/编码方式
//mdp.setText(content);
mm.addBodyPart(mdp);//将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)
//mdp.setFileName(fileAttachment);
mimemessage.setContent(mm);
/*******************附件部分***************/
BodyPart affixBody = new MimeBodyPart();
DataSource fds=new FileDataSource(fileAttachment); //得到数据源
affixBody.setDataHandler(new DataHandler(fds));
affixBody.setFileName(fds.getName());
mm.addBodyPart(affixBody); mimemessage.setContent(mm);
mimemessage.setSentDate(new Date());
Transport transport = session.getTransport("smtp");
transport.connect(SMTPServer, user, password);
mimemessage.setRecipients(javax.mail.Message.RecipientType.TO,
InternetAddress.parse(s));
Transport.send(mimemessage);
transport.close();
Properties properties = new Properties();
Authenticator auth = new PopupAuthenticator();
Session session = Session.getInstance(properties, auth);
properties.put("mail.smtp.host", SMTPServer);
properties.put("mail.smtp.auth", "true");
MimeMessage mimemessage = new MimeMessage(session);
mimemessage.setFrom(new InternetAddress(from));
mimemessage.setSubject(subject,"gb2312"); //标题
Multipart mm=new MimeMultipart();//新建一个MimeMultipart对象用来存放BodyPart对象(事实上可以存放多个)
// 给消息对象设置内容
BodyPart mdp=new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
mdp.setContent(content,"text/html;charset=gb2312");//给BodyPart对象设置内容和格式/编码方式
//mdp.setText(content);
mm.addBodyPart(mdp);//将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)
//mdp.setFileName(fileAttachment);
mimemessage.setContent(mm);
/*******************附件部分***************/
BodyPart affixBody = new MimeBodyPart();
DataSource fds=new FileDataSource(fileAttachment); //得到数据源
affixBody.setDataHandler(new DataHandler(fds));
affixBody.setFileName(fds.getName());
mm.addBodyPart(affixBody); mimemessage.setContent(mm);
mimemessage.setSentDate(new Date());
Transport transport = session.getTransport("smtp");
transport.connect(SMTPServer, user, password);
mimemessage.setRecipients(javax.mail.Message.RecipientType.TO,
InternetAddress.parse(s));
Transport.send(mimemessage);
transport.close();
标题、收件人都没有显示,但是用main方法测试都是很好的呢。
------=_Part_1_11116972.1269221181031
Content-Type: text/html; charset=gb2312
Content-Transfer-Encoding: quoted-printable =C4=FA=BA=C3=A3=A1=C4=FA=B1=BE=B4=CE=B1=A8=CB=CD=A3=BA=BB=F9=B4=A1=CA=FD=BE=
=DD=C8=D5=B1=A8=CB=CD<table width=3D100% border=3D1 class=3Dchanson><tr bgco=
lor=3D"#FFF5F4"><td class=3Dtitle width=3D80>=BD=D3=BF=DA=C0=E0=D0=CD=B4=FA=
=C2=EB</td><td class=3Dtitle width=3D80>=BD=D3=BF=DA=C0=E0=D0=CD=C3=FB=B3=C6=
</td><td class=3Dtitle width=3D150>=B1=A8=CB=CD=CA=B1=BC=E4</td><td class=3D=
title width=3D50>=CA=C7=B7=F1=B3=C9=B9=A6</td><td class=3Dtitle width=3D50>=
=B4=ED=CE=F3=B4=FA=C2=EB</td><td class=3Dtitle width=3D80>=B4=ED=CE=F3=C3=FB=
=B3=C6</td><td class=3Dtitle>=B4=ED=CE=F3=CB=B5=C3=F7</td><td class=3Dtitle>=
=B2=E9=BF=B4</td></tr>
------=_Part_1_11116972.1269221181031--
正文内容好像还没有编码转换。
检查一下servlet中的编码传递,是否转了之后没有转回来。
很困惑,希望遇见过此问题的帮忙说一下呢。
还有对于同一个包的同一个类到底是怎么找的呢?
谢谢···················