Skip to main content

Java Send Mail Using Gmail with Multiple Attachments


Here I am sharing a class for sending mail using Gmail SSL with multiple attachments.
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);
}
}
}

Comments

Popular posts from this blog

Shell Script to check tomcat status and restart

The below script checks the status of a particular tomcat status and restarts it if the tomcat does not respond....TOMCAT_HOME=/usr/local/tomcat-folder/

is_Running ()
{

        wget -O - http://yourserver.com/ >& /dev/null
 if( test $? -eq 0 ) then
  return 0
 else
  return 1
 fi
}


kill_Hanged_Processes ()
{
 echo "killing hanged processes......"
 javaProcs=`ps -efl| grep -v grep | grep java`
 if(test ! -z "$javaProcs") then
  echo "nonzero"
  processId=`echo $javaProcs | awk '{ print $2} '`
  echo "$processId"
  kill -9 $processId
 fi
}
stop_Tomcat ()
{
 echo "shutting down......"
 $TOMCAT_HOME/bin/shutdown.sh
}

start_Tomcat ()
{
 echo "starting......"
 $TOMCAT_HOME/bin/startup.sh
}

restart ()
{
 stop_Tomcat
 sleep 10
 kill_Hanged_Processes
 start_Tomcat
 sleep 60
}

send_Mail ()
{
#!/bin/bash
# script to send simple email
# email subject
SUBJECT="Telegraphindia.com went down"
# Email To ?
EMAIL="mailid1@gmail.com,mailid2@gmail.com"
# Em…

Getting access to menu from portlet in liferay

The below code describes the way of accessing menu items from the portlet. Here i have called it from jsp. The best thing about this is you can use the beautiful permission management of menu items of liferay as well without bothering about that. Writing services for fetching menu names from the layout tables will need more codes for permission managements......

Hope this helps.........


<%--
/**
* Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
--%>
<%@ page import="com.li…

Virus f Opyum Team

Symptoms :: 
Several processes running named f or i. 
Network choked
Apache Dead
Unknown entry  * * * * * root f Opyum Team in crontab

Investigate ::
Try to kill the f process and even root will be unable to kill it. Check file named f and i in /etc or /bin or /usr/sbin directory. Try to delete, the file f  will not delete.  Some unknown .jpg files will be there in the same folder. 

Solve :: The root is unable to delete the file f because of the immutable bit in f
To remove it use ses or chattr
Follow the below steps to remove it completely.....



remove the unknown .jpg files in system folders

#top
(kill process f) option k

#ses -i /bin/f
#rm /bin/f
#ses -i /etc/crontab
#vi /etc/crontab (and delete last line)
#reboot


Precautions ::  In my case the server was hacked bu Brute-force attack
So set a good password with a combination of upper,lower case letters and special characters.

Post your feedback or any issues you may face removing the virus.  Very less number of good threads are there explaining …