Issue with javax.mail and attached file - jakarta-mail

I want to send an email with javax.mail.
It works on ubuntu 14.04 and java 1.8.0_121 and tomcat7
With ubuntu 18.04 and java 1.8.0_181 and tomcat8 I am getting this error :
Caused by: java.io.IOException: "text/html" DataContentHandler
requires String object, was given object of type class
javax.mail.internet.MimeMultipart
Here is the code :
MimeMessage mex = new MimeMessage(session);
mex.setFrom(new InternetAddress(from));
mex.addRecipient(RecipientType.TO, new InternetAddress((String)((List)ccList).get(0)));
mex.setRecipients(RecipientType.BCC, from);
mex.setSubject(subject);
MimeMultipart var26 = new MimeMultipart();
MimeBodyPart attachBodyPart = new MimeBodyPart();
attachBodyPart.setText(messageBody);
var26.addBodyPart(attachBodyPart);
attachBodyPart = new MimeBodyPart();
byte[] data = baos.toByteArray();
new FileDataSource(fileName);
attachBodyPart.setDisposition("attachment");
attachBodyPart.setContent(data, "application/pdf");
attachBodyPart.setFileName(fileName);
var26.addBodyPart(attachBodyPart);
mex.setContent(var26, "text/html");
Transport transport = session.getTransport("smtp");
transport.connect(param.getSmtpHost(), from, pass);
transport.sendMessage(mex, mex.getAllRecipients());
transport.close();
Do you have any idea?

It's hard to believe this same code works anywhere.
Change
attachBodyPart.setText(messageBody);
to
attachBodyPart.setText(messageBody, "html");
Change
mex.setContent(var26, "text/html");
to
mex.setContent(var26);

Related

selenium c# edge browser : 'Unexpected error. Unknown error'

OS Build Number:- 18363.900
Edge Version: 83.0.478.54
MicrosoftWebDriver Version: 83.0.478.54
Here is the code:
System.Environment.SetEnvironmentVariable("webdriver.edge.driver", "E:\\edgedriver_win64\\msedgedriver.exe");
IWebDriver driver = new EdgeDriver();
I am getting error as: OpenQA.Selenium.WebDriverException: 'Unexpected error. Unknown error'
Which version of Selenium WebDriver are you using? I suggest you to use the latest version 4.0.0-alpha05 and use the code below:
EdgeOptions edgeOptions = new EdgeOptions();
edgeOptions.UseChromium = true;
edgeOptions.BinaryLocation = #"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
var msedgedriverDir = #"D:\webdriver83.0.478.54";
var driver = new EdgeDriver(msedgedriverDir, edgeOptions);
driver.Navigate().GoToUrl("https://bing.com");
Thread.Sleep(3000);
driver.Close();
Note: Change the paths in the code to your owns.
Result:

Message with Embeded image not work in JavaMail

It is part of code. When i dont use this code message sending.
messageBodyPart=new MimeBodyPart();
DataSource fds=new FileDataSource("btn2.png");
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID","<image>");
messageBodyPart.addHeader("Content-Type","image/png");
messageBodyPart.setDisposition(MimeBodyPart.INLINE);
multipart.addBodyPart(messageBodyPart);
But when i add this code message not sending.
And this is full code.
Properties props=System.getProperties();
String fromm="ffff#ffff.fffff.fff";
String pass="ffffffffff";
String to="afffffffffff#gmail.com";
props.put("mail.smtp.starttls.enable",true);
props.put("mail.smtp.host","ggg.ggg.ggg.gggg");
props.put("mail.smtp.user",fromm);
props.put("mail.smtp.password",pass);
props.put("mail.smtp.port","587");
props.put("mail.smtp.auth",true);
Session session=Session.getInstance(props,null);
MimeMessage message=new MimeMessage(session);
InternetAddress from=new InternetAddress(fromm);
message.setSubject("Salam mailiniz var");
message.setFrom(from);
message.addRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
Multipart multipart=new MimeMultipart();
BodyPart messageBodyPart=new MimeBodyPart();
String htmlText="<H1>Hello from the outside Hello, can you hear me?</H1><img src=\"cid:image\">";
messageBodyPart.setContent(htmlText,"text/html; charset=utf-8");
multipart.addBodyPart(messageBodyPart);
messageBodyPart=new MimeBodyPart();
DataSource fds=new FileDataSource("btn2.png");
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID","<image>");
messageBodyPart.addHeader("Content-Type","image/png");
messageBodyPart.setDisposition(MimeBodyPart.INLINE);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport transport=session.getTransport("smtp");
transport.connect("mail.mct.gov.az",fromm,pass);
transport.sendMessage(message,message.getAllRecipients());

