aspose file tools*
The moose likes Other JSE/JEE APIs 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 » Other JSE/JEE APIs
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");
}
}
Tomas Anderson
Greenhorn

Joined: Feb 26, 2006
Posts: 16
Try adding



where you set your properties.

Cheers...
TBA
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: javaMail with google..