当我在日志中按“发送”按钮时,显示“邮件已发送,但实际上邮件未发送”。请帮我下面的代码。为什么邮件没有发送?
GMailSender.java
package com.example.sendmail;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import java.util.Properties;
public class GMailSender extends javax.mail.Authenticator {
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;
private Multipart _multipart = new MimeMultipart();
static {
Security.addProvider(new com.example.sendmail.JSSEProvider());
}
public GMailSender(String user, String password) {
this.user = user;
this.password = password;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", mailhost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
session = Session.getDefaultInstance(props, this);
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
public synchronized void sendMail(String subject, String body,
String sender, String recipients) throws Exception {
try {
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(
body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setDataHandler(handler);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(body);
_multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(_multipart);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO,
new InternetAddress(recipients));
Transport.send(message);
} catch (Exception e) {
}
}
public void addAttachment(String filename) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("download image");
_multipart.addBodyPart(messageBodyPart);
}
public class ByteArrayDataSource implements DataSource {
private byte[] data;
private String type;
public ByteArrayDataSource(byte[] data, String type) {
super();
this.data = data;
this.type = type;
}
public ByteArrayDataSource(byte[] data) {
super();
this.data = data;
}
public void setType(String type) {
this.type = type;
}
public String getContentType() {
if (type == null)
return "application/octet-stream";
else
return type;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(data);
}
public String getName() {
return "ByteArrayDataSource";
}
public OutputStream getOutputStream() throws IOException {
throw new IOException("Not Supported");
}
}
}
JSSEProvider.java
package com.example.sendmail;
import java.security.AccessController;
import java.security.Provider;
public final class JSSEProvider extends Provider {
public JSSEProvider() {
super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
AccessController
.doPrivileged(new java.security.PrivilegedAction<Void>() {
public Void run() {
put("SSLContext.TLS",
"org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
put("Alg.Alias.SSLContext.TLSv1", "TLS");
put("KeyManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
put("TrustManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
return null;
}
});
}
}
MainActivity.java
package com.example.sendmail;
import android.os.Bundle;
import android.os.Environment;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button send;
//String filename = "/sdcard/mysdfile";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send = (Button) findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread(new Runnable() {
@SuppressLint("SdCardPath") public void run() {
try {
GMailSender sender = new GMailSender("sendermail@gmail.com","**password***");
///Toast.makeText(getApplicationContext(), "Connect", Toast.LENGTH_LONG).show();
//sender.addAttachment(filename);
sender.addAttachment(Environment.getExternalStorageDirectory().getPath()+"/sdcard/mysdfile.txt");
sender.sendMail("Test mail", "This mail has been sent from android app along with attachment","sendermail@gmail.com",
"recievermail@yahoo.com");
Log.i("Mail", "Sent");
//Toast.makeText(getApplicationContext(),"Your mail has been sent",Toast.LENGTH_LONG).show();
} catch (Exception e) {
runOnUiThread(new Runnable()
{
public void run()
{
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
}
});
Log.i("Mail", "Failed"+e);
//Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show();
}
}
}).start();
send.setText("Ok");
}
});
}
}
没有错误出现,并且不在发件人邮件中发送邮件,在收件人邮件收件箱中也没有邮件。
MainActivity.java
点击按钮:
String fromEmail = "sample@gmail.com";
String fromPassword = "xxxxxx";
String toEmails = to_sample@gmail.com;
String adminEmail = "admin@gmail.com";
String emailSubject = "App Registration Mail";
String adminSubject = "App Registration Mail";
String emailBody = "Your message";
String adminBody = "Your message";
new SendMailTask(YOurActivity.this).execute(fromEmail,
fromPassword, toEmails, emailSubject, emailBody);
GMail.java
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import android.app.AlertDialog.Builder;
import android.util.Log;
public class GMail {
final String emailPort = "587";// gmail's smtp port
final String smtpAuth = "true";
final String starttls = "true";
final String emailHost = "smtp.gmail.com";
String fromEmail;
String fromPassword;
@SuppressWarnings("rawtypes")
String toEmailList;
String emailSubject;
String emailBody;
Properties emailProperties;
Session mailSession;
MimeMessage emailMessage;
public GMail() {
}
@SuppressWarnings("rawtypes")
public GMail(String fromEmail, String fromPassword,
String toEmailList, String emailSubject, String emailBody) {
this.fromEmail = fromEmail;
this.fromPassword = fromPassword;
this.toEmailList = toEmailList;
this.emailSubject = emailSubject;
this.emailBody = emailBody;
emailProperties = System.getProperties();
emailProperties.put("mail.smtp.port", emailPort);
emailProperties.put("mail.smtp.auth", smtpAuth);
emailProperties.put("mail.smtp.starttls.enable", starttls);
Log.i("GMail", "Mail server properties set.");
}
public MimeMessage createEmailMessage() throws AddressException,
MessagingException, UnsupportedEncodingException {
mailSession = Session.getDefaultInstance(emailProperties, null);
emailMessage = new MimeMessage(mailSession);
emailMessage.setFrom(new InternetAddress(fromEmail, fromEmail));
Log.i("GMail","toEmail: "+toEmailList);
emailMessage.addRecipient(Message.RecipientType.TO,
new InternetAddress(toEmailList));
emailMessage.setSubject(emailSubject);
emailMessage.setContent(emailBody, "text/html");// for a html email
// emailMessage.setText(emailBody);// for a text email
Log.i("GMail", "Email Message created.");
return emailMessage;
}
public void sendEmail() throws AddressException, MessagingException {
Transport transport = mailSession.getTransport("smtp");
transport.connect(emailHost, fromEmail, fromPassword);
Log.i("GMail","allrecipients: "+emailMessage.getAllRecipients());
transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
transport.close();
Log.i("GMail", "Email sent successfully.");
}
}
SendMailTask.java:
import java.util.List;
import android.app.Activity;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.os.AsyncTask;
import android.util.Log;
@SuppressWarnings("rawtypes")
public class SendMailTask extends AsyncTask {
private ProgressDialog statusDialog;
private Activity sendMailActivity;
public SendMailTask(Activity activity) {
sendMailActivity = activity;
}
protected void onPreExecute() {
statusDialog = new ProgressDialog(sendMailActivity);
statusDialog.setMessage("Getting ready...");
statusDialog.setIndeterminate(false);
statusDialog.setCancelable(false);
statusDialog.show();
}
@SuppressWarnings("unchecked")
@Override
protected Object doInBackground(Object... args) {
try {
Log.i("SendMailTask", "About to instantiate GMail...");
publishProgress("Processing input....");
GMail androidEmail = new GMail(args[0].toString(),
args[1].toString(), args[2].toString(), args[3].toString(),
args[4].toString());
publishProgress("Preparing mail message....");
androidEmail.createEmailMessage();
publishProgress("Sending email....");
androidEmail.sendEmail();
publishProgress("Email Sent.");
Log.i("SendMailTask", "Mail Sent.");
Config.mailSuccess="1";
} catch (Exception e) {
publishProgress(e.getMessage());
Log.e("SendMailTask", e.getMessage(), e);
}
return null;
}
@Override
public void onProgressUpdate(Object... values) {
statusDialog.setMessage(values[0].toString());
}
@Override
public void onPostExecute(Object result) {
statusDialog.dismiss();
}
}
库文件
我是android初学者!!我正试图发送一封电子邮件。我正在接受用户的姓名、电子邮件和反馈;我想向发送电子邮件emample@xyz.com带有姓名电子邮件和反馈。 我不想使用INTENT\u SEND或INTENT emailIntent=new INTENT(android.content.INTENT.ACTION\u SEND); 我想在后台发送而不打开电子邮件意图。 我试过这个但是
我正在用我的电子邮件Id发送我的邮件数据。我制作了一个包含editText(用于emailId)和按钮的mainActivity类。另一个类是BroadcastReceiver类,我在其中检索数据。现在我不明白如何将这些数据发送到后台提供的电子邮件中。我在谷歌上搜索了很多,但都没有得到所需的回复。请分享这些想法并帮助我。
问题内容: 我试图直接在单击按钮时打开Gmail的电子邮件发送表单,但这总是显示用于发送电子邮件的选项列表。 我这样做是为了打开GMail表单: 但这不是打开GMail表单。我应该怎么做才能打开GMail表单,请帮忙。 有什么办法吗? 问题答案: 在线路上使用一些东西
本文向大家介绍PHP实现163邮箱自动发送邮件,包括了PHP实现163邮箱自动发送邮件的使用技巧和注意事项,需要的朋友参考一下 163邮箱大家都使用过吧,那么基于php如何实现163邮箱自动发送邮件功能呢,下面呐喊教程小编给大家分享具体实现代码: 想给大家展示下效果图: demo.html sendmail.php email.class.php 有关PHP实现163邮箱自动发送邮件功能小编就到此
如何以一种高效可靠的方式来告知应聘者他的申请你已收到?或将订单信息发送给你的客户作为备份?或通知与会者此次报名的活动时间、地点?在金数据,这些都不用你动手。下面以短信为例介绍自动的短信/邮件推送系统的使用方法。 添加手机字段 在金数据中,发送短信/邮件的前提,是要以「手机」字段收集用户的手机号码,系统是以此字段来识别发送对象的,使用「单行文字」无法发送短信。 创建短信模板 进入表单「设置」 ——
是否可以在不打开应用程序的情况下发送whatsApp消息,在后台发送,就像使用以下方式发送短信: 如果是,怎么做?我尝试的代码打开了应用程序(意图): 或者是否可以打开应用程序,发送消息到给定的地址,然后关闭它? 谢谢!