I am trying to send an attachment by using JavaMail API, and it doesn't sem to work. Can you please tell me the mistake which I am making, the file ABC.pdf is in the same file of the project. I do get the correct path in "s" in the first sysout. The program never reaches the second sysout. I hace replace Id's with "trial" in email id.
Please help
Message message = new MimeMessage(session);
message.setSubject("Trial Messages");
message.setFrom(new InternetAddress("trial#gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("trial#yahoo.co.in"));
MimeBodyPart body = new MimeBodyPart();
body.setText(content);;
Multipart part = new MimeMultipart();
part.addBodyPart(body);
File f = new File("ABC.pdf");
String s = f.getAbsolutePath();
System.out.printf(s);
DataSource source = new FileDataSource(s);
body.setDataHandler(new DataHandler(source));
body.setFileName("ABC Bill");
part.addBodyPart(body);
System.out.printf(s);
Transport.send(message);
System.out.printf(s);
Try to replace
body.setFileName("ABC Bill");
with
body.setFileName(source.getName());
You can try here to passe source.getName() to sysout to see if everything is correct (the right file name)
add also
// add the Multipart to the message
message.setContent(part);
remove part.addBodyPart(body); because you added it twice
Message message = new MimeMessage(session);
message.setSubject("Trial Messages");
message.setFrom(new InternetAddress("trial#gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("trial#yahoo.co.in"));
MimeBodyPart body = new MimeBodyPart();
body.setText(content);;
Multipart part = new MimeMultipart();
File f = new File("ABC.pdf");
String s = f.getAbsolutePath();
System.out.printf(s);
DataSource source = new FileDataSource(s);
body.setDataHandler(new DataHandler(source));
body.setFileName("ABC Bill");
part.addBodyPart(body);
message.setContent(part);
System.out.printf(s);
Transport.send(message);
System.out.printf(s);
Related
I tried in many ways to get the reply in same thread using outlook account and javamail api but iam not able to get reply in same thread instead iam getting as attachment.
I tried to copy whole content and save in current message even then iam getting as attachment, also tried to change the content disposition as inline still it didn't work
you can find the code below which i had tried.
Properties properties = new Properties();
Session emailSession = Session.getDefaultInstance(properties,null);
store = emailSession.getStore("imaps");
store.connect(host,mailbox_username, mailbox_password);
folder = store.getFolder("Inbox");
folder.open(Folder.READ_WRITE);
Message[] unreadMessages = folder.search(new FlagTerm(new Flags(Flags.Flag.SEEN),false));
if(unreadMessages.size()>0)
{
for (int i = 0; i < unreadMessages.length; i++)
{
log.info("retriving message "+(i+1))
Message message = unreadMessages[i]
Address[] froms = message.getFrom();
String senderEmailAddress =(froms[0]).getAddress();
if(senderEmailAddress.endsWith("#gmail.com"))
{
subject = message.getSubject()
log.info(message.getSubject())
}
else
{ //reply to same mail here we need to reply to the message
Message message2 = new MimeMessage(emailSession);
message2= (MimeMessage) message.reply(false);
message2.setSubject("RE: " + message.getSubject());
//message2.setFrom(new InternetAddress(from));
message2.setReplyTo(message.getReplyTo());
message2.addRecipient(Message.RecipientType.TO, new InternetAddress(senderEmailAddress));
BodyPart messageBodyPart = new MimeBodyPart();
content = "some reply message"
//multipart.addBodyPart(content);
messageBodyPart.setText(content);
Multipart multipart = new MimeMultipart("related");
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
//messageBodyPart.setDataHandler(message.getDataHandler());
//bodyPart.setDataHandler(new DataHandler(ds));
//messageBodyPart.setHeader("Content-Type", "image/jpeg; name=image.jpg");
//messageBodyPart.setHeader("Content-ID", "<image>");
//messageBodyPart.setHeader("Content-Disposition", "inline");
//messageBodyPart.addBodyPart(bodyPart);
//msg.setContent(content);
messageBodyPart.setDisposition(MimeBodyPart.INLINE);
messageBodyPart.setContent(message, "message/rfc822");
messageBodyPart.setDataHandler(message.getDataHandler());
// Add part to multi part
multipart.addBodyPart(messageBodyPart);
// Associate multi-part with message
message2.setContent(multipart);
Transport t = emailSession.getTransport("smtp");
try {
t.connect(mailbox_username, mailbox_password);
t.sendMessage(message2, message2.getAllRecipients());
} finally {
t.close();
}
}
}
}
"inline" vs. "attachment" is just advice for the mail reader. Many ignore the device, or aren't capable of displaying all content types inline.
If you want the text of the original message to appear in the body of the reply message (e.g., indented with ">"), you need to extract the original text and reformat it appropriately, adding it to the text of the reply, then set that new String as the content of the reply message.
I have a web service that sends an email with attachments.
the code snippet that sends email is
MimeMultipart content = new MimeMultipart("related");
msg.setContent(content);
MimeBodyPart attachment = new MimeBodyPart();
File file = new File("filename.txt");
String fileName = "";
DataSource fds;
String fullPathFile = mail.getAttachment().get(i);
String pathArray[] = fullPathFile.split("/");
fds = new FileDataSource(file);
attachment.setDataHandler(new DataHandler(fds));
attachment.setHeader("Content-ID", "<" + id + ">");
attachment.setFileName(fds.getName());
content.addBodyPart(attachment);
This works fine for every email app. But in the native iPhone email app, I am unable to view the attachment.
In the image, we can see the attachment icon but when I open the email, I find no attachments
I also referred the link:
https://discussions.apple.com/thread/7491137?start=30&tstart=0
Is there a programing solution for this?
The way you attache the MimeBodyPart is causing this issue. I had the same problem. Your fix would look like:
attachments = new MimeBodyPart();
DataSource source = new FileDataSource(dest);
attachments.setDataHandler(new DataHandler(source));
attachments.setFileName(source.getName());
mp.addBodyPart(attachments);
Multipart htmlAndTextMultipart = new MimeMultipart("alternative");
MimeBodyPart htmlBodyPart = new MimeBodyPart();
htmlBodyPart.setContent(body, "text/html; charset=utf-8");
htmlAndTextMultipart.addBodyPart(htmlBodyPart);
MimeBodyPart htmlAndTextBodyPart = new MimeBodyPart();
htmlAndTextBodyPart.setContent(htmlAndTextMultipart);
mp.addBodyPart(htmlAndTextBodyPart);
am sending automatic mail using the bellow code,it will send mail some times and it will get stuck at Transport.send(message) line some times.so am not able to completely relay on this for sending mail automatically can any one help me to solve the issue .
// Create a default MimeMessage object.
ZipFolder.zipmyfolder(sResultReportFoldernew);
//ZipFolder.zipmyfolder(resultscreenshotfilepath);
Message message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
// Set Subject: header field
String filepathtomail1 = sResultReportFoldernew+".zip";
String baseurl33=urlsplit(baseurl1);
//String baseurl33=baseurl1;
sCurTime=ApplicationIndependent.getDateTime("dd-MMM-YYYY hh:mm:ss z");
message.setSubject("Automation results for "+baseurl33+" "+sCurTime);
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Now set the actual message
messageBodyPart.setText("please find the bellow attachment for the test excecution report");
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
String filepathtomail2 =resultscreenshotfilepath+".zip";
System.out.println("--------------------------beforee calling add attach");
addAttachment(multipart,filepathtomail1,baseurl1);
// Send the complete message parts
message.setContent(multipart);
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
I am using this code to send an excel file through Outlook using Java.
The excel is generated through another Java Code.It has been generated Properly.
When i use this code then the recipient receives the attachment in undefined Format.Please help
try {
msg.setFrom();
java.util.Date date=new java.util.Date();
fromAddress = new InternetAddress(from);
msg.setFrom(fromAddress);
//msg.addRecipient(Message.RecipientType.CC, to);
msg.setSubject("For Testing Purpose "+date);
//msg.setSentDate(new Date());
msg.setText("Attachment");
/*
* ************************Test for
* attachment*************************
*/
String fileAttachment = "D://testing.xlsx";
// create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
String bodytext = "Hi,<br>";
bodytext = bodytext +"<br>";
bodytext = bodytext
+ "Please find Attachnment for last week Reviewr's "+date
+ "<br>"+"</Font><br><br><br>Thanks<br>Gulshan Raj";
messageBodyPart.setContent(bodytext , "text/html");
// Part two is attachment
DataSource source = new FileDataSource(fileAttachment);
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName("Reviewers Data");
// Put parts in message
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(attachmentBodyPart);
msg.setContent(multipart);
/* ******************************************************************** */
Transport.send(msg);
System.out.println("2^^^^^^^fileAttachment "+fileNm);
System.out.println("2^^^^^^^bodytext "+bodytext);
} catch (MessagingException mex) {
System.out.println("2^^^^^^^^^^^^^^^^^^^send failed, exception: "+ mex);
}
I am using the following code to send a binary file as an email attachment in Java. The code works in that is does send the file. However, in the file that is received any hex value of $0d either deleted or converted to $0a. Since the file is a binary file not a text file the received file is incorrect. Any suggestions please?
Rgds,
Helen
String fileAttachment = "command.cmd";
Session session =
Session.getInstance(props, null);
// Define message
MimeMessage message =
new MimeMessage(session);
message.setFrom(
new InternetAddress(from));
message.addRecipient(
Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject(emailSubject);
// create the message part
MimeBodyPart messageBodyPart =
new MimeBodyPart();
//fill message
messageBodyPart.setText("Hi");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
FileDataSource source =
new FileDataSource(fileAttachment);
System.out.println("Sending");
messageBodyPart.setDataHandler(
new DataHandler(source));
messageBodyPart.setFileName(fileAttachment);
messageBodyPart.setDisposition(Part.ATTACHMENT);
multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart);
// Send the message
Transport.send( message );
Binary attachments should really be encoded in a way that their non-printable characters are gone. The most obvious way to do that coming to mind would be BASE64 encoding.