Delanthi J

Greenhorn
+ Follow
since Apr 14, 2003
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Delanthi J

Many thanks to Nilesh.
20 years ago
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
20 years ago
Nilesh, if you dont mind can I see the code that you compiled please.
Thanks
20 years ago
Well, it doesnt work on mine, thanks anywhere. What sort of smpt host are you using.
20 years ago
William, i can see, the program couldnt find the file, but can you see why.
Nilesh, that didnt work.
Thanks
20 years ago
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)
20 years ago
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());
}
}
20 years ago