Hey folks, Ive been using JavaMail for years now but just moved to naother server that requires SMTP authentication to send email to any addy outside of the domain. I did some reading & found some code that I thought would do the trick but it keeps blowing this exception when I try to access the default session:
java.lang.SecurityException: Access to default session denied
at javax.mail.Session.getDefaultInstance(Session.java:175)
at MailServer.sendSupportForm(MailServer.java:34)
at SupportForm.doPost(SupportForm.java:18)
Here is the code Im using (usernames etc have been changed):
public class MailServer {
boolean sent;
public boolean sendSupportForm(
String domain, String emailAddy, String typeProb, String comments){
String to = "outside_email_addy";
String from = emailAddy;
String subject = "Hosting Support Form";
String messageText = "Domain Name: "+domain+"\n Email Address: "+emailAddy+"\n Type Problem: " +typeProb+"\n Comments: "+comments;
boolean sessionDebug = false;
java.util.Properties props = System.getProperties();
props.put("mail.smtp.host", "host.my_domain.com");
props.put("mail.smtp.auth", "true");
props.put("mail.transport.protocol", "smtp");
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(sessionDebug);
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new java.util.Date());
msg.setText(messageText);
Transport.send(msg);
sent = true;
}
catch(MessagingException mex){
mex.printStackTrace();
}
return sent;
}
};
class SMTPAuthenticator extends Authenticator {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("my_smtp_username", "my_smtp_password");
}
};
Im not really sure what Im doing wrong here. The code I found on the web also used a Store object (javax.mail.Store) but they were logging into yahoo's pop3 email so I didnt think I would need that .... here is that code, just in case:
// Next two lines are specific to Yahoo
Store store = s.getStore("pop3");
store.connect("pop.mail.yahoo.com", "my"_user_name, "my_pass_word");
Thanks in advance to anyone that can help .... Really need to be able to send email outside of the domain!