File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Sockets and Internet Protocols and the fly likes javaMail with google.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "javaMail with google.." Watch "javaMail with google.." New topic

javaMail with google..

Rob Qas
Ranch Hand

Joined: Apr 10, 2006
Posts: 34
I am trying to send mail using JAVAMAIL API with GMAIL SMTP
using this code in a servlet..

and evrytime i got this error>>

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Authentication Required m1sm854399ugc

package com.mail;

import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class serv1 extends HttpServlet
public void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException

String to= (String)request.getParameter("to");
String subject= (String)request.getParameter("subject");
String msgContent = tring)request.getParameter("msgContent");

Properties prop = new Properties();

prop.put("mail.transport.protocol", "smtp");
prop.put("", "");
prop.put( "mail.smtp.auth ", "true");
prop.put("mail.debug", "true");
prop.put("mail.smtp.port", "465");
prop.put("mail.smtp.socketFactory.port", "465");
prop.put("mail.smtp.socketFactory.class", "");
prop.put("mail.smtp.socketFactory.fallback", "false");

Session session = Session.getDefaultInstance(prop,
new ForcedAuthenticator());
MimeMessage msg = new MimeMessage(session);

InternetAddress addressFrom = new InternetAddress("", "");
InternetAddress addressTo = new InternetAddress(to);
msg.setRecipient(Message.RecipientType.TO, addressTo);

msg.addHeader("HEADER", "Razi header ");
msg.setContent(msgContent, "text/html");

catch (Exception e)

class ForcedAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("myUserName",
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
I do not see that you setup in properties user name and password. I think these values will be
and "mail.smtp.user"
Just found them using MSN search. I use light e-mail sender in my jAddressBook project and it works quite well with google SMTP.

Tough in space?, <a href="" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="" target="_blank" rel="nofollow">Check it here.</a><br /><a href="" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
I agree. Here's the link:
subject: javaMail with google..
It's not a secret anymore!