java internetaddress.parse_javax.mail.internet.ParseException:在Content-Type字符串中,期望’/’,得到:...

我想使用JSF实现带附件的邮件.我试过这段代码:

private Part file;

private String sendFromGMail(String from, String pass, String[] to, String subject, String body)

{

String status;

Properties props = System.getProperties();

String host = "smtp.gmail.com";

props.put("mail.smtp.starttls.enable", "true");

props.put("mail.smtp.host", host);

props.put("mail.smtp.user", from);

props.put("mail.smtp.password", pass);

props.put("mail.smtp.port", "587");

props.put("mail.smtp.auth", "true");

Session session = Session.getDefaultInstance(props);

MimeMessage message = new MimeMessage(session);

try

{

message.setFrom(new InternetAddress(from));

InternetAddress[] toAddress = new InternetAddress[to.length];

// To get the array of addresses

for (int i = 0; i < to.length; i++)

{

toAddress[i] = new InternetAddress(to[i]);

}

for (int i = 0; i < toAddress.length; i++)

{

message.addRecipient(Message.RecipientType.TO, toAddress[i]);

}

message.setSubject(subject);

message.setText(body);

MimeBodyPart messageBodyPart = new MimeBodyPart();

Multipart multipart = new MimeMultipart();

messageBodyPart = new MimeBodyPart();

messageBodyPart.setDataHandler(new DataHandler(file, file.getName()));

messageBodyPart.setFileName(file.getName());

multipart.addBodyPart(messageBodyPart);

message.setContent(multipart);

Transport transport = session.getTransport("smtp");

transport.connect(host, from, pass);

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

transport.close();

}

catch (AddressException ae)

{

ae.printStackTrace();

return status = "Cannot send Message!";

}

catch (MessagingException me)

{

me.printStackTrace();

return status = "Cannot send Message!";

}

return status = "Message is send!";

}

//.......................

public Part getFile() {

return file;

}

public void setFile(Part file) {

this.file = file;

}

public void upload() {

if (file != null) {

try (InputStream inputStream = file.getInputStream(); FileOutputStream outputStream = new FileOutputStream("D:" + File.separator + "files" + File.separator + file.getSubmittedFileName())) {

int bytesRead = 0;

final byte[] chunck = new byte[1024];

while ((bytesRead = inputStream.read(chunck)) != -1) {

outputStream.write(chunck, 0, bytesRead);

}

FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Upload successfully ended!"));

} catch (IOException e) {

FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Upload failed!"));

}

}

}

但是在Tomcat日志文件中我收到此错误:

javax.mail.internet.ParseException: In Content-Type string , expected '/', got :

at javax.mail.internet.ContentType.(ContentType.java:103)

at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1448)

at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2190)

at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2151)

at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1846)

at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1196)

at com.web.common.Contacts.sendFromGMail(Contacts.java:135)

at com.web.common.Contacts.sendEmail(Contacts.java:53)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:497)

at org.apache.el.parser.AstValue.invoke(AstValue.java:247)

at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)

at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)

at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)

at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)

at javax.faces.component.UICommand.broadcast(UICommand.java:315)

at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)

at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)

at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)

at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)

at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)

at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)

at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)

at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)

at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)

at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)

at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)

at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

at java.lang.Thread.run(Thread.java:745)

有这个问题的解决方案吗?

在我的情况下,我想使用h:inputFile上传文件并将其作为电子邮件附件发送.但我得到错误.

我不是已经在其他地方回答了这个问题吗?

将Part.getInputStream()与ByteArrayDataSource一起使用:

ByteArrayDataSource ds = new ByteArrayDataSource(file.getInputStream(), file.getContentType());

messageBodyPart.setDataHandler(new DataHandler(ds));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaMail.jsp代码: <%@ page import="javax.mail.*"%> <%@ page import="javax.activation.*"%> <%@ page import="javax.mail.internet.*"%> <%@ page import="java.util.*,java.io.*"%> <%@ page contentType="text/html;charset=utf8"%> <%@page import="java.net.URL"%> <%@page import="org.apache.commons.httpclient.methods.GetMethod"%> <%@page import="org.apache.commons.httpclient.HttpClient"%> <%@page import="vdo.ws.GetSource"%> <html> <head> <title>CH17 - JavaMail2.jsp</title> </head> <body> <h2> 利用JavaMail来发送电子邮件 - HTML格式 </h2> <% InternetAddress[] address = null; //request.setCharacterEncoding("utf8"); String mailserver = "smtp.163.com";//发出邮箱的服务器 String From = request.getParameter("From");//发出的邮箱 String to = request.getParameter("To");//发到的邮箱 String Subject = request.getParameter("Subject");//标题 String type = request.getParameter("Type");//发送邮件格式为html String messageText = request.getParameter("Message");// 发送内容 boolean sessionDebug = false; try { // 设定所要用的Mail 服务器和所使用的传输协议 java.util.Properties props = System.getProperties(); props.put("mail.host", mailserver); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.auth", "true");//指定是否需要SMTP验证 // 产生新的Session 服务 javax.mail.Session mailSession = javax.mail.Session.getDefaultInstance(props, null); mailSession.setDebug(sessionDebug); Message msg = new MimeMessage(mailSession); // 设定发邮件的人 msg.setFrom(new InternetAddress(From)); // 设定收信人的信箱 address = InternetAddress.parse(to, false); msg.setRecipients(Message.RecipientType.TO, address); // 设定信的主题 msg.setSubject(Subject); // 设定送信的时间 msg.setSentDate(new Date()); Multipart mp = new MimeMultipart(); MimeBodyPart mbp = new MimeBodyPart(); // 设定邮件内容的类型为 text/plain 或 text/html mbp.setContent(messageText, type + ";charset=utf8"); mp.addBodyPart(mbp); msg.setContent(mp); Transport transport = mailSession.getTransport("smtp"); ////请填入你的邮箱用户名和密码,千万别用我的^_^ transport.connect(mailserver, "zhang-xinjie", "******");//设发出邮箱的用户名、密码 transport.sendMessage(msg, msg.getAllRecipients()); transport.close(); //Transport.send(msg); out.println("邮件已顺利发送"); } catch (MessagingException mex) { mex.printStackTrace(); out.println(mex); } try{ response.sendRedirect("../indexSelf.jsp");//转向某页 }catch (Exception e){ e.printStackTrace(); } %> </body> </html>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值