I tried the method posted above, it was ok on my local machine but I couldnt get it to work reliably on my server so I used JavaxMail instead:
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendApp
{
public static void send(String from, String to, String CC, String subject, String content)
throws AddressException, MessagingException
{
String smtpHost = "127.0.0.1";
int smtpPort = 25;
// Create a mail session
java.util.Properties props = new java.util.Properties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", ""+smtpPort);
props.put("mail.smtp.auth", "true");
//If authentication is required on server you will need
//the following line and an Authenticator class for it
//to call
Authenticator auth = new SimpleAuthenticator();
Session session = Session.getDefaultInstance(props, auth);
// Construct the message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setRecipient(Message.RecipientType.CC, new InternetAddress(CC));
msg.setRecipient(Message.RecipientType.BCC, new InternetAddress(CC));
msg.setSubject(subject);
msg.setText(content);
// Send the message
Transport.send(msg);
}
}
|