嗨,我正在开发一个Android应用程序,将点击一个按钮发送邮件.代码起初工作但由于某种原因它现在不起作用.有人可以帮我这个吗?
xyz@outlook.com是收件人.
abc@gmail.com是发件人.
我已经对主题和邮件正文进行了硬编码.
package com.example.clc_construction;import java.io.file;import java.io.UnsupportedEnCodingException;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;import androID.app.Activity;import androID.app.ProgressDialog; import androID.content.Intent;import androID.graphics.Bitmap;import androID.os.AsyncTask;import androID.os.Bundle;import androID.os.Environment;public class Email extends Activity{public String jobNo;public String teamNo;private static final String username = "abc@gmail.com";private static final String password = "000000";private static final String emailID = "xyz@outlook.com";private static final String subject = "Photo";private static final String message = "Hello";private Multipart multipart = new MimeMultipart();private MimeBodyPart messageBodyPart = new MimeBodyPart();public file mediafile;@OverrIDeprotected voID onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentVIEw(R.layout.camera_screen); Intent intent = getIntent(); jobNo = intent.getStringExtra("Job_No"); teamNo = intent.getStringExtra("Team_No"); sendMail(emailID,subject,message);}private voID sendMail(String email, String subject, String messageBody) { Session session = createSessionObject(); try { Message message = createMessage(email, subject, messageBody, session); new SendMailTask().execute(message); } catch (AddressException e) { e.printstacktrace(); } catch (MessagingException e) { e.printstacktrace(); } catch (UnsupportedEnCodingException e) { e.printstacktrace(); } }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); } });}private Message createMessage(String email, String subject, String messageBody, Session session) throws MessagingException, UnsupportedEnCodingException{ Message message = new MimeMessage(session); message.setFrom(new InternetAddress("xzy@outlook.com", "Naveed Qureshi")); message.addRecipIEnt(Message.RecipIEntType.TO, new InternetAddress(email, email)); message.setSubject(subject); message.setText(messageBody); return message;}public class SendMailTask extends AsyncTask<Message, VoID, VoID>{ private ProgressDialog progressDialog; @OverrIDe protected voID onPreExecute() { super.onPreExecute(); progressDialog = ProgressDialog.show(Email.this, "Please wait", "Sending mail", true, false); } @OverrIDe protected voID onPostExecute(VoID aVoID) { super.onPostExecute(aVoID); progressDialog.dismiss(); } protected VoID doInBackground(javax.mail.Message... messages) { try { Transport.send(messages[0]); } catch (MessagingException e) { e.printstacktrace(); } return null; }}}
解决方法:
放入你的清单文件,
<uses-permission androID:name="androID.permission.ACCESS_NETWORK_STATE" />
检查你是否有互联网连接,
public boolean isOnline() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getActiveNetworkInfo(); if (netInfo != null && netInfo.isConnectedOrConnecting()) { return true; } return false;}
并最终使用此代码发送电子邮件
final String username = "username@gmail.com";final String password = "password";PropertIEs props = new PropertIEs();props.put("mail.smtp.auth", "true");props.put("mail.smtp.starttls.enable", "true");props.put("mail.smtp.host", "smtp.gmail.com");props.put("mail.smtp.port", "587");Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getpasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress("from-email@gmail.com")); message.setRecipIEnts(Message.RecipIEntType.TO, InternetAddress.parse("to-email@gmail.com")); message.setSubject("Testing Subject"); message.setText("Dear Mail Crawler," + "\n\n No spam to my email, please!"); MimeBodyPart messageBodyPart = new MimeBodyPart(); Multipart multipart = new MimeMultipart(); messageBodyPart = new MimeBodyPart(); String file = "path of file to be attached"; String filename = "attachmentname" DataSource source = new fileDataSource(file); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setfilename(filename); multipart.addBodyPart(messageBodyPart); message.setContent(multipart); Transport.send(message); System.out.println("Done"); } catch (MessagingException e) { throw new RuntimeException(e); }
总结 以上是内存溢出为你收集整理的使用SMTP在没有意图的情况下在android中发送邮件全部内容,希望文章能够帮你解决使用SMTP在没有意图的情况下在android中发送邮件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)