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", host);
Get sessions
Session session =
Session.getinstance (props, null);
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 ("Hi");
Multipart Multipart = new Mimemultipart ();
Multipart.addbodypart (Messagebodypart);
Part Two is attachment
Messagebodypart = new MimeBodyPart ();
DataSource Source =
New Filedatasource (fileattachment);
Messagebodypart.setdatahandler (
New DataHandler (source));
Messagebodypart.setfilename (fileattachment);
Multipart.addbodypart (Messagebodypart);
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.