Code to attache file in email code and sent to email address:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Properties;
import java.util.Scanner;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.IOUtils;
import org.apache.tomcat.util.http.fileupload.FileItemIterator;
import org.apache.tomcat.util.http.fileupload.FileItemStream;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
/**
* @author Administrator
* Class to email notification for advertisement form with attachment of file
*/
public class Email {
public static void main(String[] args) throws IOException {
final String username = "testmail@gmail.com";
final String password = "testpassword";
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
//2) compose message
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(username));
message.setSubject("Message Aleart");
//3) create MimeBodyPart object and set your message text
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText("This is message body");
//4) create new MimeBodyPart object and set DataHandler object to this object
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
String filename = "C:\\test_doc.txt";//change accordingly
DataSource source = new FileDataSource(filename);
messageBodyPart2.setDataHandler(new DataHandler(source));
messageBodyPart2.setFileName("a2.text");
//5) create Multipart object and add MimeBodyPart objects to this object
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart1);
multipart.addBodyPart(messageBodyPart2);
//6) set the multiplart object to the message object
message.setContent(multipart );
//7) send message
Transport.send(message);
System.out.println("message sent....");
}catch (MessagingException ex) {
ex.printStackTrace();
}
}
public static void sendEmail(String content){
final String username = "testmail@gmail.com";
final String password = "testpassword";
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(username));
message.setSubject("Testing Subject");
message.setText(content);
// message.setContent(message, "charset=utf-8");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public void sendEmail(HttpServletRequest request) {
final String username = "testmail@gmail.com";
final String password = "testpassword";
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
String resultMessage = "";
try {
Message msg = new MimeMessage(session);
if(!ServletFileUpload.isMultipartContent(request)) {
return;
}
msg.setFrom(new InternetAddress(username));
InternetAddress[] toAddresses = { new InternetAddress(username) };
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject("Advertisement application form");
msg.setSentDate(new Date());
String contentDetail = "";
MimeBodyPart messageBodyPart = new MimeBodyPart();
// creates multi-part
Multipart multipart = new MimeMultipart();
// File upload option to get instance from mime type form data
ServletFileUpload file = new ServletFileUpload();
FileItemIterator iter = file.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
if (item.isFormField()) {
contentDetail += item.getFieldName() + " : "+new Scanner(item.openStream()).nextLine()+"
";
} else{
//Code to attach the file
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
InputStream input = item.openStream();
System.out.println("File name is:" + item.getName());
File tempFile = new File(item.getName());
OutputStream out = new FileOutputStream(tempFile);
out.write(IOUtils.toByteArray(input));
messageBodyPart2.attachFile(tempFile);
messageBodyPart2.setFileName(item.getName());
multipart.addBodyPart(messageBodyPart2);
}
}
messageBodyPart.setContent(contentDetail, "text/html");
multipart.addBodyPart(messageBodyPart);
// sets the multi-part as e-mail's content
msg.setContent(multipart);
// sends the e-mail
Transport.send(msg);
resultMessage = "The e-mail was sent successfully";
request.setAttribute("resultMessage", resultMessage);
} catch (Exception ex) {
ex.printStackTrace();
resultMessage = "There were an error: " + ex.getMessage();
request.setAttribute("resultMessage", resultMessage);
}
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Properties;
import java.util.Scanner;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.IOUtils;
import org.apache.tomcat.util.http.fileupload.FileItemIterator;
import org.apache.tomcat.util.http.fileupload.FileItemStream;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
/**
* @author Administrator
* Class to email notification for advertisement form with attachment of file
*/
public class Email {
public static void main(String[] args) throws IOException {
final String username = "testmail@gmail.com";
final String password = "testpassword";
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
//2) compose message
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(username));
message.setSubject("Message Aleart");
//3) create MimeBodyPart object and set your message text
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText("This is message body");
//4) create new MimeBodyPart object and set DataHandler object to this object
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
String filename = "C:\\test_doc.txt";//change accordingly
DataSource source = new FileDataSource(filename);
messageBodyPart2.setDataHandler(new DataHandler(source));
messageBodyPart2.setFileName("a2.text");
//5) create Multipart object and add MimeBodyPart objects to this object
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart1);
multipart.addBodyPart(messageBodyPart2);
//6) set the multiplart object to the message object
message.setContent(multipart );
//7) send message
Transport.send(message);
System.out.println("message sent....");
}catch (MessagingException ex) {
ex.printStackTrace();
}
}
public static void sendEmail(String content){
final String username = "testmail@gmail.com";
final String password = "testpassword";
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(username));
message.setSubject("Testing Subject");
message.setText(content);
// message.setContent(message, "charset=utf-8");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public void sendEmail(HttpServletRequest request) {
final String username = "testmail@gmail.com";
final String password = "testpassword";
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
String resultMessage = "";
try {
Message msg = new MimeMessage(session);
if(!ServletFileUpload.isMultipartContent(request)) {
return;
}
msg.setFrom(new InternetAddress(username));
InternetAddress[] toAddresses = { new InternetAddress(username) };
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject("Advertisement application form");
msg.setSentDate(new Date());
String contentDetail = "";
MimeBodyPart messageBodyPart = new MimeBodyPart();
// creates multi-part
Multipart multipart = new MimeMultipart();
// File upload option to get instance from mime type form data
ServletFileUpload file = new ServletFileUpload();
FileItemIterator iter = file.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
if (item.isFormField()) {
contentDetail += item.getFieldName() + " : "+new Scanner(item.openStream()).nextLine()+"
";
} else{
//Code to attach the file
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
InputStream input = item.openStream();
System.out.println("File name is:" + item.getName());
File tempFile = new File(item.getName());
OutputStream out = new FileOutputStream(tempFile);
out.write(IOUtils.toByteArray(input));
messageBodyPart2.attachFile(tempFile);
messageBodyPart2.setFileName(item.getName());
multipart.addBodyPart(messageBodyPart2);
}
}
messageBodyPart.setContent(contentDetail, "text/html");
multipart.addBodyPart(messageBodyPart);
// sets the multi-part as e-mail's content
msg.setContent(multipart);
// sends the e-mail
Transport.send(msg);
resultMessage = "The e-mail was sent successfully";
request.setAttribute("resultMessage", resultMessage);
} catch (Exception ex) {
ex.printStackTrace();
resultMessage = "There were an error: " + ex.getMessage();
request.setAttribute("resultMessage", resultMessage);
}
}
}
No comments:
Post a Comment