jimbo692007
03-30-2007, 08:06 AM
hi,
i'm trying to send an email thru smtp.gmail.com but i keep getting "javax.mail.AuthenticationFailedException" error as i send it. i can connect to the host grand but it wont authenticate.
any help would be very much appreciated!
this is the code i'm using:
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class SendEmail {
private Log log = LogFactory.getLog("SendEmail.java");
public void sendMail()
{
log.info("IN SEND EMAIL");
Properties myProperties=new Properties();
myProperties.put("mail.smtp.host", "smtp.gmail.com");
myProperties.put("mail.smtp.port", "587");
myProperties.put("mail.smtp.auth", "true");
myProperties.put("mail.debug", "true");
myProperties.put("mail.smtp.starttls.enable", "true");
log.info("MYPORPERTIES DONE");
Session ses = Session.getDefaultInstance(myProperties,null);
Message msg=new MimeMessage(ses);
try {
InternetAddress fromAddress = new InternetAddress("noreply@example.com","example");
InternetAddress toAddress = new InternetAddress("example@gmail.com");
msg.setFrom(fromAddress);
msg.setRecipient(Message.RecipientType.TO,toAddress);
msg.setSubject("TEST");
String body = "Test Mail";
msg.setContent(body, "text/plain");
msg.setSentDate(new Date());
Transport trans = ses.getTransport("smtp");
trans.connect("smtp.gmail.com","example@gmail.com","password");
msg.saveChanges();
trans.send(msg);
trans.close();
}
catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
i'm trying to send an email thru smtp.gmail.com but i keep getting "javax.mail.AuthenticationFailedException" error as i send it. i can connect to the host grand but it wont authenticate.
any help would be very much appreciated!
this is the code i'm using:
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class SendEmail {
private Log log = LogFactory.getLog("SendEmail.java");
public void sendMail()
{
log.info("IN SEND EMAIL");
Properties myProperties=new Properties();
myProperties.put("mail.smtp.host", "smtp.gmail.com");
myProperties.put("mail.smtp.port", "587");
myProperties.put("mail.smtp.auth", "true");
myProperties.put("mail.debug", "true");
myProperties.put("mail.smtp.starttls.enable", "true");
log.info("MYPORPERTIES DONE");
Session ses = Session.getDefaultInstance(myProperties,null);
Message msg=new MimeMessage(ses);
try {
InternetAddress fromAddress = new InternetAddress("noreply@example.com","example");
InternetAddress toAddress = new InternetAddress("example@gmail.com");
msg.setFrom(fromAddress);
msg.setRecipient(Message.RecipientType.TO,toAddress);
msg.setSubject("TEST");
String body = "Test Mail";
msg.setContent(body, "text/plain");
msg.setSentDate(new Date());
Transport trans = ses.getTransport("smtp");
trans.connect("smtp.gmail.com","example@gmail.com","password");
msg.saveChanges();
trans.send(msg);
trans.close();
}
catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}