Cant download file from webservice using MTOM and Axis2 stub

I m trying to download a file from my Axis2 webservice server using MTOM and ADB.
I can download the file if I dont enable the MTOM both on server and the client sides. Any suggestions or code sample would be nice :)
Client side
ServerWSStub stub = new ServerWSStub();
stub._getServiceClient().getOptions().setProperty(Constants.Configuration.ENABLE_MTOM,Constants.VALUE_TRUE);
Server side axis2.xml
<parameter name="enableMTOM">optional</parameter>
This is my Server
public DataHandler download(String konum) throws Exception {
System.out.println("The filePath for download: " + konum);
FileDataSource dataSource = new FileDataSource(konum);
DataHandler datahandler = new DataHandler(dataSource);
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace ns = fac.createOMNamespace("http://benim.projem.org/dosya", "dosyam");
OMText textData = fac.createOMText(datahandler, true);
OMElement ele = fac.createOMElement("sonuc", ns);
ele.addChild(textData);
System.out.println(ele);
return datahandler;
This is my Client
ServerWSStub stub = new ServerWSStub();
//stub._getServiceClient().getOptions().setProperty(Constants.Configuration.ENABLE_MTOM,Constants.VALUE_TRUE);
//when uncommented i get java.lang.NoClassDefFoundError: org/apache/james/mime4j/MimeException
//while trying to invoke _operationClient.execute(true); in ServerWSStub
//I guess it is because of wrong unparsing
Download download = new Download();
download.setKonum(konum);
try {
DownloadResponse downloadResponse = stub.download(download);
DataHandler dh =(DataHandler) downloadResponse.get_return();
File file = new File("C:/dosya/"+fileNameType);
if (!file.getParentFile().exists())
file.getParentFile().mkdirs();
if(!file.exists()){
file.createNewFile();
}
FileOutputStream fileOutputStream = new FileOutputStream(file);
dh.writeTo(fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
} catch (ServerWSExceptionException e) {
e.printStackTrace();
}
Any
I finally got the solution I guess. The Stream closes before the client gets the whole file thats why first I used the getOptions().setTimeOutInMilliSeconds(10000) method but it was also useless and then in the Stub file I commented
_messageContext.getTransportOut().getSender().cleanup(_messageContext);//look for the method's finally part
part so that during a large file transportation the stream had not been closed and i could download the whole file without any silly exceptions :)
--MIMEBoundary_e56e8a77b94fbdd7678582aa5ca53f50b1d56c0d828499ea
Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
Content-Transfer-Encoding: binary
Content-ID: <0.146e8a77b94fbdd7678582aa5ca53f50b1d56c0d828499ea#apache.org>
<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns:downloadResponse xmlns:ns="http://servis.ws.projem.tez.benim"><ns:return><xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:1.046e8a77b94fbdd7678582aa5ca53f50b1d56c0d828499ea#apache.org" /></ns:return></ns:downloadResponse></soapenv:Body></soapenv:Envelope>
--MIMEBoundary_e56e8a77b94fbdd7678582aa5ca53f50b1d56c0d828499ea
Content-Type: text/plain
Content-Transfer-Encoding: binary
Content-ID: <1.046e8a77b94fbdd7678582aa5ca53f50b1d56c0d828499ea#apache.org>
binary code here
Simply add apache-mime4j-core-0.7.2.jar to WEB-INF/lib on the service (server) side. The jar can be found here.

Tika 1.2 PDF parse error - org.apache.pdfbox.cos.COSString cannot be cast to org.apache.pdfbox.cos.COSDictionary

I am using Solr 4.0 and DIH (data import handler) with TikaProcessor for extracting text from PDF files stored in database. When I run indexing it gets failed to parse some PDF files and got the stack trace mentioned below.
Since Solr 4.0 uses Tika 1.2 I have written a unit test to parse the same PDF file using Tika 1.2 API, I got the same error.
The same problem with Tika 1.3 jars also. But when I tried using Tika 1.1 jars it works fine. Please let me if any of you have seen this error and how to fix this?
(I have posted the same in tika mailing list, but not much luck)
When I open the PDF file it is showing PDF/A mode. Not sure if this something related to the problem.
Here is the exception:
org.apache.tika.exception.TikaException: Unexpected RuntimeException from org.apache.tika.parser.pdf.PDFParser#1fbfd6<mailto:org.apache.tika.parser.pdf.PDFParser#1fbfd6>
at org.apache.tika.parser.CompositeParser.parse(CompositeParser.java:244)
at org.apache.tika.parser.CompositeParser.parse(CompositeParser.java:242)
at org.apache.tika.parser.AutoDetectParser.parse(AutoDetectParser.java:120)
at com.pc.TikaWithIndexing.main(TikaWithIndexing.java:53)
Caused by: java.lang.ClassCastException: org.apache.pdfbox.cos.COSString cannot be cast to org.apache.pdfbox.cos.COSDictionary
at org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink.getAction(PDAnnotationLink.java:93)
at org.apache.tika.parser.pdf.PDF2XHTML.endPage(PDF2XHTML.java:148)
at org.apache.pdfbox.util.PDFTextStripper.processPage(PDFTextStripper.java:444)
at org.apache.pdfbox.util.PDFTextStripper.processPages(PDFTextStripper.java:366)
at org.apache.pdfbox.util.PDFTextStripper.writeText(PDFTextStripper.java:322)
at org.apache.tika.parser.pdf.PDF2XHTML.process(PDF2XHTML.java:66)
at org.apache.tika.parser.pdf.PDFParser.parse(PDFParser.java:153)
at org.apache.tika.parser.CompositeParser.parse(CompositeParser.java:242)
... 3 more
Here is the code snippet in JAVA:
String fileString = "C:/Bernard A J Am Coll Surg 2009.pdf";
File file = new File(fileString );
URL url = file.toURI().toURL();
ParseContext context = new ParseContext();;
Detector detector = new DefaultDetector();;
Parser parser = new AutoDetectParser(detector);;
Metadata metadata = new Metadata();
context.set(Parser.class, parser); //PPt,word,xlsx-- pdf,html
ByteArrayOutputStream outputstream = new ByteArrayOutputStream();
InputStream input = TikaInputStream.get(url, metadata);
ContentHandler handler = new BodyContentHandler(outputstream);
parser.parse(input, handler, metadata, context);
input.close();
outputstream.close();

Exception to fix javax.mail.AuthenticationFailedException exception

I am learning how to send an email with javamail API, i have created the necessary properties and instructions to send a simple email using SMTP server, and i am using this code :
Properties props=new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session= Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getpPasswordAuthentication(){
return new PasswordAuthentication("myemailadresse#gmail.com", "password");
}
});
try{
Message message=new MimeMessage(session);
message.setFrom(new InternetAddress("myemail"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recepientemailadresse"));
message.setSubject("the java mail test");
message.setText("Guess what brother the java mail is working correctly");
Transport.send(message);
JOptionPane.showMessageDialog(rootPane, "message sent");
}
catch(Exception e){
JOptionPane.showMessageDialog(rootPane, e);
e.printStackTrace();
}
and i the run time an exception occurred mentioning that :
javax.mail.AuthenticationFailedException: failed to connect, no password specified?
at javax.mail.Service.connect(Service.java:329)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at transfer.Maitest.jButton1ActionPerformed(Maitest.java:96)
at transfer.Maitest.access$000(Maitest.java:20)
at transfer.Maitest$1.actionPerformed(Maitest.java:45)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
would you tell me what did i miss please ??
First, read this JavaMail FAQ entry about common mistakes. After correcting them, read this JavaMail FAQ entry that tells you how to connect to Gmail. If it still doesn't work, this JavaMail FAQ entry about debugging will help.
If your email server does not require authentication and you don't want to supply a password (for example, test environment), try this:
props.put("mail.smtp.auth", "false");

Resources