Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to send email through servlet using java mail API

 
khushi paul
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
i am trying to send mail through servlet using java mail API.
i get following error

Sending failed; nested exception is: javax.mail.MessagingException: 530 authentication needed

wat shud i do??pls help,as i m new to servlets n mail API
thanks in advance
 
K Riaz
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you correctly used a valid username and password when connecting to a SMTP server? You could check by telneting to the server.
 
khushi paul
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks 4 ur reply kashif...i m using following code,can u pls tell me where shud i provide username n password..
moreover is it not possible to send mail if i don't know the user name or passwrd of server?is there any way possible?

String host ="my_host_name";
String from = "abc@somename";
String to ="xyz@samename";
String subject = "This is a trial msg!";
String message = "Message comes here!";
Properties prop =System.getProperties();
prop.put("mail.host", host);
Session ses1 = Session.getDefaultInstance(prop,null);
MimeMessage msg = new MimeMessage(ses1);
msg.setFrom(new InternetAddress(from))
msg.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
msg.setSubject(subject);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(message);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
Transport.send(msg);
 
Arjun Shastry
Ranch Hand
Posts: 1898
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can write a small class which extends javax.mail.Authenticator.Override the method getPasswordAuthentication().Pass the object of the small class:
Session ses1 = Session.getDefaultInstance(prop,new smallClass());
If you download Free SMTP/POP3 server,there is no need to pass Authentication.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic