cancel
Showing results for 
Search instead for 
Did you mean: 

Javamail with attachments

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

hi ,

read the following blog it is explaining how to send mails with attachements using java.I think u need not use the file upload only neee to get the file path.

Thanks and Regards

shanto aloor

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi Krishna

Please check this block link

Hope it will help you

Regards

Ruturaj

Former Member
0 Kudos

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

Former Member
0 Kudos

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

Former Member
0 Kudos

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

Former Member
0 Kudos

Bala,

does your program use FileUpload UI? but i don't find the dictionary type 'Resource' used anywhere in the pgm.

Regards,

kM

Former Member
0 Kudos

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

Former Member
0 Kudos

Hi Murthy,

I directly Form data to mail using word attachment.

I have also done excel upload excel download, Then only u need resource property.

If u r directly sending form data as attachment it is not rewquired.

Regards,

Bala

Former Member
0 Kudos

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

Former Member
0 Kudos

Bala,

Can you send me your project to krish.mur at gmail

Thanks,

kM

Former Member
0 Kudos

Hi,

I sent the project to you.

Check it.

Regards,

Bala

Former Member
0 Kudos

Hi Bala..

Am also lookiing for same requirment , need a java mail feature in WDP application could u plz send the project that u have .

mymail Id is "rajeshkelamatgmaildotcom"

Thnaks in Advance

Regards

Rajesh

piyush_kumar6
Active Contributor
0 Kudos

Hi Bala,

Can you send me the project for sending mail with attachment.

Regards,

Piyush