Tuesday, August 12, 2014

How to send mail on Android programatically (mail API)

There are two ways to do it, using intents or using a mail API.  The main difference is that when implementing an intent, you are basically leveraging a mail app that is already installed in your phone and therefore, this method is much simpler. Here I will explain how implement the mail API.


How to do it:

1. Download the java mail library: https://code.google.com/p/javamail-android/downloads/list
2. Add the corresponding permissions in the AndroidManifest.xml file:
 
   
3.Create a class MailHelper and copy the following code:
import java.io.File;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;


public class MailHelper {

    private String originEmail;
    private String originPassword;

    private String destinationEmail;

    private String auth;
    private String sslEnable;
    private String smtpHost;
    private String smtpPort;

    public Mail(String originEmail,String originPassword,String destinationEmail){
        this.originEmail=originEmail;
        this.originPassword=originPassword;
        this.destinationEmail=destinationEmail;

        this.auth="true";
        this.sslEnable="true";
        this.smtpHost="smtp.gmail.com";
        this.smtpPort="587";
    }

    public boolean send(String attachmentFilePath){
        boolean noError=true;
        final String username=originEmail;
        final String password=originPassword;

        Properties props = new Properties();
        props.put("mail.smtp.auth", auth);
        props.put("mail.smtp.starttls.enable", sslEnable);
        props.put("mail.smtp.host", smtpHost);
        props.put("mail.smtp.port", smtpPort);

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        try {

            BodyPart text=new MimeBodyPart();
            text.setText("Text");

            BodyPart bodyAttachment=new MimeBodyPart();
            bodyAttachment.setDataHandler(new DataHandler(new FileDataSource(attachmentFilePath)));
            bodyAttachment.setFileName("Attachment");

            MimeMultipart mm=new MimeMultipart();
            mm.addBodyPart(text);
            mm.addBodyPart(bodyAttachment);

            MimeMessage message=new MimeMessage(session);
            message.setFrom(new InternetAddress(originEmail));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(destinationEmail));
            message.setSubject("Subject");
            message.setContent(mm);

            Transport.send(message);
        } catch (MessagingException e) {
            noError=false;
            throw new RuntimeException(e);

        }
        return noError;
    }
}

4.Place the following code in a separate thread, because this a time consuming task that can potentially freeze your UI. You can usean AsynTask for this purpose, for example.
import java.io.File;

MailHelper mail=new MailHelper("origin@gmail.com","originPassword"
,"destination@gmail.com");
Boolean noError=m.send(fileAbsolutePath);










Friday, August 8, 2014

How to write code snippets on Blogger

blah blah blah: Ok, so as my plan is to write some code samples in the future, the first thing I need to deal with is how to do it on this blog engine


how to do it:
1. Go to your blog dashboard

2. Go to Template
 3. Click on edit HTML
4. Paste the following code

<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/> 
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCpp.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPhp.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushRuby.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushVb.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPerl.js' type='text/javascript'></script> 
<script language='javascript'> 
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/current/scripts/clipboard.swf';
SyntaxHighlighter.all();
</script>
5. Now, the style is recorded in your template. When you want to paste some code, you have to use the following tags:
<pre class="brush: csharp">
my code
</pre>
6. Done

Note:
If your code contains character such as < , >  or others, this won't work. You need to escape your code.
A great tool is this:
http://www.opinionatedgeek.com/dotnet/tools/htmlencode/encode.aspx

My new project

I am a software engineer whose native language is not English. The purpose of this blog will be for me to practice more written English and at the same time, have a record of all the things I learn on my everyday job and life. This will not only benefit me, but other people. That is why I thought it would be great to share.