Hi Guys,
Here is the Email Client for other accounts. Using this you can fetch and send emails.
Note: Keep in mind that settings are very important in this case.
The class to fetch email.
MailServerSettings.java
public class MailServerSettings {
private static Session mSession;
private Context mContext;
private String mSmtpServer, mSmtpPort, mUser, mPwd, mServer, mSmtpServe, mPort, mSecurity;
public MailServerSettings(Context context, String emailid, String pwd, String server, String port, String security, String accType, String smtpServer, String smtpPort, String smtpSecurity) throws Exception{
try{
mContext = context;
mUser = emailid;
mPwd = pwd;
mServer = server;
mSmtpServe = smtpServer;
mPort = smtpPort;
mSecurity = smtpSecurity;
Properties props = new Properties();
Store store = null;
if(accType.equalsIgnoreCase("IMAP")){
props = setIMAPProperties(port, security);
if(security.trim().equalsIgnoreCase("none")){
mSession = Session.getDefaultInstance(props);
}else{
try{
mSession = Session.getDefaultInstance(props,new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(mUser,mPwd);
}
});
}catch(Exception e){
mSession = Session.getInstance(props,new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(mUser,mPwd);
}
});
}
}
store = mSession.getStore("imap");
}else{
props = setPOP3Properties(port, security);
if(security.trim().equalsIgnoreCase("none")){
mSession = Session.getDefaultInstance(props);
}else{
try{
mSession = Session.getDefaultInstance(props,new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(mUser,mPwd);
}
});
}catch(Exception e){
mSession = Session.getInstance(props,new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(mUser,mPwd);
}
});
}
}
store = mSession.getStore("pop3");
}
store.connect(mServer, mUser, mPwd);
fetchMail(store, emailid, pwd, security, accType, index);
store.close();
}catch(Exception e){
e.printStackTrace();
throw e;
}
}
//To verify outgoing server settings...
public MailServerSettings(Context context, String emailid, String pwd, String server, String port, String security) throws Exception{
mContext = context;
mUser = emailid;
mPwd = pwd;
mServer = server;
Properties props = new Properties();
props = setSMTPProperties(emailid, pwd, server, port, security);
if(security.trim().equalsIgnoreCase("none")){
mSession = Session.getInstance(props);
}
else
{
try{
mSession = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(mUser,mPwd);
}
});
}catch(Exception e){
mSession = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(mUser,mPwd);
}
});
}
}
Transport trnsport = mSession.getTransport("smtp");
trnsport.connect(server, emailid, pwd);
trnsport.close();
}
private Properties setIMAPProperties(String port, String security){
Properties properties = new Properties();
properties.setProperty("mail.imap.port", port);
if(security!= null){
if(security.trim().equalsIgnoreCase("ssl")){
properties.setProperty("mail.imap.socketFactory.port", port);
properties.setProperty("mail.imap.ssl.enable", "true");
properties.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
}else if(security.trim().equalsIgnoreCase("ssl (accept all certificates)")){
properties.setProperty("mail.imap.socketFactory.port", port);
properties.setProperty("mail.imap.ssl.enable", "true");
properties.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
}
}
return properties;
}
private Properties setPOP3Properties(String port, String security){
Properties props = new Properties();
props.setProperty("mail.pop3.port", port);
props.setProperty("mail.pop3.ssl", "false");
if(security!= null){
if(security.trim().equalsIgnoreCase("ssl")){
props.setProperty("mail.pop3.starttls.enable", "true");//For yahoo
props.setProperty("mail.pop3.ssl", "true");
props.setProperty("mail.pop3.ssl.enable", "true");
props.setProperty("mail.pop3.socketFactory.port", port);
props.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
}else if(security.trim().equalsIgnoreCase("ssl (accept all certificates)")){
props.setProperty("mail.pop3.starttls.enable", "true");//For yahoo
props.setProperty("mail.pop3.socketFactory.port", port);
props.setProperty("mail.pop3.ssl", "true");
props.setProperty("mail.pop3.ssl.enable", "true");
props.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
}
}
return props;
}
private Properties setSMTPProperties(String emailid, String pwd, String server,String port, String security){
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", server);
props.put("mail.smtp.port", port);
if(security.trim().equalsIgnoreCase("none")){
props.put("mail.smtp.auth", "false");
}else{
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", port);
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
}
props.put("mail.smtp.user", emailid);
props.put("mail.smtp.password", pwd);
//For Hotmail set auth...
if(security.trim().equalsIgnoreCase("none")){
props.put("mail.smtp.auth", "false");
}else{
props.put("mail.smtp.auth", "true");
}
return props;
}
private void fetchMail(Store store, String user, String pwd, String security, String type){
try{
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message[] message = folder.getMessages();
int n = message.length;
Address[] from = message[n-1].getFrom(); //Fetch the latest email ....
String sub = message[n-i].getSubject();
String recipients = from[0].toString();
int start = recipients.trim().indexOf("<");
int end = recipients.trim().lastIndexOf(">");
if(end == -1)
end = recipients.trim().length();
String add = recipients.substring(start+1, end);
String text = "Message to send.";
SendMail m = new SendMail(mSmtpServer, mSmtpPort, mSmtpPort);
m.setCredential(mUser, mPwd);
String[] toArr = { add };
m.setEmail(toArr, mUser, "subject",text);
try {
m.send();
} catch (AddressException e) {
java.lang.System.out.println("Email sent failed. Error: " + e.getMessage());
} catch (MessagingException e) {
java.lang.System.out.println("Email sent failed. Error: " + e.getMessage());
}
folder.close(false);
store.close();
}catch(Exception e){ }
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(mUser, mPwd);
}
}
The class to send email.
SendMail.java
public class SendMail extends javax.mail.Authenticator {
private String _user;
private String _pass;
private String[] _to;
private String _from;
private String _port;
private String _sport;
private String _host;
private String _subject;
private String _body;
private boolean _auth;
private Multipart _multipart;
private SendMail() {
_auth = true; // smtp authentication - default on
_multipart = new MimeMultipart();
}
public SendMail(String smtpserver, String smtpport, String smtpsocketfactoryport) {
_host = smtpserver; // default smtp server
_port = smtpport; // default smtp port
_sport = smtpsocketfactoryport; // default socketfactory port
_auth = true; // smtp authentication - default on
_multipart = new MimeMultipart();
MailcapCommandMap mc = (MailcapCommandMap) CommandMap
.getDefaultCommandMap();
mc
.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc
.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc
.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc
.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc
.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
}
public void setCredential(String username, String password) {
_user = username;
_pass = password;
}
public void send() throws AddressException,MessagingException
{
Properties props =_setProperties();
Session session = Session.getInstance(props, this);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(_from));
InternetAddress[] addressTo = new InternetAddress[_to.length];
for (int i = 0; i < _to.length; i++) {
addressTo[i] = new InternetAddress(_to[i]);
}
msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
msg.setSubject(_subject);
msg.setSentDate(new Date());
// setup message body
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(_body);
_multipart.addBodyPart(messageBodyPart);
// Put parts in message
msg.setContent(_multipart);
// send email
Transport.send(msg);
}
public void setEmail(String[] to, String from, String subject, String body) {
_to = to;
_body = body;
_subject = subject;
_from = from;
}
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(_user, _pass);
}
private Properties _setProperties() {
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true"); //for Hotmail...
props.put("mail.smtp.host", _host);
props.put("mail.smtp.ssl.enable", true);
props.put("mail.debug", "true");
if (_auth) {
props.put("mail.smtp.auth", "true");
}
props.put("mail.smtp.port", _port);
props.put("mail.smtp.socketFactory.port",_sport);
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback","false");
return props;
}
}
Note: You will need to add these jar files in your app:
can you please attached download file.
ReplyDeleteProperties pop3Props = new Properties();
ReplyDelete// pop3Props.setProperty("mail.pop3s.port", "995");
Session session = Session.getDefaultInstance(pop3Props, null);
Store store = session.getStore("pop3s");
Log.i("before connect", str1);
store.connect(pop3Host, user, password);
Log.i("after connect", str1);
Folder emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
Message[] messages = emailFolder.getMessages();
for (int i = 0; i < messages.length; i++) {
Message message = messages[i];
Log.i("Text", str1);
}
emailFolder.close(false);
store.close();
} catch (NoSuchProviderException e) {
// Log.i("before connect", e.printStackTrace());
e.printStackTrace();
Log.e("YOUR_APP_LOG_TAG", "I got an error", e);
}
catch (MessagingException e) {e.printStackTrace();
Log.e("YOUR_APP_LOG_TAG", "I got an error", e);
I have session connection problem, or please inform me , how do solve that problem
}