email - Sending mail attachment using Java -
i trying send email using java , g mail have stored files on cloud , stored files want send attachment mail.
it should add files mail , not links of files.
how can send such attachments ?
working code, have used java mail 1.4.7 jar
import java.util.properties; import javax.activation.*; import javax.mail.*; public class mailprojectclass { public static void main(string[] args) { final string username = "your.mail.id@gmail.com"; final string password = "your.password"; 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.mail.id@gmail.com")); message.setrecipients(message.recipienttype.to, internetaddress.parse("to.mail.id@gmail.com")); message.setsubject("testing subject"); message.settext("pfa"); mimebodypart messagebodypart = new mimebodypart(); multipart multipart = new mimemultipart(); messagebodypart = new mimebodypart(); string file = "path of file attached"; string filename = "attachmentname"; 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(); } } }
Comments
Post a Comment