I looked at many entries on the internet without having luck.
Here is my code:
import java.io.IOException;
import java.util.Properties;
import javax
Just change the gmailUsername and gmailPass with actual gmail credentials. And also change the to and from address with your desired one. Here we have used Gmail SSL smtp port for sending mail. You can change it with your own mailing server details.
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
/**
Author Mridul Chatterjee
*/
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
public static void main(String[] args) {
Properties props = new Properties();
props.put(”mail.smtp.host”, “smtp.gmail.com”);
props.put(”mail.smtp.socketFactory.port”, “465″);
props.put(”mail.smtp.socketFactory.class”,
“javax.net.ssl.SSLSocketFactory”);
props.put(”mail.smtp.auth”, “true”);
props.put(”mail.smtp.port”, “465″);
ArrayList fileNames = new ArrayList();
fileNames.add(”C:/Write.txt”);
fileNames.add(”C:/Write1.txt”);
fileNames.add(”C:/Write2.txt”);
fileNames.add(”C:/Write3.txt”);
fileNames.add(”C:/25148.jpg”);
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(”gmailUsername”,”gmailPass”);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(”from@no-spam.com”));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(”tomail@mail.com”));
message.setSubject(”Testing Subject”);
message.setText(”Dear Mail Crawler,” +
“nn No spam to my email, please!”);
// multipart.addBodyPart(messageBodyPart);
// DataSource source = new FileDataSource(filename);
// messageBodyPart.setDataHandler(new DataHandler(source));
// messageBodyPart.setFileName(filename);
System.out.println(fileNames.size());
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
for(int i=0;i {
System.out.println(fileNames.get(i));
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource((String)fileNames.get(i));
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName((String)fileNames.get(i));
multipart.addBodyPart(messageBodyPart);
//message.setContent(multipart);
}
//multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
System.out.println(”Mail Sent Successfully….”);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}