Android中的自定义域邮件

时间:2014-04-24 10:04:36

标签: android email

你好朋友,我想在android中配置邮件设置.. 当我在互联网上搜索时, 我只能通过g-mail发送, 我如何在自定义域中发送邮件 e,g:someone@example.com 如果可以,请尽快回复

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    private EditText recipient,subject,body;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recipient = (EditText) findViewById(R.id.receiptent);
        subject = (EditText) findViewById(R.id.subject);
        body = (EditText) findViewById(R.id.body); 

        Button sendBtn = (Button) findViewById(R.id.sendEmail);
        sendBtn.setOnClickListener(new View.OnClickListener() {
           public void onClick(View view) {
             sendEmail();
             recipient.setText("");
             subject.setText("");
             body.setText("");
           }
     });

    }

    protected void sendEmail() {

        String[] recipients = {recipient.getText().toString()};
        Intent email = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
        email.setType("message/rfc822");

        email.putExtra(Intent.EXTRA_EMAIL, recipients);
        email.putExtra(Intent.EXTRA_SUBJECT, subject.getText().toString());
        email.putExtra(Intent.EXTRA_TEXT, body.getText().toString());

        try {
           startActivity(Intent.createChooser(email, "Choose an email client from..."));

        } catch (android.content.ActivityNotFoundException ex) {
           Toast.makeText(MainActivity.this, "No email client installed.",
                 Toast.LENGTH_LONG).show();
        }
     }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

2 个答案:

答案 0 :(得分:1)

使用Action_send意图,用户选择如何发送邮件。因此,如果您只在测试设备上配置了g-mail,则只能通过g-mail发送。

使用像someone@example.com这样的帐户设置具有action_send intent-filter的邮件应用程序会将此邮件应用程序添加到intent的选项中。

如果您想选择帐户并以编程方式发送邮件,那么我猜你必须使用像JavaMail这样的API。

答案 1 :(得分:1)

1.i想在android中配置邮件设置,例如:someone@example.com如果可以的话请尽快回复。

YES,this is possible in android.you can user any proxy server like abc@anyDomain.com.

//您可以使用以下代码从您的Android客户端发送电子邮件。

 import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;
import javax.mail.Authenticator;import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class EMailSender {
public EMailSender(String host, final String from, final String pass, String to, String      sub, String mess) throws Exception {
Properties props = System.getProperties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
Authenticator auth = new Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, pass);
}};
Session session = Session.getInstance(props, auth);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject(sub);
message.setText(mess);
Transport.send(message);
}

public static void main(String arg[]) throws Exception {
if(arg.length == 5) {
StringBuilder message = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String temp = "", subject;
System.out.print("Enter subject: ");
subject = br.readLine();
System.out.println("Enter the message (end it with a . on a single line):");
while((temp = br.readLine()) != null) {
if(temp.equals("."))
break;
message.append(temp+"\n");
}
System.out.println("Sending message...");
new EMailSender(arg[0], arg[1], arg[2], arg[3], subject, message.toString());
System.out.println("Sent the message.");
  }
 else System.err.println("Usage:\njava SendTextMail <host> <port> <from> <pass> <to>");
}
}

制作一个单独的类并将所有这些代码放在taht class.now你必须调用
通过传递必需参数来构造此类的构造函数。并且可以轻松地向任何服务器上的任何人发送邮件。