Sending mail

Sending mail

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 = "[email protected] ";
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 mail with attachment in jsp - JSP-Servlet
sending mail with attachment in jsp  Hi Experts
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, 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
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
mail
mail  how to send mail using jsp   Please visit the following links: JSP Send Mail Java Mail Tutorials
mail with multiple attachments
mail with multiple attachments  code for sending mail with multiple attachments in jsp
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
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
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
php send mail with attachment
php send mail with attachment  Syntax of sending email with attachment in PHP
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
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 mails - JSP-Servlet
sending mails  my intension is sending mails through java program with out installing any softwares.what is code
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
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 emails - JavaMail
sending emails  what is the code for sending emails in java  Hi Friend, Please visit the following link: http://www.roseindia.net/javamail/ Hope that it will be helpful for you. Thanks
sending data to facebook
sending data to facebook  how to post data into facebook from a java program
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
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 hidden values
Sending hidden values  how to send more than one hidden values through javascript function . Main purpose is to remove href link (Query string should not appear in url bar
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
Sending File - JSP-Servlet
Sending File  How to use tag in Jsp, & How read a file fom Client  Hi Friend, We used tag to upload a file. Please visit the following links: http://www.roseindia.net/jsp/fileupload.shtml http
E-mail Tutorials
E-mail Tutorials       Sending an email in JSP In this section, you will learn how to send
sending sms from laptop to mobile
sending sms from laptop to mobile   sending sms from laptop to mobile for multi users
sending sms from laptop to mobile
sending sms from laptop to mobile   sending sms from laptop to mobile for multi users
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
for sending project build
for sending project build  hello I want to send my iphone project build to someone else how can i send it...   yes you can send butADS_TO_REPLACE_1 Follow the steps on the portal to creating an ad hoc build
sending a zip file to servlet
sending a zip file to servlet  I have created a .zip file in a servlet on the local system(no static ip). The .zip file contains xml files. I have to send it to another servlet which is in a server(has a static ip). I have done
Sending message using Java Mail
Sending message using Java Mail        This Example shows you how to send a message... properties = System.getProperties();ADS_TO_REPLACE_2   // Setup mail
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
sending automatic email - JavaMail
sending automatic email  Dear sir. In my project i need to send an automatic email to the clients when their accounts are going to expire in 30 days.i am using jsp,mysql and tomcat for my project.Expire information are stored
ModuleNotFoundError: No module named 'sending_gmail'
ModuleNotFoundError: No module named 'sending_gmail'  Hi, My... 'sending_gmail' How to remove the ModuleNotFoundError: No module named 'sending_gmail' error? Thanks   Hi, In your python
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
sending commands through RxTx
sending commands through RxTx  i am trying to call lightOn and LightOff method from NewSerialWriter's run() method but not able to make the correct logic. please help me with this. here is my code of different classes. package

Ads