在android中使用Javamail jar向多个用户发送电子邮件
我正在设计一个android应用程序,允许用户向朋友发送邀请 我正在使用Javamail jar来完成这个过程 一切正常。但是,我希望能够同时向多个用户发送邀请,同时修改每个被邀请者的电子邮件正文。使用密件抄送或抄送是行不通的,因为身体从来都不一样 在我的AsyncTask中,我在TO地址列表上循环并一次发送一封电子邮件。但是,问题是,当发送第一封电子邮件时,假定在第二封邮件正文中的项目将包含在第一封电子邮件中,从而丢弃假定在第一封邮件中的一些邮件 请问我该如何解决这个问题 我的代码:在android中使用Javamail jar向多个用户发送电子邮件,android,jakarta-mail,Android,Jakarta Mail,我正在设计一个android应用程序,允许用户向朋友发送邀请 我正在使用Javamail jar来完成这个过程 一切正常。但是,我希望能够同时向多个用户发送邀请,同时修改每个被邀请者的电子邮件正文。使用密件抄送或抄送是行不通的,因为身体从来都不一样 在我的AsyncTask中,我在TO地址列表上循环并一次发送一封电子邮件。但是,问题是,当发送第一封电子邮件时,假定在第二封邮件正文中的项目将包含在第一封电子邮件中,从而丢弃假定在第一封邮件中的一些邮件 请问我该如何解决这个问题 我的代码: priv
private class SendEmailTask extends AsyncTask<Void, Void, Boolean> {
private ProgressDialog dialog;
public SendEmailTask() {
dialog = new ProgressDialog(_context);
}
protected void onPreExecute() {
dialog.setMessage("Sending");
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setCancelable(false);
dialog.setIndeterminate(true);
dialog.show();
}
@Override
protected Boolean doInBackground(Void... params) {
boolean result = false;
String to = "";
try {
String body = setting.parseAsset(_context, "email.html");
String name = _session.getDisplayName();
body = body.replace("<%NAME%>", name);
if (items.size() > 0) {
Mail mail = null;
for (String to: items) {
mail = new Mail("test@gmail.com",
"Thisisatest1");
body = body.replace("<%EMAIL%>", to);
mail.set_to(to);
mail.set_from("no-reply@test.com");
mail.set_subject(name + " has invited you to Test");
mail.set_body(body);
result = mail.send();
}
}
return result;
} catch (Exception e) {
Log.e("SendEmailTask", "Invite not sent to " + to, e);
dialog.dismiss();
return false;
}
}
@Override
protected void onPostExecute(Boolean result) {
dialog.dismiss();
if (result) {
Toast.makeText(_context, "Invite sent successfully.",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Invite(s) not sent.",
Toast.LENGTH_LONG).show();
}
}
}
public class Mail extends Authenticator{
private String _user;
private String _password;
private String _to;
private String _from;
private String _port;
private String _socket;
private String _host;
private String _subject;
private String _body;
private boolean _auth;
private boolean _debuggable;
private Multipart _multipart;
public String getUser() {
return _user;
}
public void setUser(String user) {
this._user = user;
}
public String getPassword() {
return _password;
}
public void setPassword(String password) {
this._password = password;
}
public String get_to() {
return _to;
}
public void set_to(String _to) {
this._to = _to;
}
public String get_from() {
return _from;
}
public void set_from(String _from) {
this._from = _from;
}
public String get_port() {
return _port;
}
public void set_port(String _port) {
this._port = _port;
}
public String get_socket() {
return _socket;
}
public void set_socket(String _socket) {
this._socket = _socket;
}
public String get_host() {
return _host;
}
public void set_host(String _host) {
this._host = _host;
}
public String get_subject() {
return _subject;
}
public void set_subject(String _subject) {
this._subject = _subject;
}
public String get_body() {
return _body;
}
public void set_body(String _body) {
this._body = _body;
}
public boolean is_auth() {
return _auth;
}
public void set_auth(boolean _auth) {
this._auth = _auth;
}
public boolean is_debuggable() {
return _debuggable;
}
public void set_debuggable(boolean _debuggable) {
this._debuggable = _debuggable;
}
public Multipart get_multipart() {
return _multipart;
}
public void set_multipart(Multipart _multipart) {
this._multipart = _multipart;
}
public Mail() {
_host = "smtp.gmail.com"; // default smtp server
_port = "465"; // default smtp port
_socket = "465"; // default socketfactory port
_user = ""; // username
_password = ""; // 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 mailCap = (MailcapCommandMap) CommandMap
.getDefaultCommandMap();
mailCap.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mailCap.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mailCap.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mailCap.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mailCap.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mailCap);
}
public Mail(String user, String pass) {
this();
this._user = user;
this._password = pass;
}
public boolean send() throws Exception {
Properties props = setProperties();
if (!_user.equals("") && !_password.equals("") && !_to.equals("")
&& !_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();
addressTo.setAddress(_to);
msg.setRecipient(MimeMessage.RecipientType.TO, addressTo);
msg.setSubject(_subject);
msg.setSentDate(new Date());
// setup message body
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(_body);
messageBodyPart.setHeader("Content-Type", "text/html");
_multipart.addBodyPart(messageBodyPart);
// Put parts in message
msg.setContent(_multipart);
msg.saveChanges();
// send email
Transport.send(msg);
return true;
} else {
return false;
}
}
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(_user, _password);
}
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", _socket);
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
return props;
}
}
私有类SendEmailTask扩展了AsyncTask{
私人对话;
公共SendEmailTask(){
dialog=新建ProgressDialog(_上下文);
}
受保护的void onPreExecute(){
设置消息(“发送”);
setProgressStyle(ProgressDialog.STYLE_微调器);
对话框。可设置可取消(false);
对话框。setUndeterminate(true);
dialog.show();
}
@凌驾
受保护的布尔doInBackground(Void…params){
布尔结果=假;
字符串至“”;
试一试{
String body=setting.parseAsset(_context,“email.html”);
字符串名称=_session.getDisplayName();
body=body.replace(“,name”);
如果(items.size()>0){
Mail=null;
对于(字符串到:项){
邮件=新邮件(“test@gmail.com",
“这是测试1”);
主体=主体。替换(“,至);
邮件。将_设置为(至);
mail.set_from(“否-reply@test.com");
mail.set_subject(name+“已邀请您进行测试”);
邮件。集_体(body);
结果=mail.send();
}
}
返回结果;
}捕获(例外e){
Log.e(“SendEmailTask”,“邀请未发送到”+收件人,e);
dialog.dismise();
返回false;
}
}
@凌驾
受保护的void onPostExecute(布尔结果){
dialog.dismise();
如果(结果){
Toast.makeText(_上下文,“邀请已成功发送”,
Toast.LENGTH_LONG).show();
}否则{
Toast.makeText(getApplicationContext(),“未发送邀请。”,
Toast.LENGTH_LONG).show();
}
}
}
公共类邮件扩展验证器{
私有字符串用户;
私有字符串\u密码;
私有字符串_to;
私有字符串_来自;
专用字符串\u端口;
私有字符串\u套接字;
私有字符串\u主机;
私有字符串(subject);;
私有字符串(body),;
私有布尔认证;
私有布尔可调试;
私有多部分_多部分;
公共字符串getUser(){
返回用户;
}
公共void setUser(字符串用户){
这个。_user=user;
}
公共字符串getPassword(){
返回密码;
}
public void setPassword(字符串密码){
此密码为.\u password=password;
}
公共字符串get_to(){
返回;
}
公共无效设置为(字符串为){
这个._to=_to;
}
公共字符串get_from(){
从中返回;
}
公共无效集\从(字符串\从){
这个.\u from=\u from;
}
公共字符串get_port(){
返回端口;
}
公共无效集\端口(字符串\端口){
这个。_端口=_端口;
}
公共字符串get_socket(){
返回插座;
}
公共无效集合\u套接字(字符串\u套接字){
这个。_socket=_socket;
}
公共字符串get_host(){
返回主机;
}
公共无效集\u主机(字符串\u主机){
这个。_host=_host;
}
公共字符串get_subject(){
返回主题;
}
公共无效集合主题(字符串主题){
这个._subject=_subject;
}
公共字符串get_body(){
返回体;
}
公共无效集\正文(字符串\正文){
这个。_body=_body;
}
公共布尔值为_auth(){
返回_auth;
}
公共无效集\u auth(布尔值\u auth){
这是.\u auth=\u auth;
}
公共布尔值是可调试的(){
返回_可调试;
}
公共空集\u可调试(布尔值\u可调试){
这是.\u debuggable=\u debuggable;
}
公共多部分get_Multipart(){
返回多部分;
}
公共无效集\u multipart(multipart\u multipart){
这个.\u multipart=\u multipart;
}
公共邮件(){
_host=“smtp.gmail.com”;//默认smtp服务器
_port=“465”;//默认smtp端口
_socket=“465”//默认socketfactory端口
_user=”“;//用户名
_password=”“;//密码
_from=”“;//从发送的电子邮件
_subject=”“;//电子邮件主题
_body=”“;//电子邮件正文
_debuggable=false;//打开或关闭调试模式-默认关闭
_auth=true;//smtp身份验证-默认为打开
_multipart=新的MimeMultipart();
//MailCap有问题,javamail找不到
//多部分/混合部分的处理程序,因此需要添加此位。
MailcapCommandMap mailCap=(MailcapCommandMap)CommandMap
.getDefaultCommandMap();
mailCap.addMailcap(“text/html;x-java-content-handler=com.sun.mail.handlers.text_html”);
mailCap.addMailcap(“text/xml;x-java-content-handler=com.sun.mail.handlers.text_xml”);
mailCap.addMailcap(“text/plain;;x-java-content-handler=com.sun.mail.handlers.text_plain”);
mailCap.addMailcap(“multipart/*;x-java-content-handler=com.sun.mail.handlers.multipart_mixed”);
mailCap.addMailcap(“message/rfc822;;x-java-content-handler=com.sun.mail.handlers.message\u rfc822”);
setDefaultCommandMap(mailCap);
}
公共邮件(字符串用户、字符串传递){
这个();
这个。_user=user;
此密码为.\u password=pass;
}
public boolean send()引发异常{
Properties props=setProperties();
如果(!\u user.equals(“”)&&!\u password.equals(“”)&!\u to.equals(“”)
&&!\u from.equals(“”&!\u subject.equals(“”&!\u body.equals(“”){
for (InviteItem item : items) {
Mail mail = new Mail("test@test.com",
"MYPASSIFORGOTTOCHANGE");
to = ((com.plan.library.contact.Invite) item)
.get_email();
invitees.add(to);
String body = html.replace("<%EMAIL%>", to);
mail.set_to(to);
mail.set_from("test@test.com");
mail.set_subject(name + " has invited");
mail.set_body(body);
result = mail.send();
mail = null;
}