//Step3:create a body part to hold the "text" portion of the message
System.out.println("Constructing 'text' body part");
MimeBodyPart textBodyPart=new MimeBodyPart();
textBodyPart.setText(messageText);
//Step 4:Create a body part to hold the "file" portion of the message
System.out.println("Attaching 'file' body part:"+fileName);
MimeBodyPart fileBodyPart=new MimeBodyPart();
FileDataSource fds=new FileDataSource(fileName);
fileBodyPart.setDataHandler(new DataHandler(fds));
fileBodyPart.setFileName(fds.getName());
System.out.println("Finished attaching file");
//Step 5:Create a Multipart/container and add the parts
Multipart container=new MimeMultipart();
container.addBodyPart(textBodyPart);
container.addBodyPart(fileBodyPart);
//Step 6:Add the Multipart to the actual messge
testMessage.setContent(container);
System.out.println("Messge constructed");
//Step 7:Now send the message
Transport.send(testMessage);
System.out.println("Message sent.");
}
public static void main(String[] args){
String smtpHost="smtp.citiz.net";
String from="??@citiz.net";
String to="??@citiz.net";
String filename="D:/dd.txt";
String subject="Test message";
StringBuffer theMessage=new StringBuffer();
theMessage.append("ddddhello22,\n\n");
theMessage.append("Hope all is well on you end.\n");
theMessage.append("Cheers");
try{
QuickMailAttach.sendMessage(smtpHost,from,to,subject,theMessage.toString(),filename);
}catch(javax.mail.MessagingException exc){
exc.printStackTrace();
}
}
static class SmtpAuth extends javax.mail.Authenticator {
private String user,password;
public void getuserinfo(String getuser,String getpassword){
user = getuser;
password = getpassword;
}
protected javax.mail.PasswordAuthentication getPasswordAuthentication(){
return new javax.mail.PasswordAuthentication(user,password);
}
}
}