princehyderabad
asked on
Java Mail Attachment
hi experts,
I'v this code works fine now I need to modify this in order to make attachment file work:
ublic class JavaMail {
public String sendMail(java.lang.String from, java.lang.String to)
throws IOException, ServletException, AddressException, SendFailedException, MessagingException, NamingException
{
try {
String textmessage = "some msg....";
InitialContext ctx = new InitialContext();
Session mailSession = (Session) ctx.lookup("java:comp/env/ mail/xz");
Message message = new MimeMessage(mailSession);
//Set the from address
InternetAddress[] fromAddress = InternetAddress.parse(from );
message.addFrom(fromAddres s);
//Set the to address
InternetAddress[] toAddress = InternetAddress.parse(to);
message.setRecipients(Mess age.Recipi entType.TO ,toAddress );
//Set the subject and text
message.setSubject(subject );
message.setText(textmessag e);
//send mail
Transport.send(message);
}
catch(AddressException e) {
status = "Address Exception: "+e;
System.out.println(status) ;
} catch(SendFailedException e) {
status = "Send Failed Exception: "+e;
System.out.println(status) ;
} catch (MessagingException e) {
status = "MessagingException: "+e;
System.out.println(status) ;
} catch (NamingException e) {
status = "Naming Exception: "+e;
System.out.println(status) ;
} catch (Exception e) {
status = "General Exception: "+e;
System.out.println(status) ;
}
return status;
} // END sendMail
}
and the way I'm calling above class in another class:
..
JavaMail rm = new JavaMail();
jm.sendMail(FromEmailAddre ss, ToEmailAddress);
..
Please let me what changes do I need to go for
thx,
PH
I'v this code works fine now I need to modify this in order to make attachment file work:
ublic class JavaMail {
public String sendMail(java.lang.String from, java.lang.String to)
throws IOException, ServletException, AddressException, SendFailedException, MessagingException, NamingException
{
try {
String textmessage = "some msg....";
InitialContext ctx = new InitialContext();
Session mailSession = (Session) ctx.lookup("java:comp/env/
Message message = new MimeMessage(mailSession);
//Set the from address
InternetAddress[] fromAddress = InternetAddress.parse(from
message.addFrom(fromAddres
//Set the to address
InternetAddress[] toAddress = InternetAddress.parse(to);
message.setRecipients(Mess
//Set the subject and text
message.setSubject(subject
message.setText(textmessag
//send mail
Transport.send(message);
}
catch(AddressException e) {
status = "Address Exception: "+e;
System.out.println(status)
} catch(SendFailedException e) {
status = "Send Failed Exception: "+e;
System.out.println(status)
} catch (MessagingException e) {
status = "MessagingException: "+e;
System.out.println(status)
} catch (NamingException e) {
status = "Naming Exception: "+e;
System.out.println(status)
} catch (Exception e) {
status = "General Exception: "+e;
System.out.println(status)
}
return status;
} // END sendMail
}
and the way I'm calling above class in another class:
..
JavaMail rm = new JavaMail();
jm.sendMail(FromEmailAddre
..
Please let me what changes do I need to go for
thx,
PH
ASKER
hi kawas
You just copied from :http://www.jguru.com/faq/view.jsp?EID=30251
I already knew this.
What I'm looking is how to modify my existing code so that attachment code can be added. And how to make call to my JavaMail class when attachment code is ready. See my 2nd half question the way I'm calling my javamail class without attchment code. Same way I want to know how to make call to Javamail class when attachment code is updated.
THX,
PH
You just copied from :http://www.jguru.com/faq/view.jsp?EID=30251
I already knew this.
What I'm looking is how to modify my existing code so that attachment code can be added. And how to make call to my JavaMail class when attachment code is ready. See my 2nd half question the way I'm calling my javamail class without attchment code. Same way I want to know how to make call to Javamail class when attachment code is updated.
THX,
PH
public class JavaMail {
public String sendMail(java.lang.String from, java.lang.String to)
throws IOException, ServletException, AddressException, SendFailedException, MessagingException, NamingException
{
try {
String textmessage = "some msg....";
InitialContext ctx = new InitialContext();
Session mailSession = (Session) ctx.lookup("java:comp/env/ mail/xz");
Message message = new MimeMessage(mailSession);
//Set the from address
InternetAddress[] fromAddress = InternetAddress.parse(from );
message.addFrom(fromAddres s);
//Set the to address
InternetAddress[] toAddress = InternetAddress.parse(to);
message.setRecipients(Mess age.Recipi entType.TO ,toAddress );
//Set the subject and text
message.setSubject(subject );
message.setText(textmessag e);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mess ageBodyPar t);
// attachment
messageBodyPart = new MimeBodyPart();
DataSource source =
new FileDataSource(fileAttachm ent);
messageBodyPart.setDataHan dler(
new DataHandler(source));
messageBodyPart.setFileNam e(fileAtta chment);
multipart.addBodyPart(mess ageBodyPar t);
// Put parts in message
message.setContent(multipa rt);
//send mail
Transport.send(message);
}
catch(AddressException e) {
status = "Address Exception: "+e;
System.out.println(status) ;
} catch(SendFailedException e) {
status = "Send Failed Exception: "+e;
System.out.println(status) ;
} catch (MessagingException e) {
status = "MessagingException: "+e;
System.out.println(status) ;
} catch (NamingException e) {
status = "Naming Exception: "+e;
System.out.println(status) ;
} catch (Exception e) {
status = "General Exception: "+e;
System.out.println(status) ;
}
return status;
} // END sendMail
}
public String sendMail(java.lang.String from, java.lang.String to)
throws IOException, ServletException, AddressException, SendFailedException, MessagingException, NamingException
{
try {
String textmessage = "some msg....";
InitialContext ctx = new InitialContext();
Session mailSession = (Session) ctx.lookup("java:comp/env/
Message message = new MimeMessage(mailSession);
//Set the from address
InternetAddress[] fromAddress = InternetAddress.parse(from
message.addFrom(fromAddres
//Set the to address
InternetAddress[] toAddress = InternetAddress.parse(to);
message.setRecipients(Mess
//Set the subject and text
message.setSubject(subject
message.setText(textmessag
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mess
// attachment
messageBodyPart = new MimeBodyPart();
DataSource source =
new FileDataSource(fileAttachm
messageBodyPart.setDataHan
new DataHandler(source));
messageBodyPart.setFileNam
multipart.addBodyPart(mess
// Put parts in message
message.setContent(multipa
//send mail
Transport.send(message);
}
catch(AddressException e) {
status = "Address Exception: "+e;
System.out.println(status)
} catch(SendFailedException e) {
status = "Send Failed Exception: "+e;
System.out.println(status)
} catch (MessagingException e) {
status = "MessagingException: "+e;
System.out.println(status)
} catch (NamingException e) {
status = "Naming Exception: "+e;
System.out.println(status)
} catch (Exception e) {
status = "General Exception: "+e;
System.out.println(status)
}
return status;
} // END sendMail
}
ASKER
thanks kawas, I think I'm asking basci stuff.
By the way can U also tell me how to call this javamail. My previous code is called in this way:
JavaMail rm = new JavaMail();
jm.sendMail(FromEmailAddre ss, ToEmailAddress);
Now that attachment is also need to pass here, how can i make work.
By the way can U also tell me how to call this javamail. My previous code is called in this way:
JavaMail rm = new JavaMail();
jm.sendMail(FromEmailAddre
Now that attachment is also need to pass here, how can i make work.
ASKER
Atleast for testing that code let me know how to attached file
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
so bascially this is the way I should call my javamail class from other jsp or class as:
JavaMail rm = new JavaMail();
jm.sendMail(FromEmailAddre ss, ToEmailAddress, fileAttachment);
JavaMail rm = new JavaMail();
jm.sendMail(FromEmailAddre
yep, i forgot to say that. sorry
You can also make it a static method, like a utility:
public static void sendMail ( .... )
{
}
And call it using:
JavaMail.sendMail ( .... ) ;
public static void sendMail ( .... )
{
}
And call it using:
JavaMail.sendMail ( .... ) ;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class AttachExample {
public static void main (String args[])
throws Exception {
String host = args[0];
String from = args[1];
String to = args[2];
String fileAttachment = args[3];
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host"
// Get session
Session session =
Session.getInstance(props,
// Define message
MimeMessage message =
new MimeMessage(session);
message.setFrom(
new InternetAddress(from));
message.addRecipient(
Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject(
"Hello JavaMail Attachment");
// create the message part
MimeBodyPart messageBodyPart =
new MimeBodyPart();
//fill message
messageBodyPart.setText("H
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mess
// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source =
new FileDataSource(fileAttachm
messageBodyPart.setDataHan
new DataHandler(source));
messageBodyPart.setFileNam
multipart.addBodyPart(mess
// Put parts in message
message.setContent(multipa
// Send the message
Transport.send( message );
}
}