CodeCharge Studio
search Register Login  

Visual PHP Web Development

Visually Create Internationalized Web Applications, Web Reports, Calendars, and more.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> Java

 How to Send email with the help of JavaMailAPI

Print topic Send  topic

Author Message
Saikat
Posted: 11/22/2005, 3:28 PM

Quote :
The MailUser bean class

:-):-):-):-):-):-):-):-):-):-)

package mailbeans;

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
//import java.sql.*;
import java.text.*;
import java.util.*;

import mailbeans.TestIp;

public class MailUser
{

private String id,password;
public Vector mailInfo;

public MailUser()
{

}

public MailUser(String id,String password)
{
this.id=id;
this.password=password;
}

public void setId(String id)
{
this.id=id;
}

public void setPassword(String password)
{
this.password=password;
}

public String getId()
{
return id;
}

public String getPassword()
{
return password;
}

public boolean isValidUser(String hostName)
{
boolean found = false;
try
{
Properties props=new Properties();
Session session=Session.getDefaultInstance(props,null);
Store store=session.getStore("pop3");
store.connect(hostName,this.id,this.password);
found=true;
}
catch(Exception e)
{
found = false;
}
return found;
}

public void sendMail(String to,String cc,String bcc,String subject,String myTxt,String smtpHost) throws Exception
{
Properties props=new Properties();
props.put("mail.smtp.host",smtpHost);
Session session=Session.getInstance(props);
Message msg=new MimeMessage(session);
msg.setFrom(new InternetAddress(this.id));
msg.setSubject(subject);
msg.setSentDate(new java.util.Date());
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
msg.setRecipients(Message.RecipientType.CC,InternetAddress.parse(cc));
msg.setRecipients(Message.RecipientType.BCC,InternetAddress.parse(bcc));
msg.setContent(myTxt,"text/plain");
Transport t=session.getTransport("smtp");
Transport.send(msg);
}

public void sendMailWithAttach(String to,String cc,String bcc,String subject,String myTxt,String myDirStr,String smtpHost) throws Exception
{
Properties props=new Properties();
//props.put("mail.transport.protocol","smtp");
//props.put("mail.smtp.host",smtpHost);
//props.put("mail.smtp.port","25");
props.put("mail.smtp.host",smtpHost);
Session session=Session.getInstance(props);
Message msg=new MimeMessage(session);
msg.setFrom(new InternetAddress(this.id));
msg.setSubject(subject);
msg.setSentDate(new java.util.Date());
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
msg.setRecipients(Message.RecipientType.CC,InternetAddress.parse(cc));
msg.setRecipients(Message.RecipientType.BCC,InternetAddress.parse(bcc));
Multipart mailBody=new MimeMultipart();
MimeBodyPart mainBody=new MimeBodyPart();
mainBody.setText(myTxt);
mailBody.addBodyPart(mainBody);
java.io.File myDir=new java.io.File(myDirStr);
String[] myFiles=myDir.list();
for(int i=0;i<myFiles.length;i++)
{
System.out.println(myDirStr+myFiles);
FileDataSource fds=new FileDataSource(myDirStr+myFiles);
MimeBodyPart mimeAttach=new MimeBodyPart();
mimeAttach.setDataHandler(new DataHandler(fds));
mimeAttach.setFileName(fds.getName());
mailBody.addBodyPart(mimeAttach);
}
msg.setContent(mailBody);
Transport.send(msg);
}

public Message[] getMyMails(String hostName) throws Exception
{
Properties props=new Properties();
Session session=Session.getDefaultInstance(props,null);
Store store=session.getStore("pop3");
store.connect(hostName,this.id,this.password);
Folder folder=store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message[] msg=folder.getMessages();
return msg;
}

public void foo(String hostName) throws Exception
{
Properties props=new Properties();
Session session=Session.getDefaultInstance(props,null);
Store store=session.getStore("pop3");
store.connect(hostName,this.id,this.password);
URLName url = store.getURLName();

System.out.println(url);
Folder folder=store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
//folder.delete(true);
//folder.delete(true);
//folder.open(Folder.READ_ONLY);
folder.create(Folder.HOLDS_FOLDERS);
Message[] msg=folder.getMessages();
}

public Vector getMailInfo(String hostName) throws Exception
{
mailInfo=new Vector();
Message [] msg=getMyMails(hostName);
for(int i=0;i<msg.length;i++)
{

String s=msg.getFrom()[0].toString();
if(s==null)
s="- N/A -";
else
s=s.replace(s.charAt(0),s.toUpperCase().charAt(0));
mailInfo.add(s);//<<<<<<<<<<<<<
s=msg.getSubject();
if(s==null)
s="- N/A -";
mailInfo.add(s);//<<<<<<<<<<<<<
Date d=msg.getSentDate();
SimpleDateFormat df = new SimpleDateFormat("EE MMM d , yyyy");
if(d==null)
mailInfo.add("- N/A -");//<<<<<<<<<<<<<
else
mailInfo.add(df.format(d));//<<<<<<<<<<<<<
int size = msg.getSize();
if(size<1024)
mailInfo.add(size+" bytes");
else if(size<1024*1024)
{
size /= 1024;
mailInfo.add(size+" kb");
}
else
{
size/=1024*1024;
mailInfo.add(size+" mb");
}
//<<<<<<<<<<<<<

}
return mailInfo;
}

public int getMailCount(String hostName) throws Exception
{
Message [] msg=getMyMails(hostName);
return msg.length;
}

public Vector getDetails(String hostName,int mailNo) throws Exception
{
mailInfo=new Vector();
Message [] msg = getMyMails(hostName);
String recpTo = "";
int i=0;
Address [] addTo = msg[mailNo].getRecipients(Message.RecipientType.TO);
if(addTo == null)
recpTo = "- N/A - ";
else
{
recpTo+="<";
for(;i<addTo.length-1;i++)
{
recpTo +=addTo.toString()+"> , <";
}
recpTo += addTo.toString()+">";
}
mailInfo.add(recpTo);//<<<<<<<<<<<<<

String recpCc = "";
i=0;
Address [] addCc = msg[mailNo].getRecipients(Message.RecipientType.CC);
if(addCc == null)
recpCc = " ";
else
{
recpCc+="<";
for(;i<addCc.length-1;i++)
{
recpCc += addCc.toString()+"> , <";
}
recpCc += addCc.toString()+">";
}
mailInfo.add(recpCc);//<<<<<<<<<<<<<

String s = msg[mailNo].getFrom()[0].toString();
mailInfo.add(s);

Date d=msg.getSentDate();
SimpleDateFormat df = new SimpleDateFormat("EE MMM d , yyyy");
if(d==null)
mailInfo.add("- N/A -");//<<<<<<<<<<<<<
else
mailInfo.add(df.format(d));//<<<<<<<<<<<<<

s = msg[mailNo].getSubject();
if(s==null || s.equals(""))
s = "- N/A - ";
mailInfo.add(s);//<<<<<<<<<<<<<

s = msg[mailNo].getContent().toString();
if(s == null)
s = "- N/A - ";
mailInfo.add(s);//<<<<<<<<<<<<<

return mailInfo;
}
}

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

MS Access to Web

Convert MS Access to Web.
Join thousands of Web developers who build Web applications with minimal coding.

CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.