Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

smtp authentication - security exception  RSS feed

DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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(
at MailServer.sendSupportForm(
at SupportForm.doPost(

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("", "");
props.put("mail.smtp.auth", "true");
props.put("mail.transport.protocol", "smtp");
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSentDate(new java.util.Date());
sent = true;
catch(MessagingException mex){
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("", "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!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!