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));

`Authentication failed; nested exception is javax.mail.AuthenticationFailedException` 这个错误通常是由于身份验证失败引起的。可能的原因包括用户名或密码错误,SMTP服务器未开启身份验证,或者安全性设置太高。以下是一些可能的解决方法: 1.检查用户名和密码是否正确,确保它们与您的邮件提供商提供的相同。 2.检查SMTP服务器设置,确保已启用身份验证。如果没有提供身份验证方法,请将 `mail.smtp.auth` 设为 `false`。 3.检查安全设置,确保您的电子邮件客户端已正确配置。如果您使用的是 Gmail,请确保启用了“允许较低安全性应用程序访问您的帐户”选项。 4.如果您使用的是 Outlook.com,请确保已启用“使用与我的传入邮件服务器相同的设置”选项。 以下是一个可能的解决方案: ```java Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("yourusername@gmail.com", "yourpassword"); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress("from@example.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to@example.com")); message.setSubject("Testing Subject"); message.setText("Dear Mail Crawler," + "\n\n No spam to my email, please!"); Transport.send(message); System.out.println("Done"); } catch (MessagingException e) { throw new RuntimeException(e); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值