import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import java.awt.*;
import javax.activation.*;
/**
* A Class class.
* <P>
* @author Ozgur Ugur
*/
public class Mailman {
public static boolean sendMail (String HOST,String FROM,String TO,String
subject,String messagetxt) throws Exception {
String host = HOST;
String from = FROM;
String to = TO;
String username="ougur";
String password="1";
// Get system properties
Properties props = System.getProperties();
// Properties props = new Properties();
// Setup mail server
props.put("mail.smtp.host", host);
// Get session
Session session = Session.getDefaultInstance(props, null);
// Define message
MimeMessage message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress(from));
// Set the to address
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set the subject
message.setSubject(subject);
// Set the content
message.setText(messagetxt);
// Send message
message.saveChanges(); // implicit with send()
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
//Transport.send(message);
return true;
}
public static boolean sendMail (String HOST,String SMTP_USER,String
SMTP_PWL,String FROM,String TO,String subject,String messagetxt) throws
Exception {
String host = HOST;
String from = FROM;
String to = TO;
String username=SMTP_USER;
String password=SMTP_PWL;
// Get system properties
Properties props = System.getProperties();
// Properties props = new Properties();
// Setup mail server
props.put("mail.smtp.host", host);
// Get session
Session session = Session.getDefaultInstance(props, null);
// Define message
MimeMessage message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress(from));
// Set the to address
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set the subject
message.setSubject(subject);
// Set the content
message.setText(messagetxt);
// Send message
message.saveChanges(); // implicit with send()
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
//Transport.send(message);
return true;
}
/***************************************
************ attachment
********************************************/
public static boolean sendMail (String HOST,String SMTP_USER,String
SMTP_PWL,String FROM,String TO,String subject,String messagetxt,String
AttachedFile) throws Exception {
String host = HOST;
String from = FROM;
String to = TO;
String username=SMTP_USER;
String password=SMTP_PWL;
// Get system properties
Properties props = System.getProperties();
// Properties props = new Properties();
// Setup mail server
props.put("mail.smtp.host", host);
// Get session
Session session = Session.getDefaultInstance(props, null);
// Define message
MimeMessage message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress(from));
// Set the to address
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
// Set the subject
message.setSubject(subject);
// Set the content---------------------------------------
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText(messagetxt);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(AttachedFile);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(AttachedFile);
multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart);
// Set the content--------------------------------------->
// Send message
message.saveChanges(); // implicit with send()
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
//Transport.send(message);
return true;
}
/*********************************************/
public static void main(String[] params){
String from="",to="",subject="",msg="",host="",user="a",pwl="a";
if(params[0].equals("help")){
printUsage();
System.exit(0);
}
try{
for(int i=0;i<9;i++){
System.out.println(params[i].trim());
}
}catch(Exception eese){
}
from=params[0].trim();
to=params[1].trim();
subject=params[2].trim().replace('_',' ');
msg=params[3].trim().replace('_',' ').replace('~','\n');
host=params[4].trim();
try{
if(params[5].trim().equals("1")){
user=params[6].trim();
pwl=params[7].trim();
if(params[8].trim().equals("1")){
Mailman.sendMail(host,user,pwl,from,to,subject,msg,params[9].trim());
}else{
Mailman.sendMail(host,user,pwl,from,to,subject,msg);
}
}else{
if(params[8].trim().equals("1")){
Mailman.sendMail(host,user,pwl,from,to,subject,msg,params[9].trim());
}else{
Mailman.sendMail(host,from,to,subject,msg);
}
}
}catch(Exception eee){
eee.printStackTrace();
System.exit(1);
}
}
public static void printUsage(){
String t="";
t="com.infotech.utils.mailman.Mailman <from> <to> <subject> <message>
<smtp_host> <authenticate_user> [<smtp_user>] [<smtp_password>]\n"+
"Use ~ for crlf character \n"+
"Use _ for space character\n"+
"Example\n"+
"java com.infotech.utils.mailman.Mailman ozgur_ugur_ce@...
ougur@... testing_connection msg_budur~gibi_duruyor inet 1 ougur
testpwl 1 debugger.cfg";
System.out.println(t);
}
}