Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JavaMail Sending mail
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


naveen
Sending mail
2 Answer(s)      7 years ago
Posted in : JavaMail


Need a simple example of sending mail in Java


Advertisement
View Answers

February 23, 2008 at 12:08 AM


Hi,

To send email you need a local mail server such as apache james. You first download apache james mail server and install on the local network.

Then create users for to send and received email.

Then your clients will send and receive emails using outlook client.

From java program you can also send email.

Here is an example code to send emails:

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class Mail extends Thread{
public java.lang.String from = "";
public java.lang.String[] to ;
public java.lang.String message = "";
Session mailSession;
public java.lang.String subject = "";
public java.lang.Exception error;
public Mail() {
super();
Properties props = new Properties();
props.put("mail.smtp.host", "your.mail.server.name");
mailSession = Session.getDefaultInstance(props, null);
}
public java.lang.Exception getError() {
return error;
}
public java.lang.String getFrom() {
return from;
}
public java.lang.String getMessage() {
return message;
}
InternetAddress[] getRecipients(){
InternetAddress[] recipient= new InternetAddress[to.length];
for ( int i=0 ; <recipient.length ; i++ ) {
try {
recipient[i]= new InternetAddress(to[i]);
} catch ( Exception e ) {}
}
return recipient;
}
public java.lang.String getSubject() {
return subject;
}
public java.lang.String[] getTo() {
return to;
}
public static void main(String[] args) {

Mail mail=new Mail();
mail.from="from.user@host";
String[] s={"to.user@host"};
mail.to=s;

mail.message="Test";
mail.send();

}
public void run(){
try {
Message msg = new MimeMessage(mailSession);
InternetAddress fromAddress = new InternetAddress(from);
msg.setFrom(fromAddress);
InternetAddress[] address = getRecipients();
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
} catch ( Exception e ) { error=e; }
}
public boolean send() {
run();
if ( error!=null ) {
Utility.debug("SendMessage : from:"+from+",to:"+to+"\n"+error);
return false;
}
return true;

}
public void setError(java.lang.Exception newError) {
error = newError;
}
public void setFrom(java.lang.String newFrom) {
from = newFrom;
}
public void setMessage(java.lang.String newMessage) {
message = newMessage;
}
public void setSubject(java.lang.String newSubject) {
subject = newSubject;
}
public void setTo(java.lang.String[] newTo) {
to = newTo;
}
}

Thanks


February 23, 2008 at 3:59 PM


Java Code for sending messages(text or multimedia) from 1 pc to another in LAN.

Example Code

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.smtp.SMTPTransport;
import java.io.*;
import java.util.*;
public class MailSender
{

private Transport transport;
private Message message;
private Session session;
public static void main(String[] args) throws Exception{
MailSender mailSender = new MailSender();
String from = "mail_mevinod@yahoo.com ";
String to[] = new String[]{"vinod@localhost"};
String subject = "news";
String content = "Hi, how are you?";
mailSender.setMessage(from,to,subject,content);
mailSender.setSMTPServer("localhost",25,null,null);
mailSender.send();
}
public void setMessage(String fromAddress,String toAddress,String subject,String content)
throws Exception
{
String[] addresses = {toAddress};
setMessage(fromAddress,addresses,subject,content);
}
public void setMessage(String fromAddress,String[] toAddresses,String subject,String content)
throws Exception
{
session = Session.getInstance(System.getProperties());
message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromAddress));
InternetAddress[] toIntAdds = new InternetAddress[toAddresses.length];
for (int i=0;i<toAddresses.length;i++)
toIntAdds[i] = new InternetAddress(toAddresses[i]);
message.setRecipients(Message.RecipientType.TO,toIntAdds);
message.setSubject(subject);
message.setSentDate(new java.util.Date());
message.setText(content);
File f = new File("Message.properties");
Properties pro = new Properties();
FileInputStream in = new FileInputStream(f);
pro.load(in);
//String companyName="Cinbell";
//String messageIdKey=pro.getProperty(companyName);
//System.out.println("messageIdKey:"+messageIdKey);
message.setHeader(messageIdKey,"1000");
/* File f = new File("Message.properties");
Properties pro = new Properties();
FileInputStream in = new FileInputStream(f);
pro.load(in);
Enumeration em = pro.keys();
while(em.hasMoreElements()){
String key = (String)em.nextElement();
String val = pro.getProperty(key);
message.setHeader(val,"1000"); } */
}
public void setSMTPServer(String host,int port,String user,String password) throws Exception{
transport = new SMTPTransport(session,new URLName(host));
transport.connect(host,port,null,null); }
public void send() throws Exception{
message.saveChanges();
transport.sendMessage(message, message.getAllRecipients());
System.out.println("Message is sent.");
transport.close(); } }


