在Android中使用SMTP发送电子邮件失败?

时间:2014-01-24 19:20:07

标签: java android smtp

我是Android的新手,我已经按照本教程 SMTP link another SMTP tutorial

但遗憾的是,我无法从模拟器/设备S3发送电子邮件,每次尝试我都会收到“CATCH”阻止消息。 这是从Activity

扩展的类
ImageView submitBtn = (ImageView)findViewById(R.id.askscreen_submit_btn);
        submitBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            try {
// CALL GetText method to make post method call
                 m = new Mail("abc@gmail.com","password"); 
                sendEmail(v);

            } catch (Exception ex) {
                    content.setText(" url exeption! ");
                }   
            }
        });
    }
    public void sendEmail(View view){
        String[] toArr = {"abc@gmail.com"}; // This is an array, you can add more emails, just separate them with a coma
        m.setTo(toArr); // load array to setTo function
        m.setFrom("abc@gmail.com"); // who is sending the email 
        m.setSubject("subject"); 
        m.setBody("your message goes here"); 

        try { 
            //m.addAttachment("/sdcard/myPicture.jpg");  // path to file you want to attach
            if(m.send()) { 
                // success
                Toast.makeText(AskfatwaActivity.this, "Email was sent successfully.", Toast.LENGTH_LONG).show(); 
            } else { 
                // failure
                Toast.makeText(AskfatwaActivity.this, "Email was not sent.", Toast.LENGTH_LONG).show(); 
            } 
        } catch(Exception e) { 
            // some other problem
            Toast.makeText(AskfatwaActivity.this, "There was a problem sending the email.", Toast.LENGTH_LONG).show(); 
        } 

    }

有人可以请帮帮我是什么原因?

1 个答案:

答案 0 :(得分:0)

我曾经实现过这个代码,它的级别相当低但它完成了这项工作,说实话从来没有使用过api所以...我能提供的所有帮助,还要注意base64代码的东西,用户名和密码必须是base64编码,所以你可能想要一些为你做这个的库

  import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import java.net.Socket;

import java.net.UnknownHostException;

import javax.swing.JOptionPane;

public class Email {




         public static void main(String[] args) throws UnknownHostException, IOException {


        Socket so = new Socket("smtp.mail.yahoo.com",25);    




BufferedReader inputStream = new BufferedReader(new InputStreamReader(so.getInputStream()));
PrintWriter outputStream   = new PrintWriter(new OutputStreamWriter(so.getOutputStream()),true);


String enemail   =  JOptionPane.showInputDialog(null, "Enter your e-mail in BASEA64 code");
String password  =  JOptionPane.showInputDialog(null, "Enter your password in BASEA64 code");
String from      =  JOptionPane.showInputDialog(null, "From");
String to        =  JOptionPane.showInputDialog(null, "To");
String subject   =  JOptionPane.showInputDialog(null, "Subject");
String msg       =  JOptionPane.showInputDialog(null, "Message");

outputStream.println("HELO ");      
System.out.println(inputStream.readLine());



outputStream.println("AUTH LOGIN");     
System.out.println(inputStream.readLine());




outputStream.println(enemail);      
System.out.println(inputStream.readLine());



outputStream.println(password);     
System.out.println(inputStream.readLine());


outputStream.println("VRFY");       
System.out.println(inputStream.readLine());

outputStream.println("MAIL FROM:<"+from+">");       
System.out.println(inputStream.readLine());


outputStream.println("RCPT TO:<"+to+">");       
System.out.println(inputStream.readLine());

outputStream.println("DATA");       
System.out.println(inputStream.readLine());


outputStream.println("Subject:"+subject);
outputStream.println("FROM :<"+from+">");
outputStream.println("TO :<"+to+">");
outputStream.println(msg);
outputStream.println("");       
outputStream.println(".");      
so.close();




         }
         }