通过Progmatically发送多行电子邮件

时间:2017-09-23 13:35:35

标签: android

我正在尝试以编程方式发送多行电子邮件。

有多行但它只发送最后一行的数据。以下是我用来发送电子邮件的代码。

//设置属性

 Properties properties = new Properties();
 properties.put("mail.smtp.host","smtp.gmail.com");
 properties.put("mail.smtp.socketFactory.port","465"); 
 properties.put("mail.smtp.socketFactory.class",
 "javax.net.ssl.SSLSocketFactory");
 properties.put("mail.smtp.port","465");
 properties.put("mail.smtp.auth","true");

 session = Session.getDefaultInstance(properties, new Authenticator() {
     @Override
     protected PasswordAuthentication getPasswordAuthentication() {
     return new PasswordAuthentication("email","password");
     }
 });

    progressDialog = ProgressDialog.show(this,"","Sending Mail....",true);
    SettingsActivity.RetreiveFeedTask task = new SettingsActivity.RetreiveFeedTask();
    task.execute();

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("emailAddress"));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(email));
message.setSubject(subject);

// AsyncTask doInBackground()

SQLiteDatabase db = passDBHelper.getReadableDatabase();
String query = "Select * from " + PassDBHelper.TABLE_NAME;

Cursor cursor = db.rawQuery(query,null);

int nameColumnIndex = cursor.getColumnIndex(PassDBHelper.PASS_COLUMN_NAME);
int emailColumnIndex = cursor.getColumnIndex(PassDBHelper.PASS_COLUMN_USERNAME);
int passColumnIndex = cursor.getColumnIndex(PassDBHelper.PASS_COLUMN_PASSWORD);

while(cursor.moveToNext())
{
    String name = cursor.getString(nameColumnIndex);
    String e = cursor.getString(emailColumnIndex);
    String pass = cursor.getString(passColumnIndex);

    message.setContent(name+"\n"+e+"\n"+pass+"\n","text/html;charset=utf-8");//setting content
}

Transport.send(message);//sending email
cursor.close();

1 个答案:

答案 0 :(得分:0)

如果您需要将多行内容作为电子邮件正文发送,您可以使用StringBuilder

像这样

StringBuilder sb;

sb.append("First Name : ");
sb.append(fname.getText().toString());
sb.append('\n');
sb.append("Last Name : ");
sb.append(lname.getText().toString());
sb.append('\n');
sb.append("Email : ");
sb.append(email.getText().toString());

 message.setText(sb.toString())