× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Hi here is my code which works - I've built a RCP app that monitors iSeries
events and sends emails to a list of email addresses passed to a mailer
which splits out the email addresses as strings and then creates this object
calling the sendmail method of each object. All the from and to addresses
are just strings..

Granny and sucking eggs - here's the bit that creates the object and calls
the sendmail method.....there is also code in there to ping the smtp
server..I put all the println's in to see what's happening - I really am
originally a procedural coder (S36)...

if (smtpServerOnline()) {

System.out.println("SMTP Started");

String subject = "SysChecker Alert";

// Split addresses from list to String array

splitEmailList();

// The server is on-line - send emails

for (int i = 0; i < addresses.length; i++) {

// parse the destination addresses

System.out.println("Emailing " + addresses[i]);

Mailer mail = new Mailer(addresses[i], setMessage(),

fromAddress, mailServer, subject);

mail.sendEmail();

mail = null;

}

}


Here is the mailer object

import java.util.Properties;
import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.AddressException;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

public class Mailer {

private String fromAddress;

private String toAddress;

private String content;

private String mailHost;

private Properties props;

private Session session;

private String subject;

public Mailer(String toAddress, String content, String fromAddress,

String mailHost, String subject) {

this.fromAddress = fromAddress;

this.toAddress = toAddress;

this.content = content;

this.mailHost = mailHost;

this.subject = subject;

}

public void sendEmail() {

// Get system properties

props = System.getProperties();

// Specify the desired SMTP server

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

// create a new Session object

session = Session.getInstance(props, null);

// create a new MimeMessage object (using the Session created above)

Message message = new MimeMessage(session);

try {

message.setFrom(new InternetAddress(fromAddress));

} catch (AddressException e) {

e.printStackTrace();

} catch (MessagingException e) {

e.printStackTrace();

}

try {

message.setRecipients(Message.RecipientType.TO,

new InternetAddress[] { new InternetAddress(toAddress) });

} catch (AddressException e) {

e.printStackTrace();

} catch (MessagingException e) {

e.printStackTrace();

}

try {

message.setSubject(subject);

} catch (MessagingException e) {

e.printStackTrace();

}

try {

message.setContent(content, "text/plain");

} catch (MessagingException e) {

e.printStackTrace();

}

try {

Transport.send(message);

} catch (MessagingException e) {

e.printStackTrace();

}

}

}



Good luck

Kevin


As an Amazon Associate we earn from qualifying purchases.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.