Home Answers Viewqa JavaMail Sending mail

 
 


naveen
Sending mail
2 Answer(s)      5 years and 4 months ago
Posted in : JavaMail

Need a simple example of sending mail in Java

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 Pages:
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
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
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
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
mail with multiple attachments
mail with multiple attachments  code for sending mail with multiple attachments in jsp
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
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
php send mail with attachment
php send mail with attachment  Syntax of sending email with attachment in PHP
java mail
(); out.println("Thanks for sending mail!"); } catch(Exception e
regarding sending mesage - JavaMail
regarding sending mesage  i have tried the following program... properties = System.getProperties(); // Setup mail server... me in this regard   Hi friend, I am sending you a link
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
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
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 an email in JSP
Sending an email in JSP       Sending an email in JSP In this section, you will learn how to send an email in jsp.   Following is a simple JSP page for sending
Sending emails and insert into trable
Sending emails and insert into trable  I have created a form, once people submit it should go to the email and insert to table also, the second part...(); @mail($email_to, $email_subject, $email_message, $headers); $query="insert
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
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
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 api - Development process
mail api  hi to all, it,s abhishek................. actually i am using mail api for sending the mail with attachments(audio and video)..... suppose i am sending a video which will be open on real player 3.21.. so i wants
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
send the mail with attachment - Struts
send the mail with attachment   Hi Freinds, i am sending the mail without attachment file now. But i need to send the mail with attachment... the mail . Mail Problem is file path: how to get the file path from the browse("we
E-mail Tutorials
E-mail Tutorials       Sending an email in JSP In this section, you will learn how to send
java mail - Java Beginners
java mail  how to send a mail without authentication one domain....  Hi, You have to use the Java mail api. You need to use the SMTP... is tutorials on Java Mail API: http://www.roseindia.net/javamail/index.shtml http
JAVA MAIl - JavaMail
JAVA MAIl  Hi, I am trying to send a mail using java mail api. But I am getting following error: javax.mail.SendFailedException: Sending failed; nested exception is: javax.mail.MessagingException: Could not connect
why mails are sending from the linux server - JavaMail
java application,mail are not sending .Mail server is installed on windows 2003...why mails are sending from the linux server  Mails are not sending from the linux server.We have 3 systems. server system is windows 2003 server
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
sending email using smtp in java
sending email using smtp in java  Hi all, I am trying to send and email to through my company mail server. Following is my code package com.tbss... IHGWTMEX07.SerWizSol.com Microsoft ESMTP MAIL Service ready at Wed, 21 Dec 2011 14
Java Mail - JMS
, For solving the problem visit to : http://www.roseindia.net/mail/sending-an-email... running very well,creating no other problems. Now i am trying to learn java mail service. I hav written a sample mail program to just test the code.The code
mail
mail  how to send mail using jsp   Please visit the following links: JSP Send Mail Java Mail Tutorials
Introduction of Java Mail
of Electronic Mail for example- composing, reading, sending text mails and also...; Sending message using Java Mail This Example shows you... Introduction of Java Mail     
java mail API
(); out.println("Thanks for sending mail!"); } catch(Exception e){ out.println...java mail API  <html> <head> <title>Mail API<...%" cellpadding="0" cellspacing="0"> <h1>Mail API</h1> <tr>
Sending message using Java Mail
Sending message using Java Mail        This Example shows you how to send a message... = System.getProperties();   // Setup mail server   
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
Java Mail - JSP-Servlet
Java Mail  hi, i refer ur J2EE Tutorial - Send Email From JSP & Servlet to send emails from my application.but when i run the jsp it will throw thhese exceptions. ERROR.....javax.mail.SendFailedException: Sending failed
Mail from JSP with SMTP - JavaMail
Mail from JSP with SMTP  Hi, Can any one pls guide me how to send mail from JSp page by using SMTP IP address. Regards...,Britto.M  Hi Friend, Please visit the following link: http://www.roseindia.net/mail
how to send a mail - JSP-Servlet
when a receiver gets a mail ,the matter will be shown in a single line . I am sending a following matter Dear Harini , U r bonus is 2000 . Regards Hr. But when a receiver got a this mail as a Dear Harini,U r bonus
Email - Electronic mail
is electronic mail format and API for sending, receiving and electronic mail...Email - Electronic mail In this article we will understand the E-mail and see... internet users. We prefer email over standard mail. Email is free