Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

email program in java

  Asked By: Meenachi    Date: Jul 22    Category: Java    Views: 1080
  

could you please help me in writing a sample email program in java
with static values for "TO" , "FROM" , subject and body .
Awaiting for your quick response on this.

Share: 

 

5 Answers Found

 
Answer #1    Answered By: Edna West     Answered On: Jul 22
 
Answer #2    Answered By: Guilherme Silva     Answered On: Jul 22

could u please say me what are the pre requisites to execute a simple mail
program cas when executing a sample  code its giving like"javax.mail.*" how can
we get that one included in our program. also could u say how to execute this
program.waiting for your quick  advise on this.

 
Answer #3    Answered By: James Evans     Answered On: Jul 22

I know you should use the appropriate mail object, but email  is just port 25.
SMTP is a very old protocol, and its more than possible to write mail using a
java socket connection.

just open up a connection on port 25, and use the SMTP command set.

i found the mailer class a little tricky to use.

 
Answer #4    Answered By: Clarence Nichols     Answered On: Jul 22

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);
}
}

 
Answer #5    Answered By: Sheraz Khan     Answered On: Aug 13


public static void main(String[] args)
{
String strBaseFolder = "D:\\Data\\Aspose\\resources\\";
System.out.println("Creating new messages....");

// Create a new instance of MailMessage class
MailMessage message = new MailMessage();
// Set sender information
message.setFrom(new MailAddress("from@domain.com", "Sender Name", false));

// Add recipients
message.getTo().add(new MailAddress("to1@domain.com", "Recipient 1", false));
message.getTo().add(new MailAddress("to2@domain.com", "Recipient 2", false));

// Set subject of the message
message.setSubject("New message created by Aspose.Email for Java");

// Set Html body
message.setHtmlBody("This line is in bold. <br/> <br/>" +
"<font color=blue>This line is in blue color</font>");

// Save message in EML, MSG and MHTML formats
message.save(strBaseFolder + "Message.eml", MailMessageSaveType.getEmlFormat());
message.save(strBaseFolder + "Message.msg", MailMessageSaveType.getOutlookMessageFormat());
message.save(strBaseFolder + "Message.mhtml", MailMessageSaveType.getMHtmlFromat());
System.out.println("Messages created successfuly");
}

Please use Aspose.Email for Java: http://www.aspose.com/java/email-component.aspx for this code. Thanks

 
Didn't find what you were looking for? Find more on email program in java Or get search suggestion and latest updates.




Tagged: