Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a mailing List

Status
Not open for further replies.

daph

Programmer
Jun 9, 2000
65
CA
Hi,

I have to create a mailing list and I have no idea where to start since I've never done this before. I've been trying to figure out the most efficient and relyable way of doing this. I have no idea how many people will be in that mailing list - could be 10 or 1000. This list will also be very dynamic (I can give a better explaination if needed). The messages sent will be automated.

We were thinking of using Sendmail. We are on Unix and JSP. Could anyone give me a hint to where to begin, where I could go for good references? Do I need some program to help me do this? Or would it be easy enough to just write a script? If I can only get a few good ideas and know where to do some research on this, I'd know where to start. Examples would help too. I'll take anything right now! :)

Thanks a lot guys,

Daph
 
The below java code will send mail (JavaMail - you need the mail.jar file from sun) ...

<code>
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
import javax.activation.*;

public class SendMail {
static String mxServer;
String toAddress, fromAddress;
String subject, text;
MimeBodyPart mbp = null;


/**
* Construct an email. <BR>
* If the mxServer is unknown, then the static method SendMail.nslookup() can be called to retrieve the domain's mx server. <BR>
* Attachments are optional. <BR>
* @param String mxServer - The Mail eXchange server to send the mail to.
* @param String toAddress - The recipient.
* @param String fromAddress - The sender - can be anyting as long as looks like an email address - eg 'me@somewhere.com'.
* @param String subject - The mail's subject.
* @param String text - The body of the mail.
*/
public SendMail(String mxServer, String toAddress, String fromAddress, String subject, String text) {
this.mxServer = mxServer;
this.toAddress = toAddress;
this.fromAddress = fromAddress;
this.subject = subject;
this.text = text;

}

/**
* Add an attachment to the mail (from a file on disk).
* @param File file - the File object to attach.
*/
public void attach(File file) {
try {
mbp = new MimeBodyPart();
mbp.setFileName(file.getName());
mbp.setDataHandler(new DataHandler(new FileDataSource(file)));
} catch (MessagingException me) {
me.printStackTrace();
}
}

/**
* Send a message using the contstructor properties.
* If there is also an attachment to send, add it too.
*/
public void send() {
Properties props = System.getProperties();
props.put(&quot;mail.smtp.host&quot;, mxServer);
Session session = Session.getDefaultInstance(props, null);

try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(fromAddress));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress, false));
msg.setSubject(subject);
msg.setText(text);
msg.setHeader(&quot;X-Mailer&quot;, &quot;JavaMail&quot;);

if (mbp != null) {
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp);
msg.setContent(mp);
}

Transport.send(msg);
} catch (AddressException ae) {
ae.printStackTrace();
} catch (MessagingException me) {
me.printStackTrace();
}
}

/**
* Given a domain name like 'hotmail.com', perform an OS nslookup call,
* and loop it, looking for the word 'exchanger' in the line. On Linux and Windoze
* the mx mail server is always the last word/token in the line, so set it as such.
* This pays no attention to the preference of which mx server to use, but could (and should !)
* be built in really. Still, never mind.
* @param String domain - the domain to lookup.
*/
public static String nslookup(String domain) {
String mailserver = null;
try {
Process p = Runtime.getRuntime().exec(&quot;nslookup -type=mx &quot; +domain);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

boolean gotMxLine = false;
String line = null;
String token = null;

while ((line = br.readLine()) != null) {
gotMxLine = false;
//System.out.println(line);
StringTokenizer st = new StringTokenizer(line);
while (st.hasMoreTokens()) {
token = st.nextToken();
if (token.equals(&quot;exchanger&quot;)) {
gotMxLine = true;
}
if (gotMxLine) {
mailserver = token;
}
}

}
} catch (IOException ioe) {
ioe.printStackTrace();
return null;
}

System.out.println(&quot;Mail Server to use is :: &quot; +mailserver);
return mailserver;
}

public static void main(String args[]) {
if (args.length < 5) {
System.out.println(&quot;Usuage :: <mxServer> <toAddress> <fromAddress> <subject> <text>&quot;);
System.exit(1);
}
String msgText = &quot;&quot;;
for (int i = 4; i < args.length; i++) {
msgText += (&quot; &quot; +args);
}

new SendMail(args[0], args[1], args[2], args[3], msgText).send();
}
}
</code>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top