• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

File attachment in java mail

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I've the following code for sending java mail with attachment. Its sending the mail but not attaching the file. Please could someone help. Thanks
/* Bean Properties */

private String to = "mail@xxx.com";
private String from = null;
private String subject = null;
private String message = null;
private File attach =null;
public static Properties props = null;
public static Session session = null;

static {
/*Setting Properties for STMP host */
props = System.getProperties();
props.put("mail.smtp.host", "mail.xxx.com");
session = Session.getDefaultInstance(props, null);
}
/* Setter Methods */
public void setTo(String to) {
this.to = to;
}

public void setFrom(String from) {
this.from = from;
}

public void setSubject(String subject) {
this.subject = subject;
}

public void setMessage(String message) {
this.message = message;
}

public void setAttach(File attach) {
this.attach = attach;
}
/* Sends Email */
public void sendMail() throws Exception {
if(!this.everythingIsSet())
throw new Exception("Could not send email.");
try {
MimeMessage message = new MimeMessage(session);
message.setRecipient(Message.RecipientType.TO,
new InternetAddress(this.to));

message.setFrom(new InternetAddress(this.from));
message.setSubject(this.subject);
message.setText(this.message);
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(this.message);

// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();

// attach the file to the message
javax.activation.DataSource fds = new FileDataSource(attach);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());

// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);

// add the Multipart to the message
message.setContent(mp);
message.setText(this.message);
Transport.send(message);



}
catch (MessagingException e) {
throw new Exception(e.getMessage());
}
}
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't use both
message.setContent(mp);
message.setText(this.message);
in the same piece of code!
Use message.setText(...) for emails without attachments and message.setContent(...) for emails with attachments.
 
Delanthi J
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hiya, I got the following error message after taking the setText() out.
Sending failed; nested exception is: javax.mail.MessagingException: IOException while sending message; nested exception is: java.io.FileNotFoundException: C:\j3t\attach.txt (No such file or directory)
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error message is telling you the program is not able to find the file "C:\j3t\attach.txt".
 
Nilesh Pereira
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if C:\j3t\attach.txt does exist, then try
C:\\j3t\\attach.txt or even C:/j3t/attach.txt.
Hope that helps.
 
Delanthi J
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
William, i can see, the program couldnt find the file, but can you see why.
Nilesh, that didnt work.
Thanks
 
Nilesh Pereira
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure the file exists?
I just took your code posted above, compiled and executed it, and it worked fine. Sent the attachment and everything.
 
Delanthi J
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it doesnt work on mine, thanks anywhere. What sort of smpt host are you using.
 
Delanthi J
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nilesh, if you dont mind can I see the code that you compiled please.
Thanks
 
Nilesh Pereira
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The SMTP server I used was an iMail server, but that shouldn't make any difference. All I did was put your code into a class, insert a few system outs, and add a main method.
 
Delanthi J
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nilesh, thanks for that code, Its sort of working. I didnt use the main method, i hard coded everything in the bean property. Now the test is to take these values from the form.
Thanks xx
 
Delanthi J
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks to Nilesh.
 
Nilesh Pereira
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Delanthi,
You are welcome. But for the record, the only reason I could help you was because I had already made those errors before.
 
permaculture is largely about replacing oil with people. And one tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic