can';不要使用android代码发送邮件
我正在尝试在应用程序中发送邮件。我不会弹出任何与用户的交互。我有两个java代码can';不要使用android代码发送邮件,android,email,Android,Email,我正在尝试在应用程序中发送邮件。我不会弹出任何与用户的交互。我有两个java代码 MainActivity.java package com.example.test; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.MenuInflater; import android.view.View; import android.widget.T
MainActivity.java
package com.example.test;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
public static String EXTRA_MESSAGE = "com.example.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
public void sendMessage(View view)
{
try {
test sender = new test();
sender.send();
Toast.makeText(getApplicationContext(), "Message Sent!!! :)", Toast.LENGTH_LONG).show();
} catch (Exception e) {
//Log.e("SendMail", e.getMessage(), e);
}
}
public void exit(View view)
{
System.exit(0);
}
}
package com.example.test;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import android.widget.Toast;
public class test extends MainActivity
{
public void send()
{
String host = "smtp.gmail.com";
String from = "MYUSERNAME";
String pass = "MYPASS";
try
{
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true"); // added this line
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
String[] to = {"ADDRESSTOSEND@gmail.com"}; // added this line
Session session = Session.getDefaultInstance(props, null);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for( int i=0; i < to.length; i++ ) { // changed from a while loop
toAddress[i] = new InternetAddress(to[i]);
}
System.out.println(Message.RecipientType.TO);
for( int i=0; i < toAddress.length; i++) { // changed from a while loop
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject("This is subject");
message.setText("Welcome to your mail");
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), "Error" + e, Toast.LENGTH_LONG).show();
}
}
}
test.java
package com.example.test;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
public static String EXTRA_MESSAGE = "com.example.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
public void sendMessage(View view)
{
try {
test sender = new test();
sender.send();
Toast.makeText(getApplicationContext(), "Message Sent!!! :)", Toast.LENGTH_LONG).show();
} catch (Exception e) {
//Log.e("SendMail", e.getMessage(), e);
}
}
public void exit(View view)
{
System.exit(0);
}
}
package com.example.test;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import android.widget.Toast;
public class test extends MainActivity
{
public void send()
{
String host = "smtp.gmail.com";
String from = "MYUSERNAME";
String pass = "MYPASS";
try
{
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true"); // added this line
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
String[] to = {"ADDRESSTOSEND@gmail.com"}; // added this line
Session session = Session.getDefaultInstance(props, null);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for( int i=0; i < to.length; i++ ) { // changed from a while loop
toAddress[i] = new InternetAddress(to[i]);
}
System.out.println(Message.RecipientType.TO);
for( int i=0; i < toAddress.length; i++) { // changed from a while loop
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject("This is subject");
message.setText("Welcome to your mail");
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), "Error" + e, Toast.LENGTH_LONG).show();
}
}
}
package com.example.test;
导入java.util.*;
导入javax.mail.*;
导入javax.mail.internet.*;
导入android.widget.Toast;
公共类测试扩展了main活动
{
公共无效发送()
{
String host=“smtp.gmail.com”;
字符串from=“MYUSERNAME”;
字符串pass=“MYPASS”;
尝试
{
Properties props=新属性();
props.put(“mail.smtp.starttls.enable”,“true”);//添加了此行
props.put(“mail.smtp.host”,host);
props.put(“mail.smtp.user”,from);
props.put(“mail.smtp.password”,pass);
props.put(“mail.smtp.port”,“587”);
props.put(“mail.smtp.auth”,“true”);
字符串[]到={”ADDRESSTOSEND@gmail.com“};//添加了这一行
Session Session=Session.getDefaultInstance(props,null);
Message Message=新的mimessage(会话);
message.setFrom(新的InternetAddress(from));
InternetAddress[]toAddress=新的InternetAddress[to.length];
//获取地址数组的步骤
对于(int i=0;i
我按“发送”按钮时无法发送邮件。我已经使用xml onClick调用了sendMessage()
eclipse中没有错误。项目编译、安装,但我没有收到邮件。当我使用javac
尝试使用java并运行test.java时,效果很好。我的帐户里有邮件
谢谢你的帮助。你有什么建议吗
<uses-permission android:name="android.permission.INTERNET" />
到
就像这样使用代码
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[] { "abc@gmail.com" });
i.putExtra(Intent.EXTRA_SUBJECT, headline);
i.putExtra(Intent.EXTRA_TEXT, source);
try {
activity.startActivity(Intent.createChooser(i,"Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
}
我还没有试过你的发送代码,但是你获取上下文的方式会引发一个空指针 在
Toast.makeText(getApplicationContext(),“Error”+e,Toast.LENGTH_LONG).show()中的getApplicationContext()
中可能会引发NullPointerException代码>行。您不应该扩展活动以获取上下文。改为使用构造函数将上下文传递给测试类
例如:
public class Test {
private Context cntxt;
public Test(Context context){
this.cntxt = context;
}
public void send() {
//Code to send
//And when you need the context you use the cntxt
Toast.makeText(cntxt, "Error" + e, Toast.LENGTH_LONG).show()
}
}
还要记住,声明以大写字母开头的类名是一种很好的做法和惯例。您应该将test重命名为test。您确定正在调用sendmages()
?您是否尝试过以编程方式设置onclick侦听器?或者可能在sender.send()
上设置断点以查看实际发生的情况。调用try-inMainActivity.java
no时,我会得到java.lang.NullPointerException。正如我提到的,我需要直接发送,而不需要弹出与用户的任何交互