Sending mail from your Java application using google SMTP
January 27, 2010 2 Comments
Previous few I was trying to send mail from my JSP page. I had tried every way to make that code work. Later I found that I have to set Mail Authentication, which lead me to solution.
Here is my SendMail cass, which you can implement in any Java application to send mail.
SendMail.java
/** * * @author Prashant */ import java.security.Security; import java.util.Properties; import javax.mail.Message; import javax.mail.NoSuchProviderException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.PasswordAuthentication; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class SendMail { private String to; private String subject; private String text; public SendMail(String to, String subject, String text){ this.to = to; this.subject = subject; this.text = text; } public void send() throws NoSuchProviderException, AddressException{ try { Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); Properties props=new Properties(); props.setProperty("mail.transport.protocol","smtp"); props.setProperty("mail.host","smtp.gmail.com"); props.put("mail.smtp.auth","true"); props.put("mail.smtp.port","465"); props.put("mail.debug","true"); props.put("mail.smtp.socketFactory.port","465"); props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.socketFactory.fallback","false"); Session session=Session.getDefaultInstance(props,new GJMailAuthenticator()); session.setDebug(true); Transport transport=session.getTransport(); InternetAddress addressFrom=new InternetAddress("sender@gmail.com"); MimeMessage message=new MimeMessage(session); message.setSender(addressFrom); message.setSubject(subject); message.setContent(text,"text/html"); InternetAddress addressTo=new InternetAddress(to); message.setRecipient(Message.RecipientType.TO,addressTo); transport.connect(); Transport.send(message); transport.close(); System.out.println("DONE"); } catch(Exception e) { e.printStackTrace(); } } } class GJMailAuthenticator extends javax.mail.Authenticator{ protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("sender@gmail.com","sender_password"); } }
Mail.java
/** * * @author Prashant */ public class Mail { public static void main(String[] args) { String to = "yohoprashant@yahoo.com"; String subject = "Test"; String message = "A test message"; SendMail sendMail = new SendMail(to, subject, message); try { sendMail.send(); } catch (Exception e) { // } } }
Note: If you want to use yahoo server instead of google, just you have to change the Port address to 25.
-
import javax.mail.*;
-
import javax.mail.internet.*;
-
import java.util.*;
-
public class Mail
-
{
-
d_password = “****”,
-
d_host = “smtp.gmail.com”,
-
d_port = “465”,
-
m_to = “iamdvr@yahoo.com”,
-
m_subject = “Testing”,
-
m_text = “Hey, this is the testing email using smtp.gmail.com.”;
-
{
-
//This is for google
-
Mail.sendMail(“venkatesh@dfdf.com”,“password”,“smtp.gmail.com”,“465”,“true”,
-
“true”,true,“javax.net.ssl.SSLSocketFactory”,“false”,to,cc,bcc,
-
“hi baba don’t send virus mails..”,“This is my style…of reply..
-
If u send virus mails..”);
-
}
-
//Properties props=System.getProperties();
-
props.put(“mail.smtp.user”, userName);
-
props.put(“mail.smtp.host”, host);
-
if(!“”.equals(port))
-
props.put(“mail.smtp.port”, port);
-
if(!“”.equals(starttls))
-
props.put(“mail.smtp.starttls.enable”,starttls);
-
props.put(“mail.smtp.auth”, auth);
-
if(debug){
-
props.put(“mail.smtp.debug”, “true”);
-
}else{
-
props.put(“mail.smtp.debug”, “false”);
-
}
-
if(!“”.equals(port))
-
props.put(“mail.smtp.socketFactory.port”, port);
-
if(!“”.equals(socketFactoryClass))
-
props.put(“mail.smtp.socketFactory.class”,socketFactoryClass);
-
if(!“”.equals(fallback))
-
props.put(“mail.smtp.socketFactory.fallback”, fallback);
-
try
-
{
-
Session session = Session.getDefaultInstance(props, null);
-
session.setDebug(debug);
-
MimeMessage msg = new MimeMessage(session);
-
msg.setText(text);
-
msg.setSubject(subject);
-
msg.setFrom(new InternetAddress(“p_sambasivarao@sutyam.com”));
-
for(int i=0;i<to.length;i++){
-
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
-
}
-
for(int i=0;i<cc.length;i++){
-
msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i]));
-
}
-
for(int i=0;i<bcc.length;i++){
-
msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[i]));
-
}
-
msg.saveChanges();
-
Transport transport = session.getTransport(“smtp”);
-
transport.connect(host, userName, passWord);
-
transport.sendMessage(msg, msg.getAllRecipients());
-
transport.close();
-
return true;
-
}
-
{
-
mex.printStackTrace();
-
return false;
-
}
-
}
-
}
Excellent, it works perfectly. I will modify it to attach binary files to messages, it is an excellent starting point. Thank you, Prashant.
i nned some help please i got this error “The method setSender(InternetAddress) is undefined for the type MimeMessage” but setSender is defined in MimeMessage i don’t know where is the problem