在没有Intent的情况下在android中发送邮件

时间:2014-02-15 06:25:24

标签: java android eclipse

以下是Main class的代码。

public class Main extends Activity {

EditText to, from, message, subject, pass; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button send =  (Button) findViewById(R.id.buttonSend);
    to = (EditText) findViewById(R.id.editTextTo);
    from = (EditText) findViewById(R.id.editTextFrom);
    subject = (EditText) findViewById(R.id.editTextSubject);
    message = (EditText) findViewById(R.id.editTextMessage);
    pass = (EditText) findViewById(R.id.editPassFrom);

   final String toString = to.toString();
   final String fromString = from.toString();
   final String passString = pass.toString();
   final String subjString = subject.toString();
   final String msgString = message.toString();

    send.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {


            try {   
                GMailSender sender = new GMailSender(fromString, passString);
                sender.sendMail(subjString,   
                        msgString,   
                        fromString,   
                        toString);   
            } catch (Exception e) {   
                Log.e("SendMail", e.getMessage(), e);   
            } 
            to.setText("");
            from.setText("");
            subject.setText("");
            message.setText("");
            pass.setText("");
        }
    });

}
}

以下是GMailSender类的代码

public class GMailSender extends javax.mail.Authenticator {   
private String mailhost = "smtp.gmail.com";   
private String user;   
private String password;   
private Session session;   

static {   
    Security.addProvider(new com.provider.JSSEProvider());   
}  

public GMailSender(String user, String password) {   
    this.user = user;   
    this.password = password;   

    Properties props = new Properties();   
    props.setProperty("mail.transport.protocol", "smtp");   
    props.setProperty("mail.host", mailhost);   
    props.put("mail.smtp.auth", "true");   
    props.put("mail.smtp.port", "465");   
    props.put("mail.smtp.socketFactory.port", "465");   
    props.put("mail.smtp.socketFactory.class",   
            "javax.net.ssl.SSLSocketFactory");   
    props.put("mail.smtp.socketFactory.fallback", "false");   
    props.setProperty("mail.smtp.quitwait", "false");   

    session = Session.getDefaultInstance(props, this);   
}   

protected PasswordAuthentication getPasswordAuthentication() {   
    return new PasswordAuthentication(user, password);   
}   

public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {   
    try{
    MimeMessage message = new MimeMessage(session);   
    DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));   
    message.setSender(new InternetAddress(sender));   
    message.setSubject(subject);   
    message.setDataHandler(handler);   
    if (recipients.indexOf(',') > 0)   
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));   
    else  
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));   
    Transport.send(message);   
    }catch(Exception e){

    }
}   

public class ByteArrayDataSource implements DataSource {   
    private byte[] data;   
    private String type;   

    public ByteArrayDataSource(byte[] data, String type) {   
        super();   
        this.data = data;   
        this.type = type;   
    }   

    public ByteArrayDataSource(byte[] data) {   
        super();   
        this.data = data;   
    }   

    public void setType(String type) {   
        this.type = type;   
    }   

    public String getContentType() {   
        if (type == null)   
            return "application/octet-stream";   
        else  
            return type;   
    }   

    public InputStream getInputStream() throws IOException {   
        return new ByteArrayInputStream(data);   
    }   

    public String getName() {   
        return "ByteArrayDataSource";   
    }   

    public OutputStream getOutputStream() throws IOException {   
        throw new IOException("Not Supported");   
    }   
}   
}  

我参与了这段代码,但它没有用。

这是Logcat状态:

  

02-15 01:15:47.132:I / Choreographer(1196):跳过30帧!应用程序可能在其主线程上做了太多工作。

1 个答案:

答案 0 :(得分:1)

只需添加后台邮件库和设置邮件地址非常容易。

检查此库,它与您期望的相同,请阅读此库中的“自述文件”以了解相关步骤。 Background Mail Library

下载库,解压缩,它将提供库和演示项目,将此库导入Eclipse IDE或您正在使用android的任何IDE

以下是示例代码,

import com.kristijandraca.backgroundmaillibrary.BackgroundMail;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;


public class MainActivity extends Activity {

    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BackgroundMail bm = new BackgroundMail(MainActivity.this);
        bm.setGmailUserName("youremail@gmail.com");
        bm.setGmailPassword("yourpassword");
        bm.setMailTo("receivermail id");
        bm.setFormSubject("Backgroundmail");
        bm.setFormBody("TEsting");
        bm.setSendingMessage("Sending mail...");
        bm.setSendingMessageSuccess("Your message was sent successfully.");
        bm.setProcessVisibility(true);
        bm.send();

    }
}

右键单击项目资源管理器中的项目文件夹并选择属性goto android tab在“is Library”部分添加库,就是这样,添加Internet权限。