on 12-10-2008 9:50 AM
Namaste,
Can somebody guide me how to use FileUpload UI to attach a file for mail sending program.
I am able to get the file into the context but not understanding how to get the file attachment in the mail
Regards,
kM
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Krishna
Use following code
public class FirstMail {
public static void main(String[] args) {
try{
Properties props=new Properties();
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.host",ServerName);
props.put("mail.smtp.port",Port No);
Session session=Session.getInstance(props);
MimeMessage message=new MimeMessage(session);
Multipart mailContent=new MimeMultipart();
MimeBodyPart mailText=new MimeBodyPart();
Address from=new InternetAddress(from id);
Address toAddress = new InternetAddress(to address);
message.setFrom(from);
message.setRecipient(Message.RecipientType.TO,toAddress);
message.setSubject("My Test");
//setting the mail content
mailText.setText("Test");
mailContent.addBodyPart(mailText);
//attaching the picture
FileDataSource fds=new FileDataSource(the path of the file to be attached);
MimeBodyPart mmattch=new MimeBodyPart();
mmattch.setDataHandler(new DataHandler(fds));
mmattch.setFileName(fds.getName());
mailContent.addBodyPart(mmattch);
message.setContent(mailContent);
Transport.send(message);
}catch(Exception e){
System.out.println(e.toString());
}
}
}
Regards
Ruturaj
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ruturaj/Santo
The file to be attached would be dynamic. but this code requires the file path to be hardcoded.
In case of FileUpload UI, it works as in any other normal mail senders like gmail or outlook where in u can browse the file to be attached in the mail.
Anyways, thanks for your immediate reply!
Keep up the good work..
kM
HI,
Below is the code to attch a file to mail, Uisng java mail API.
If you want I will send u the project, U can direclty use it.
//Add the message to an output stream
if(wdContext.currentContextElement().getCtx_checkerrors()){
try {
wdContext.currentContextElement().setCtx_Rtf(msgBody.toUpperCase());
final String formattedStr = wdContext.currentContextElement().getCtx_Rtf();
ByteArrayOutputStream outfile = new ByteArrayOutputStream();
outfile.write(formattedStr.getBytes());
//String mailHost = "bcchub01.brm.pri";
String mailHost = "bcchub02.notes.brm.pri";
Properties prop = System.getProperties();
prop.put("mail.smtp.host", mailHost);
Session session = Session.getInstance(prop,null);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(wdContext.currentContextElement().getCtx_lmemail()));
if (wdContext.nodeTa_Dir_Emails().size()< 1 )//case when all roles to be removed
{
IWDNodeElement roleInpt = wdContext.nodeZ_Get_Custom_Roles_Input().currentZ_Get_Custom_Roles_InputElement();
roleInpt.setAttributeValue("Im_Businessarea", "ADULTS");
roleInpt.setAttributeValue("Im_Directorate", "DIRECTORATE FINANCE");
wdThis.wdGetRUCFormRolesCompController().executeZ_Get_Custom_Roles_Input();
}
wdThis.setDirectorateMailAddress();
String targetEmail = wdContext.currentContextElement().getCtx_directoratemail();
//String lineMgrMail = wdContext.currentContextElement().getCtx_lmemail();
//message.setRecipients(Message.RecipientType.TO, new InternetAddress[] { new InternetAddress(wdContext.currentContextElement().getCtx_directoratemail())});
//if target mail empty case when user being removed of all role
message.setRecipients(Message.RecipientType.TO, new InternetAddress[] { new InternetAddress(targetEmail.toLowerCase())});
message.setRecipients(Message.RecipientType.CC, new InternetAddress[] {new InternetAddress("mail address")});
// message.setRecipients(Message.RecipientType.CC, new InternetAddress[] {new InternetAddress("")});
message.setSubject("Voyager Request User Capability Form For User-"+ newUserName.toUpperCase());
//Added
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Voyager Request User Capability Form");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
//attachment
messageBodyPart = new MimeBodyPart();
CSTDataSource cds = new CSTDataSource(outfile, "APPLICATION/OCTET-STREAM");
messageBodyPart.setDataHandler(new DataHandler(cds));
messageBodyPart.setFileName(newUserName.toUpperCase()"'s RUCForm"".doc");
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Regards,
Bala
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
hi krishna,
You design the mail sending layout in any format, include the file upload UI also inside that .Let the user to select the file and press button 'attach'.And on that button's action retrieve the file name which user has selected and use that file name in the code which i have sent to you.
Thanks and Regards
shanto aloor
Use this code for fileUpload.
IPrivateRUCFormView.IContextElement element = wdContext.currentContextElement();
IWDResource resource = element.getFileResource();
int temp=0;
String path = null;
StringBuffer displayText = new StringBuffer(0);
try
{
InputStream text=null;
File file = new File(wdContext.currentContextElement().getFileResource().getResourceName().toString());
FileOutputStream op = new FileOutputStream(file);
if(wdContext.currentContextElement().getFileResource()!=null)
{
text=wdContext.currentContextElement().getFileResource().read(false);
while((temp=text.read())!=-1)
{
op.write(temp);
}
}
op.flush();
op.close();
path = file.getAbsolutePath();
*****************************************************************
But the above code will upload the file to server path in the portal.
U need modify it.
Regards,
Bala
User | Count |
---|---|
84 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.