问题描述
我想将消息发送直通电子邮件,而无需使用意图。当我preSS按钮,而不是的电子邮件成功发送的是土司的消息,它显示了电子邮件未发送的和它不发送电子邮件。有人可以帮助我对于这件事?
MainActivity.java
@燮pressLint(SdCardPath)
公共类MainActivity延伸活动{
@覆盖
保护无效的onCreate(捆绑冰柱){
super.onCreate(冰柱);
的setContentView(R.layout.activity_main); 按钮addImage =(按钮)findViewById(R.id.send_email);
addImage.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(查看视图){ 邮件M =新邮件([email protected],密码); 的String [] = toArr {[email protected]};
m.setTo(toArr);
m.setFrom([email protected]);
m.setSubject(邮件测试);
m.setBody(这是用我的邮件JavaMail的包装从Android设备发送了一封电子邮件。); 尝试{
m.addAttachment(/ SD卡/ filelocation); 如果(m.send()){
Toast.makeText(MainActivity.this,邮件发送成功,Toast.LENGTH_LONG).show();
}其他{
Toast.makeText(MainActivity.this,电子邮件未发送,Toast.LENGTH_LONG).show();
}
}赶上(例外五){
//Toast.makeText(MailApp.this,有发送电子邮件的一个问题。Toast.LENGTH_LONG).show();
Log.e(MailApp,无法发送电子邮件,E);
}
}
});
}
Mail.java
公共邮件(){
_host =smtp.gmail.com; //默认的SMTP服务器
_port =465; //默认的SMTP端口
_sport =465; //默认的SocketFactory端口 _user =; // 用户名
_pass =; //密码
_from =;从//发送电子邮件
_subject =; // 电子邮件主题
_body =; //邮件正文 _debuggable = FALSE;开启或关闭//调试模式 - 默认关闭
_auth = TRUE; // SMTP认证 - 拖欠 _multipart =新MimeMultipart的(); //也有一些是错误的MAILCAP,JavaMail的找不到多部分/混合部分的处理程序,所以这一点需要添加。
支持MailcapCommandMap MC =(支持MailcapCommandMap)CommandMap.getDefaultCommandMap();
mc.addMailcap(text / html的;;的X Java的内容处理器= com.sun.mail.handlers.text_html);
mc.addMailcap(文/ XML ;;的X Java的内容处理器= com.sun.mail.handlers.text_xml);
mc.addMailcap(text / plain的;;的X Java的内容处理器= com.sun.mail.handlers.text_plain);
mc.addMailcap(多重/ * ;;的X Java的内容处理器= com.sun.mail.handlers.multipart_mixed);
mc.addMailcap(信息/ RFC822 ;;的X Java的内容处理器= com.sun.mail.handlers.message_rfc822);
CommandMap.setDefaultCommandMap(MC);
} 公共邮件(用户字符串,字符串传递){
本(); _user =用户;
_pass =传递;
} 公共布尔的send()抛出异常{
属性道具= _setProperties(); 如果(_user.equals()及与放大器; _pass.equals()及与放大器; _to.length大于0&放大器;&放大器; _from.equals()及与放大器; _subject.equals( )及与放大器; _body.equals()){
会话的会话= Session.getInstance(道具,这一点); 的MimeMessage味精=新的MimeMessage(会话); msg.setFrom(新网际地址(_from)); 网际地址[] = addressTo新的网际地址[_to.length]
的for(int i = 0; I< _to.length;我++){
addressTo [I] =新的网际地址(_to [I]);
}
msg.setRecipients(MimeMessage.RecipientType.TO,addressTo); msg.setSubject(_subject);
msg.setSentDate(新的Date()); //设置邮件正文
BodyPart的messageBodyPart =新MimeBodyPart();
messageBodyPart.setText(_body);
_multipart.addBodyPart(messageBodyPart); 在消息//把部分
msg.setContent(_multipart); // 发电子邮件
Transport.send(MSG); 返回true;
}其他{
返回false;
}
} 公共无效addAttachment(字符串文件名)抛出异常{
BodyPart的messageBodyPart =新MimeBodyPart();
数据源源=新FileDataSource(文件名);
messageBodyPart.setDataHandler(新的DataHandler(源));
messageBodyPart.setFileName(文件名); _multipart.addBodyPart(messageBodyPart);
} @覆盖
公众的PasswordAuthentication的getPasswordAuthentication(){
返回新的PasswordAuthentication(_user,_pass);
} 私人性质_setProperties(){
属性道具=新特性(); props.put(mail.smtp.host,_host); 如果(_debuggable){
props.put(mail.debug为真);
} 如果(_auth){
props.put(mail.smtp.auth,真);
} props.put(mail.smtp.port,_port);
props.put(mail.smtp.socketFactory.port,_sport);
props.put(mail.smtp.socketFactory.class,javax.net.ssl.SSLSocketFactory);
props.put(mail.smtp.socketFactory.fallback,假); 返回道具;
}
的logcat
06-20 17:01:16.889:E / MailApp(7081):无法发送电子邮件
06-20 17:01:16.889:E / MailApp(7081):显示java.lang.NullPointerException
06-20 17:01:16.889:E / MailApp(7081):在java.util.Hashtable.put(Hashtable.java:365)
06-20 17:01:16.889:E / MailApp(7081):在com.example.emailtest.Mail._setProperties(Mail.java:117)
06-20 17:01:16.889:E / MailApp(7081):在com.example.emailtest.Mail.send(Mail.java:72)
06-20 17:01:16.889:E / MailApp(7081):在com.example.emailtest.MainActivity $ 1.onClick(MainActivity.java:36)
06-20 17:01:16.889:E / MailApp(7081):在android.view.View.performClick(View.java:3558)
06-20 17:01:16.889:E / MailApp(7081):在android.view.View $ PerformClick.run(View.java:14157)
06-20 17:01:16.889:E / MailApp(7081):在android.os.Handler.handleCallback(Handler.java:605)
06-20 17:01:16.889:E / MailApp(7081):在android.os.Handler.dispatchMessage(Handler.java:92)
06-20 17:01:16.889:E / MailApp(7081):在android.os.Looper.loop(Looper.java:137)
06-20 17:01:16.889:E / MailApp(7081):在android.app.ActivityThread.main(ActivityThread.java:4514)
06-20 17:01:16.889:E / MailApp(7081):在java.lang.reflect.Method.invokeNative(本机方法)
06-20 17:01:16.889:E / MailApp(7081):在java.lang.reflect.Method.invoke(Method.java:511)
06-20 17:01:16.889:E / MailApp(7081):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:790)
06-20 17:01:16.889:E / MailApp(7081):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
06-20 17:01:16.889:E / MailApp(7081):在dalvik.system.NativeStart.main(本机方法)
尝试这个 - 我试过它的工作对我来说...
的
Sending使用JavaMail API,而无需使用电子邮件中的Android默认/内置应用
I'd like to send a message thru email without using intent. When I press the button, instead of "Email was sent successfully" is the toast message, it shows "Email was not sent" and it doesn't sending the email. Can someone help me regarding this matter?
MainActivity.java
@SuppressLint("SdCardPath")
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
Button addImage = (Button) findViewById(R.id.send_email);
addImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Mail m = new Mail("[email protected]", "password");
String[] toArr = {"[email protected]"};
m.setTo(toArr);
m.setFrom("[email protected]");
m.setSubject("Email Test");
m.setBody("This is an email sent using my Mail JavaMail wrapper from an Android device.");
try {
m.addAttachment("/sdcard/filelocation");
if(m.send()) {
Toast.makeText(MainActivity.this, "Email was sent successfully.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Email was not sent.", Toast.LENGTH_LONG).show();
}
} catch(Exception e) {
//Toast.makeText(MailApp.this, "There was a problem sending the email.", Toast.LENGTH_LONG).show();
Log.e("MailApp", "Could not send email", e);
}
}
});
}
Mail.java
public Mail() {
_host = "smtp.gmail.com"; // default smtp server
_port = "465"; // default smtp port
_sport = "465"; // default socketfactory port
_user = ""; // username
_pass = ""; // password
_from = ""; // email sent from
_subject = ""; // email subject
_body = ""; // email body
_debuggable = false; // debug mode on or off - default off
_auth = true; // smtp authentication - default on
_multipart = new MimeMultipart();
// There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added.
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
}
public Mail(String user, String pass) {
this();
_user = user;
_pass = pass;
}
public boolean send() throws Exception {
Properties props = _setProperties();
if(_user.equals("") && _pass.equals("") && _to.length > 0 && _from.equals("") && _subject.equals("") && _body.equals("")) {
Session session = Session.getInstance(props, this);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(_from));
InternetAddress[] addressTo = new InternetAddress[_to.length];
for (int i = 0; i < _to.length; i++) {
addressTo[i] = new InternetAddress(_to[i]);
}
msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
msg.setSubject(_subject);
msg.setSentDate(new Date());
// setup message body
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(_body);
_multipart.addBodyPart(messageBodyPart);
// Put parts in message
msg.setContent(_multipart);
// send email
Transport.send(msg);
return true;
} else {
return false;
}
}
public void addAttachment(String filename) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
_multipart.addBodyPart(messageBodyPart);
}
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(_user, _pass);
}
private Properties _setProperties() {
Properties props = new Properties();
props.put("mail.smtp.host", _host);
if(_debuggable) {
props.put("mail.debug", "true");
}
if(_auth) {
props.put("mail.smtp.auth", "true");
}
props.put("mail.smtp.port", _port);
props.put("mail.smtp.socketFactory.port", _sport);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
return props;
}
Logcat
06-20 17:01:16.889: E/MailApp(7081): Could not send email
06-20 17:01:16.889: E/MailApp(7081): java.lang.NullPointerException
06-20 17:01:16.889: E/MailApp(7081): at java.util.Hashtable.put(Hashtable.java:365)
06-20 17:01:16.889: E/MailApp(7081): at com.example.emailtest.Mail._setProperties(Mail.java:117)
06-20 17:01:16.889: E/MailApp(7081): at com.example.emailtest.Mail.send(Mail.java:72)
06-20 17:01:16.889: E/MailApp(7081): at com.example.emailtest.MainActivity$1.onClick(MainActivity.java:36)
06-20 17:01:16.889: E/MailApp(7081): at android.view.View.performClick(View.java:3558)
06-20 17:01:16.889: E/MailApp(7081): at android.view.View$PerformClick.run(View.java:14157)
06-20 17:01:16.889: E/MailApp(7081): at android.os.Handler.handleCallback(Handler.java:605)
06-20 17:01:16.889: E/MailApp(7081): at android.os.Handler.dispatchMessage(Handler.java:92)
06-20 17:01:16.889: E/MailApp(7081): at android.os.Looper.loop(Looper.java:137)
06-20 17:01:16.889: E/MailApp(7081): at android.app.ActivityThread.main(ActivityThread.java:4514)
06-20 17:01:16.889: E/MailApp(7081): at java.lang.reflect.Method.invokeNative(Native Method)
06-20 17:01:16.889: E/MailApp(7081): at java.lang.reflect.Method.invoke(Method.java:511)
06-20 17:01:16.889: E/MailApp(7081): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
06-20 17:01:16.889: E/MailApp(7081): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
06-20 17:01:16.889: E/MailApp(7081): at dalvik.system.NativeStart.main(Native Method)
Try this- I had tried it worked for me...
http://androidautomail.blogspot.in/2013/06/auto-generated-mail-from-android.html?m=1
Sending Email in Android using JavaMail API without using the default/built-in app
这篇关于在Android中传送电子邮件,而不意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!