email - Java Mail with Attachment-Attachment file location not found -
my code sending email without attachment works fine. when try send email wth attachment, getting error message
'c:\temp\test.txt (the system cannot find file specified)' code below: '
package com.abe.test; import java.util.properties; import javax.mail.*; import javax.activation.*; import javax.mail.internet.internetaddress; import javax.mail.internet.mimebodypart; import javax.mail.internet.mimemessage; import javax.mail.internet.mimemultipart; public class testjavamail { /** * @param args */ public static void main(string[] args) { final string username = "from@gmail.com"; final string password = "xxxx"; properties props = new properties(); props.put("mail.smtp.auth", true); props.put("mail.smtp.starttls.enable", true); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); session session = session.getinstance(props, new javax.mail.authenticator() { protected passwordauthentication getpasswordauthentication() { return new passwordauthentication(username, password); } }); try { message message = new mimemessage(session); message.setfrom(new internetaddress("from@gmail.com")); message.setrecipients(message.recipienttype.to, internetaddress.parse("to@gmail.com")); message.setsubject("testing subject 1"); message.settext("pfa"); mimebodypart messagebodypart = new mimebodypart(); multipart multipart = new mimemultipart(); messagebodypart = new mimebodypart(); string file = "c:\\temp"; string filename = "test.txt"; datasource source = new filedatasource(file); messagebodypart.setdatahandler(new datahandler(source)); messagebodypart.setfilename(filename); multipart.addbodypart(messagebodypart); message.setcontent(multipart); system.out.println("sending"); transport.send(message); system.out.println("done"); } catch (messagingexception e) { e.printstacktrace(); } } }
do need set classpath or else file attachment working?
thanks
Comments
Post a Comment