我有两个用于发送简单(文本)电子邮件的java类,另一个用于附件。第一拳工作正常,但第二个,即附件类的电子邮件无效。我的应用程序崩溃,我无法弄清楚发生了什么错误:
NewEmailAttachmentSender.java:
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class NewEmailAttachmentSender {
private static final String username = "123@gmail.com";
private static final String password = "12345";
public NewEmailAttachmentSender() { }
public void sendEmailWithAttachments(String email, String subject, String messageBody, String FilePath)
{
Session session = createSessionObject();
try {
final Message message = createMessage(email, subject, messageBody, FilePath, session);
Thread t = new Thread(){
@Override
public void run() {
try {
Transport.send(message);
} catch (MessagingException e) {
//TODO Handle Exception
}
}
};
t.start();
//new SendMailTask().execute(message);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
private Message createMessage(String email, String subject, String messageBody, String FilePath, Session session) throws MessagingException, UnsupportedEncodingException {
//Log.i("check","createingMessage");
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("123@gmail.com", "POST"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress("123@gmail.com"));
message.setSubject(subject);
message.setText(messageBody);
message.setSentDate(new Date());
Multipart multipart = new MimeMultipart("mixed");
// creates message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(FilePath);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(source.getName());
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
return message;
}
private Session createSessionObject() {
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
return Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication( username, password);
}
});
}
}
主要活动:
import com.post.gmailSender.GmailSender;
import com.post.gmailSender.NewEmailAttachmentSender;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends Activity {
@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.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void gmailSender(View view)
{
Log.d("tag","clicked");
GmailSender sender = new GmailSender();
try {
sender.sendMail("123@gmail.com", "Test Mail", "Post TestMail");
//sender.start();
} catch (Exception e) {
Log.e("SendMail", e.getMessage(), e);
}
}
public void gmailwithattachment(View view)
{
Log.d("tag","clicked");
NewEmailAttachmentSender sender = new NewEmailAttachmentSender();
try {
sender.sendEmailWithAttachments("123@gmail.com", "Test Mail", "Post TestMail","/sdcard/logo.png");
} catch (Exception e) {
Log.e("SendMail", e.getMessage(), e);
}
}
}
Logcat:
08-11 15:58:22.226: E/AndroidRuntime(11763): FATAL EXCEPTION: main
08-11 15:58:22.226: E/AndroidRuntime(11763): Process: com.post.post, PID: 11763
08-11 15:58:22.226: E/AndroidRuntime(11763): java.lang.IllegalStateException: Could not find a method gmailAttchmentSender(View) in the activity class com.post.post.MainActivity for onClick handler on view class android.widget.Button with id 'button2'
08-11 15:58:22.226: E/AndroidRuntime(11763): at android.view.View$1.onClick(View.java:3810)
08-11 15:58:22.226: E/AndroidRuntime(11763): at android.view.View.performClick(View.java:4438)
08-11 15:58:22.226: E/AndroidRuntime(11763): at android.view.View$PerformClick.run(View.java:18422)
08-11 15:58:22.226: E/AndroidRuntime(11763): at android.os.Handler.handleCallback(Handler.java:733)
08-11 15:58:22.226: E/AndroidRuntime(11763): at android.os.Handler.dispatchMessage(Handler.java:95)
08-11 15:58:22.226: E/AndroidRuntime(11763): at android.os.Looper.loop(Looper.java:136)
08-11 15:58:22.226: E/AndroidRuntime(11763): at android.app.ActivityThread.main(ActivityThread.java:5034)
答案 0 :(得分:0)
我找到了崩溃的原因。
我在布局部分做错误,即
机器人:文本=" gmailwithattachment" 机器人:的onClick =" gmailAttchmentSender"
上面的文字是由我写的。现在崩溃没有发生,但带附件的电子邮件也没有发送。我也没有收到任何错误:(