This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "javaMail with google.." Watch "javaMail with google.." New topic
Author

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.io.IOException;
import java.security.Security;
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");

try
{
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Properties prop = new Properties();


prop.put("mail.transport.protocol", "smtp");
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put( "mail.smtp.auth ", "true");
prop.put("mail.debug", "true");
prop.put("mail.smtp.port", "465");
prop.put("mail.smtp.starttls.enable","true");
prop.put("mail.smtp.socketFactory.port", "465");
prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
prop.put("mail.smtp.socketFactory.fallback", "false");

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

InternetAddress addressFrom = new InternetAddress("redacted@gmail.com", "redacted@gmail.com");
msg.setFrom(addressFrom);
InternetAddress addressTo = new InternetAddress(to);
msg.setRecipient(Message.RecipientType.TO, addressTo);

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

response.sendRedirect("/pages/email.jsp");
}
catch (Exception e)
{
e.printStackTrace();
}

}
}
class ForcedAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("myUserName",
"myPassword");
}
}
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
"mail.smtp.password"
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="http://tjws.sf.net" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="http://mediachest.sf.net" target="_blank" rel="nofollow">Check it here.</a><br /><a href="http://7bee.j2ee.us/book/Generics%20in%20JDK%201.5.html" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="http://7bee.j2ee.us/bee/index-bee.html" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="http://7bee.j2ee.us/addressbook/" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="http://searchdir.sourceforge.net/" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: javaMail with google..
 
Similar Threads
Sending Email using Java mail API in J2EE
Getting exception in sending Email from Servlet
AuthenticatedFailedException is thrown javamail
urgent help in java mail
javaMail with google..