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());
Related
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);
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.
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");
i have to check for the attachments in the mail thrugh java. For that firstly getting the multipart object of the message through getcontent() as follows.
Multipart mp=(Multipart)msg.getContent()
but even if mail have attachment and the message part is simple text then it throws an exception as -- java.lang.ClassCastException: java.lang.String cannot be cast to javax.mail.Multipart
So how to get attachments for simple message mails..?
thanks..in advance.
You can use the MimeMessage constructor which accepts an InputStream.
Message msg = new MimeMessage(mySession,
new ByteArrayInputStream(myString.getBytes()));
See JavaMail documentation
I'm trying to send a PDF attachment through JavaMail, but when I receive the email, I get something like this, without any attachments in the mail:
===EMAIL CONTENT===
------=_Part_0_3786439.1313701770148
Content-Type: application/pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=ems_report.pdf
JVBERi0xLjQKJeLjz9MKMyAwIG9iago8PC9MZW5ndGggMTE4Ni9GaWx0ZXIvRmxhdGVEZWNvZGU+
PnN0cmVhbQp4nLWaTW/jNhCG7/oVBIoF0kMYfgw/1FviZNsEa+/WVrCHogc3VgIXsb1VnS7670vK
lhynMjmxRQSOBA1JPXpfaTSSyAgn55wworlw/x8W2V/ZVZFJTZTRpJhlN0X2aybInd/q2zHStC4W
2cVHTvzaY3b2Y/Gnb7trUg/G6pXqKROMqFy4QRU5l25LmT2+HtFtUoZRadpRrR/VLd2f6352M5yQ
8ctyWq1eljMyWC2+Pc+ny4eSjMtvq2pNHlcVGd0WNx6DkacOlN9+d8tZfSjtsNttb1q+4sqBKG6p
5S0XeK72sM52TLfL+Xo+Xa+qv8nkZbGYVv8iWfYFozlwd/CvlpbJ3O/LszBLOGOdEnJmqLUEbE7l
zhxW41Ln7fa3D/1xXi0OYwry3UXutv21C4x/bti3NIuMb9ees4nr33Tw5JvfXhcwTRe/5rscKwUY
s5FCdEghBdWcgJYdUgBpfl6Kd5wzWDgeoROOzhuFoDPG5D3TQYROSUsBUHQSemZTETYNnAqBYhM9
o4kImnyHbLpnNhlhA+HYJIrNLTX5kOCCCGUuwTSFnECuKJfhzPW5mpUuwY7Lh3L+TzlrQPcuLTeS
lVT5gd52L1br6fPhowunL96mPIFOebzNeeLUnAcRFRVzSQ8l4i79b+R8q6KSLtnrwyp+mk//eC6P
lRFaGRVaRmhlVKfKqCIyaiejwp2MMR214PV5fUjH0Wp5/nySlqrVUqO1VK2W+lQtRURL2dspKVNe
===CODE===
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart part1 = new MimeBodyPart();
FileDataSource source = new FileDataSource("ems_report.pdf");
part1.setDataHandler(new DataHandler(source));
part1.setFileName(source.getName());
part1.setHeader("Content-Type", "application/pdf");
part1.setHeader("Content-Transfer-Encoding", "base64");
part1.setDisposition(Part.ATTACHMENT);
multipart.addBodyPart(part1);
MimeBodyPart part2 = new MimeBodyPart();
part2.setText("mimebodypart part2");
multipart.addBodyPart(part2);
message.setContent(multipart);
message.setHeader("Content-Type", "multipart/mixed");
What did I do wrong here? I've googled for the whole day and no one seems to have the solution. Any help is appreciated, thanks so much!
What you're seeing is a binary data of the multipart. You first have to decode the base64 string. After that you can process the PDF.
PS: If you open that mail in a client (Webmail, Outlook, Thunderbird) you should see the PDF correctly.