• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with sending mail

 
Chennarao Marvatu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i am new to this site..My issue is sending mail throu command line
For this i have to implement one interface and used Vo object..


1) Mailer.java --this is the interface

package com.scalar.mail.commons;
import com.scalar.mail.commons.*;

import java.util.ArrayList;

public interface Mailer
{
public void sendMail(MailVo mailVo) throws Exception ;
//public void sendMail(ArrayList mailVoList) throws Exception;
}


2) MailVo.java --Vo object class

package com.scalar.mail.commons;
import com.scalar.mail.commons.*;

import java.util.*;
import java.io.Serializable;

public class MailVo implements Serializable
{
public String m_to=null;
public String m_from=null;
public String m_cc=null;
public String m_bcc=null;
public String m_subject=null;
public String m_message=null;
//public String[] m_fileAttachements =null;
//public String m_contentType=null;
//public boolean m_qualifyEmail=null;

public MailVo()
{}
public void setTo(String to) {
m_to=to;
}
public String getTo() {
return m_to;
}
public void setFrom(String from) {
m_from=from;
}
public String getFrom() {
return m_from;
}
/*public void setCC(String cc) {
m_cc=cc;
}
public String getCC() {
return m_cc;
}
public void setBcc(String bcc) {
m_bcc=bcc;
}
public String getBcc() {
return m_bcc;
}*/
public void setSubject(String subject) {
m_subject=subject;
}
public String getSubject() {
return m_subject;
}
public void setBody(String message) {
m_message=message;
}
public String getBody() {
return m_message;
}
/*public void setFileAttachements(String message) {
m_message=message;
}
public String[] getFileAttachements() {
return m_fileAttachements;
} */
/*public void setContentType(String contentType) {
m_contentType=contentType;
}
public String getContentType() {
return m_contentType;
};//
/*public void setQualifyEmail(String qualifyEmail) {
m_qualifyEmail=qualifyEmail;
}
public String getQualifyEmail() {
return m_qualifyEmail; */

}

3) MailManager.java
package com.scalar.mail.commons;
import com.scalar.mail.commons.*;

public class MailManager implements Mailer
{

public Mailer getMailer()
{
Mailer mailer = new MailManager();
return mailer;
}
}

4)SimpleMailer.java

package com.scalar.mail.commons;
import com.scalar.mail.commons.*;

import javax.mail.*;
import javax.mail.internet.*;
//import javax.activation.*;
//import javax.mail.Transport;

import java.util.Date;
import java.util.Properties;
import java.util.ArrayList;
import java.util.Iterator;

public class SimpleMailer
{
public static void main(String args[])
{

// public static void sendMail(MailVo mailVo) throws Exception
public static void sendMail( MailVo mailVo) throws Exception
{
System.out.println("Test Mail");
Properties props = System.getProperties();
props.put("mail.smtp.host","192.168.1.111");
Session session = Session.getDefaultInstance(props, null);

MailVo mailVo = new MailVo();
try
{
MimeMessage msg = new MimeMessage(session);

msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));
msg.setSubject(subject);
msg.setText(body);
//msg.setCC(new InternetAddress(mailVo.getCC()));
//msg.setBcc(new InternetAddress(mailVo.getBcc()));
//msg.setRecipients(Message.RecipientType.CC, addressCC);
Transport.send(msg);

} catch(MessagingException me)
{
me.printStackTrace();
}
};
}
}

5) Test.java

package com.scalar.mail.commons;
import com.scalar.mail.commons.*;

public class Test
{
public static void main(String args[]) {

try{
//MailManager m = new MailManager();
MailVo mailVo= new MailVo();

//String smtpServer = args[0];
String to = args[0];
String from = args[1];
String subject = args[2];
String body = args[3];

mailVo.setTo(to);
mailVo.setFrom(from);
mailVo.setSubject(subject);
mailVo.setBody(body);

MailManager m = new MailManager();
Mailer mailer = m.getMailer();
mailer.sendMail(mailVo);

//mv.setTo(args[0]);
//mv.setFrom(args[1]);
//mv.setCC(args[2]);
//mv.setBcc(args[3]);
//mv.setSubject(args[4]);
//mv.setBody(args[5]);
//mv.setContentType(args[6]);
//Mailer mailer = m.getMailer();
//mailer.sendMail(mv);
}
catch(Exception e) { e.printStackTrace(); }
}
}

he i have to excute Test.java so i need to pass the MailVo object to
sendMail() from SimpleMailer and access the variables

pls modify this code for working in command line

thanks in advance
Chennarao
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chinnu:

Please read our Naming Convention document. Please change your display name to comply. (We are looking for bath a first and a last name.)

Thanks,
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!