Related Tutorials/Questions & Answers:
sending mail - JSP-Servlet
sending mail  Hi, what is the code for sending mail automatically without user intervention? thanks in advance
sending mail using jsp
sending mail using jsp  please give me the detailed procedure and code for sending mail through jsp program   Please visit the following links: http://www.roseindia.net/ejb/introduction/j2eedemo.shtml http
Advertisements
java mail sending with images
java mail sending with images  I need to send images through java mail without giving content path(i.e. we don't want hard code the image path)can you tell me the idea?   Please visit the following links: http
sending a mail - JSP-Servlet
sending a mail   I m writing a code for send mail in jsp,i sending a mail to specified receiver but if i attach an file and send a mail then i m getting an error. Here Attachment is in String[] Attachment=null so how
iphone mail sending problem
iphone mail sending problem  Hi, I'm receiving the following error ... while sending mail in my iphone application Terminating app due to uncaught... it done myself... actually in my send mail code i have predefined the mailto id
Sending mail - JavaMail
Sending mail  Need a simple example of sending mail in Java  Hi,To send email you need a local mail server such as apache james. You first download apache james mail server and install on the local network.Then create
Sending images through java mail
and by clicking one image the control go to mail sending page in that the image should add to body part and the mail will sent to recipients..... please give me any idea...Sending images through java mail  Am trying to develop greeting
Sending mail (Contact Form in a website)
Sending mail (Contact Form in a website)  Hi, I just uploaded few of my webpages into a free web hosting service, where i can just upload the web pages. Am very much new into this field. I want to write a contact form, which
sending mail with attachment in jsp - JSP-Servlet
sending mail with attachment in jsp  Hi Experts
sending mail with attachment in jsp - JSP-Servlet
sending mail with attachment in jsp  Hi, Can any one pls tell me how to send email with attachment in jsp. I know how to send mail without attachment but with attachment its not working for me. If u hve any idea please
values of the "from address & to addresses" while sending a mail to localhost using javamail
values of the "from address & to addresses" while sending a mail to localhost... in JavaMail in ur website.I tried 1st program for sending the Mail.It's... is,What i have to mention in from address field and To address filed while sending
Email queue while sending mail using Struts Class
Email queue while sending mail using Struts Class  Can I maintain a queue of mails being sent from a JSP page in a DB to get its status
Email queue while sending mail using Struts Class
Email queue while sending mail using Struts Class  Can I maintain a queue of mails being sent from a JSP page in a DB to get its status
Cheking birthday excel sheet everyday and Sending Automated MAIL to birthday boy on his birthday
Cheking birthday excel sheet everyday and Sending Automated MAIL to birthday... anyone help me out in the procedure for sending automated mail to the birthday... = systemdate, 2) Auto mail should be sent to paticular person with all the other
when sending the mail from java I am getting the below error - JavaMail
when sending the mail from java I am getting the below error   when sending the mail from java I am getting the below error : Exception... not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:/mail/mail.jar,file
i have split a string by space ,done some operation and now how to properly order them for sending a mail in java or jsp - JSP-Servlet
order them for sending a mail in java or jsp  Dear sir, I have... arraylist and used that string as a argument ,passed in a sending a mail method .Now my problem is that after sent a mail when a receiver got that mail
mail
mail  how to send mail using jsp   Please visit the following links: JSP Send Mail Java Mail Tutorials
mail
mail  I wrote a program to send mail using smtp. But there is an error message " could not connect to smtp.gmail.com port 465. what is the problem how can i solve it ...   Please visit the following link: Java Mail API
mail with multiple attachments
mail with multiple attachments  code for sending mail with multiple attachments in jsp
sending mails - JSP-Servlet
sending mails   sending mail using smtp protocal ,while running,i got error an javax.mail.sendfailed exception. what is this error
php send mail with attachment
php send mail with attachment  Syntax of sending email with attachment in PHP
Sending email without authentication
/mail/sending-an-email-in-jsp.shtml...Sending email without authentication  Hi sir, Am doing a project in JSP, in that i want to send mail without any authentication of password so send
send mail using smtp in java
send mail using smtp in java  How to send mail using smtp in java?   Sending mail in JSP - SMTP
sending email code - JSP-Servlet
sending email code  How To Send Emails using jsp  Hi friend, I am sending you a link. This link will help you. Please visit for more information. http://www.roseindia.net/mail/sending-an-email
java mail programs
java mail programs  I got some codes from this about sending mail,forwarding mail,multipart mail etc..and I compiled and executed it,but it did... output.actually,do I need a mail server to get correct output of these codes?plz help me, I
Sending email with read and delivery requests
Sending email with read and delivery requests  Hi there, I am sending emails using JavaMail in Servlets on behalf of a customer from the website..., Gareth   Please visit the following link: JSP Servlet Send Mail
Sending Emails In Java
Sending Emails In Java  I want to send emails from within a java program. I saw some online java programs to do that, but they needed me to enter... with how to write the code?   Please visit the following link: Java Mail
Sending Emails In Java
Sending Emails In Java  I want to send emails from within a java program. I saw some online java programs to do that, but they needed me to enter... with how to write the code?   Please visit the following link: Java Mail
Sending Emails In Java
Sending Emails In Java  I want to send emails from within a java program. I saw some online java programs to do that, but they needed me to enter... with how to write the code?   Please visit the following link: Java Mail
java mail api - JavaMail
and Tutorials on Mail visit to : http://www.roseindia.net/mail/sending-an-email...java mail api  Hi, Pls give me the detailed information on how... points : 1.Download the Apache Mail server (James) http://james.apache.org
mail problem
for sending mail!"); } catch(Exception e){ out.println(e.getMessage...mail problem  Sir, I tried ur send mail example but it giveng...; <title>Mail API</title> </head> <body><table
Advertisements
 

 

 

DMCA.com