Below is the code which shows the way to send the java mail with multiple attachments & also the way to send mails using SSl & TLS authentication.
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.URLDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.SendFailedException;
import javax.mail.Transport;
import java.util.*;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public int SendMail(String subject, String messageText,String TO,String cc,ArrayList arr)
throws MessagingException
{
URL url=null;
String username = "";
String password = "";
String mail_transport_protocol = "";
String mail_smtp_port = "";
String mail_smtp_host = "";
try
{
username = <SMTP USERNAME>;
password = <SMTP PASSWORD>;
mail_transport_protocol = smtp;
mail_smtp_port = <PORT>;
mail_smtp_host = <SMTP HOST NAME>;
Properties props = new Properties();
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", 587);
props.setProperty("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.host",smtp.gmail.com);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
// props.put("mail.store.protocol", "pop3");
// props.put("mail.transport.protocol", "smtp");
// props.put("mail.debug", "true");
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
MimeMessage message = new MimeMessage(session);
message.setContent(messageText, mail_messageText);
message.setSubject(subject);
int index=0;
while(index < arr.size())
{
HashMap data=(HashMap) arr.get(index);
messageBodyPart = new MimeBodyPart();
url = new URL("http://domain:8080/"+data.get("doc"+index));
String filename=data.get("doc"+index).toString();
filename=filename.substring(filename.lastIndexOf("/")+1,filename.length());
//String extension=filename.substring(filename.lastIndexOf(".")+1,filename.length());
URLDataSource sourcedocs = new URLDataSource(url);
messageBodyPart.setDataHandler(new DataHandler(sourcedocs));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
index++;
}//while
message.setContent(multipart);
Transport.send(message);
}try
catch(Exception e)
{
System.out.println((new StringBuilder("Error in sending mail:")).append(e).toString());
}//catch
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
No comments:
Post a Comment