cancel
Showing results for 
Search instead for 
Did you mean: 

java code to send mails

Former Member
0 Kudos

Hi,

My problem is I have to save all the details that are shown in the texboxes to the database and to send mail to the address that is shown in the text box.Both these action should be done in one button click.Can anybody help me or send me the java code for this?

With Regards,

Jaisamma

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Jaisamma,

Using SMTP you can send mails through java code. Refer the following links...

Send mail using JavaMail API -->

http://java.sun.com/developer/onlineTraining/JavaMail/contents.html

http://search400.techtarget.com/tip/1,289483,sid3_gci1135063,00.html?Offer=wn2_1130

Send mail from JSP/Servlet -->

http://www.oop-reserch.com/smtpsender.html

Regards,

Uma

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Jaisamma,

Here is a simple program used to send mails using JavaMail. In JavaMail you'll find APIs and provider implementations allowing you to develop fully functional email client applications.

Compile and edit the following code to send mails

The first example shows how to send a basic email message via SMTP. Below, you'll find the SimpleSender class, which takes your message's details from the command line and calls a separate method -- send(...) -- to send it:

package com.lotontech.mail;

import javax.mail.*;

import javax.mail.internet.*;

import java.util.*;

/**

  • A simple email sender class.

*/

public class SimpleSender

{

/**

  • Main method to send a message given on the command line.

*/

public static void main(String args[])

{

try

{

String smtpServer=args[0];

String to=args[1];

String from=args[2];

String subject=args[3];

String body=args[4];

send(smtpServer, to, from, subject, body);

}

catch (Exception ex)

{

System.out.println("Usage: java com.lotontech.mail.SimpleSender"

+" smtpServer toAddress fromAddress subjectText bodyText");

}

System.exit(0);

}

Next, run SimpleSender as below. Replace smtp.myISP.net with your own SMTP server, as derived from your mail settings:

The send(...) method completes the SimpleSender class. I'll show the code first, then detail the theory:

/**

  • "send" method to send the message.

*/

public static void send(String smtpServer, String to, String from

, String subject, String body)

{

try

{

Properties props = System.getProperties();

// -- Attaching to default Session, or we could start a new one --

props.put("mail.smtp.host", smtpServer);

Session session = Session.getDefaultInstance(props, null);

// -- Create a new message --

Message msg = new MimeMessage(session);

// -- Set the FROM and TO fields --

msg.setFrom(new InternetAddress(from));

msg.setRecipients(Message.RecipientType.TO,

InternetAddress.parse(to, false));

// -- We could include CC recipients too --

// if (cc != null)

// msg.setRecipients(Message.RecipientType.CC

// ,InternetAddress.parse(cc, false));

// -- Set the subject and body text --

msg.setSubject(subject);

msg.setText(body);

// -- Set some other header information --

msg.setHeader("X-Mailer", "LOTONtechEmail");

msg.setSentDate(new Date());

// -- Send the message --

Transport.send(msg);

System.out.println("Message sent OK.");

}

catch (Exception ex)

{

ex.printStackTrace();

}

}

}

Former Member
0 Kudos

Hi jenish,

Thanks for your help.Especially the explanations helped me a lot.:-).But i have 1 question.Can i just use

that package?Or should I hav to do anything in the Netwearver Developer Studio to access that package?

With regads,

Jaisamma

Former Member
0 Kudos

Hi Jaisamma,

If you use Java 2 Platform, Enterprise Edition (J2EE) 1.3, it includes JavaMail, so no additional setup is required. If, however, you're running Java 2 Platform, Standard Edition (J2SE) 1.1.7 and upwards, download and install the following:

JavaMail->

http://java.sun.com/products/javamail/index.html

JavaBeans Activation Framework-> http://java.sun.com/products/javabeans/glasgow/jaf.html

To install, simply unzip the downloaded files and add the contained jar files to your classpath.

Regards,

Jenish V Joy

Former Member
0 Kudos

Hi,

take a look at these links:

<a href="http://www.javapractices.com/Topic144.cjp">http://www.javapractices.com/Topic144.cjp</a>

<a href="http://developer.apple.com/internet/java/examples/mailjava.html">http://developer.apple.com/internet/java/examples/mailjava.html</a>

<a href="http://www.javacommerce.com/articles/sendingmail.htm">http://www.javacommerce.com/articles/sendingmail.htm</a>

regards,

Dion

Former Member
0 Kudos

Hi Dion and Uma,

Thanks for your help:-).The sites helped me a lot.

With regards,

Jaisamma