Article View: borland.public.jbuilder.java.language
Article #7737Re: Email
From: Imran Hussain
Date: Mon, 21 May 2001 21:23
Date: Mon, 21 May 2001 21:23
117 lines
3963 bytes
3963 bytes
Hello Carsten,
Below is the code for a very simple EmailService class. It uses Java Mail API
from Sun.
import java.io.*;
import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;
public class EmailService {
public static void main(String[] argv) {
new EmailService();
}
/**
* Use this static function to send email messages. This is a very simple class
* that can only send text messages. It does NOT handle attachements.
*
* @param stmtHost SMTP Host name
* @param to Comma separated email address(es) for the recipient(s)
* @param subject Email Subject
* @param from Sender's email address
* @param cc Comma separated email address(es) for the CC recipient(s)
* @param bcc Comma separated email address(es) for the BCC recipient(s)
* @param body Body of the message.
*/
public static void sendEmail(String smtpHost, String to, String subject,
String from, String cc, String bcc, String body) {
String url = null;
String mailer = "Email Service"; //Change this name to suite your needs.
String record = null; // name of folder in which to record mail
boolean debug = false;
try{
Properties props = System.getProperties();
if (smtpHost != null)
props.put("mail.smtp.host", smtpHost);
// Get a Session object
Session session = Session.getDefaultInstance(props, null);
// construct the message
Message msg = new MimeMessage(session);
if (from != null)
msg.setFrom(new InternetAddress(from));
else
msg.setFrom();
if(to != null)
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));
if (cc != null)
msg.setRecipients(Message.RecipientType.CC,
InternetAddress.parse(cc, false));
if (bcc != null)
msg.setRecipients(Message.RecipientType.BCC,
InternetAddress.parse(bcc, false));
msg.setSubject(subject == null ? "<No Subject>" : subject);
msg.setText(body);
msg.setHeader("X-Mailer", mailer);
msg.setSentDate(new Date());
// send the thing off
Transport.send(msg);
System.out.println("\nMail was sent successfully.");
}catch(Exception e){
e.printStackTrace();
}
}
}
Carsten Ziegler wrote:
> Hi,
>
> I have a servlet that handles the data of an html form. Now I want to make
> an email out of the entries. I don't know how to send emails in java but I
> don't want to buy a whole book for that little task, too.
> Does anyone know a website with a demo for sending mails? Or can anyone send
> me an example?
>
> Regards,
> Carsten
Technical Details
Message-ID:
<3B09876B.70D6706C@NoSpam.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!news.tele.dk!193.190.198.17!newsfeeds.belnet.be!news.belnet.be!newsfeed.online.be!newsfeed1.uni2.dk!newssuck.uni2.dk!dnewsReferences:
<3b077bef_1@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,4646672f06eb3a97 X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-05-21 14:31:27 PST Message-ID: <3B09876B.70D6706C@NoSpam.com> Date: Mon, 21 May 2001 17:23:55 -0400 From: Imran Hussain <imranh@NoSpam.com> Reply-To: imranh@NoSpam.com X-Mailer: Mozilla 4.76 [en] (Windows NT 5.0; U) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: borland.public.jbuilder.java.language Subject: Re: Email References: <3b077bef_1@dnews> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 208.171.247.241 X-Trace: dnews 990480234 208.171.247.241 (21 May 2001 14:23:54 -0700) Lines: 118 Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!news.tele.dk!193.190.198.17!newsfeeds.belnet.be!news.belnet.be!newsfeed.online.be!newsfeed1.uni2.dk!newssuck.uni2.dk!dnews