here is the code to send an attachment:
import java.util.properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*; 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 session
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);
// put parts in message
message.setcontent(multipart);
// send the message
transport.send( message );
}
}
闽公网安备 35060202000074号