我正在尝试在应用程序中发送邮件。我不会弹出与用户的任何互动。我有两个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.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);
}
}
test.java
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();
}
}
}
按下发送按钮时无法发送邮件。我使用xml onClick调用了sendMessage()。
日食中没有错误。该项目编译,安装但我没有收到邮件。当我使用javac
尝试使用java并运行test.java时,它可以正常运行。我收到了帐户中的邮件。
感谢您的帮助。
答案 0 :(得分:1)
只需使用此代码
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) {
}
答案 1 :(得分:0)
你有吗
<uses-permission android:name="android.permission.INTERNET" />
在您的应用程序清单文件中?
编辑1:
如何改变
Session session = Session.getDefaultInstance(props, null);
到
Session session = Session.getDefaultInstance(props, new MailAuthenticator(from, pass));
答案 2 :(得分:0)
我没有尝试过您的发送代码,但是获取上下文的方式会引发空指针。
可能在getApplicationContext()
行的Toast.makeText(getApplicationContext(), "Error" + e, Toast.LENGTH_LONG).show();
中引发了NullPointerException。您不应该扩展活动以获取上下文。使用构造函数将上下文传递给Test类。
例如:
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。