• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

smtp authentication - security exception

 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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(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!
 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic