使用JavaMail API发送图像时遇到了一些麻烦。我从相机获取图像但是,我不知道如何发送这个附加消息。这就是我到现在为止开发的方式。
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.Spinner;
import android.widget.ImageView;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import static android.os.StrictMode.ThreadPolicy;
import static android.os.StrictMode.setThreadPolicy;
public class DenunciaActivity extends AppCompatActivity {
private EditText campoNome;
private EditText campoCPF;
private EditText campoEmail;
private EditText campoTelefone;
private EditText campoNomeDoEstabelecimento;
private EditText campoBairroDoEstabelecimento;
private EditText campoPontoDeReferencia;
private EditText campoDescricaoDenuncia;
private Spinner campoSexo;
private Spinner campoMotivo;
private Button botaoSubmeter;
private ImageView logoBack;
private String contaEmail;
private String senha;
private Session session;
private ImageButton primeiroBotao;
private ImageButton segundoBotao;
private ImageButton terceiroBotao;
static final int REQUEST_IMAGE_CAPTURE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_denuncia);
//localização dos itens na tela
campoNome = (EditText) findViewById(R.id.campoNomeID);
campoCPF = (EditText) findViewById(R.id.campoCPFID);
campoEmail = (EditText) findViewById(R.id.campoEmailID);
campoTelefone = (EditText) findViewById(R.id.campoTelefoneID);
campoNomeDoEstabelecimento = (EditText) findViewById(R.id.campoNomeDoEstabelecimentoID);
campoBairroDoEstabelecimento = (EditText) findViewById(R.id.campoBairroEstabelecimentoID);
campoPontoDeReferencia = (EditText) findViewById(R.id.campoPontoDeReferenciaID);
campoDescricaoDenuncia = (EditText) findViewById(R.id.campoDescricaoDenunciaID);
campoSexo = (Spinner) findViewById(R.id.campoSexoID);
campoMotivo = (Spinner) findViewById(R.id.campoMotivoID);
botaoSubmeter = (Button) findViewById(R.id.btnSubmeterID);
logoBack = (ImageView) findViewById(R.id.imageGoBackID);
primeiroBotao = (ImageButton) findViewById(R.id.cameraPrimeiraID);
segundoBotao = (ImageButton) findViewById(R.id.cameraSegundaID);
terceiroBotao = (ImageButton) findViewById(R.id.cameraTerceiraID);
//Verificar se existe camera
if(!hasCamera()){
primeiroBotao.setEnabled(false);
segundoBotao.setEnabled(false);
terceiroBotao.setEnabled(false);
}
//inicializaçao ds variaveis senha e email
contaEmail = "*******@******.com.br";
senha = "*************";
//ArrayAdapter campo sexo sexo
ArrayAdapter sexo = ArrayAdapter.createFromResource(getApplicationContext(), R.array.array_sexo,
R.layout.spinner_layout);
sexo.setDropDownViewResource(R.layout.spinner_dropdown_new);
campoSexo.setAdapter(sexo);
//ArrayAdapter campo motivo
ArrayAdapter motivo = ArrayAdapter.createFromResource(getApplicationContext(), R.array.string_motivos,
R.layout.spinner_layout);
motivo.setDropDownViewResource(R.layout.spinner_dropdown_new);
campoMotivo.setAdapter(motivo);
//botão logoBack
logoBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(DenunciaActivity.this, "Teste", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
//botão primeira camera
primeiroBotao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
//Toast.makeText(DenunciaActivity.this, "Primeira câmera", Toast.LENGTH_SHORT).show();
}
});
//botao segunda camera
segundoBotao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(DenunciaActivity.this, "Segunda câmera", Toast.LENGTH_SHORT).show();
}
});
//botao terceira camera
terceiroBotao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(DenunciaActivity.this, "Terceira câmera", Toast.LENGTH_SHORT).show();
}
});
//Botão submeter
botaoSubmeter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ThreadPolicy policy = new ThreadPolicy.Builder().permitAll().build();
setThreadPolicy(policy);
Properties properties = new Properties();
properties.put("mail.smtp.host", "mail.**********.**.**.br");
//properties.put("mail.smtp.socketFactory.port", "587");
//properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.port", "25");
//properties.put("mail.smtps.starttls.enable", "true\"");//novaLinha
try {
session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(contaEmail, senha);
}
});
if (session != null){
String campos = "Dados do consumidor" + "<br/>" + "Nome: " + campoNome.getText().toString() + "<br/>" + "CPF: " + campoCPF.getText().toString() +
"<br/>" + "E-mail: " + campoEmail.getText().toString() + "<br/>" + "Telefone: " + campoTelefone.getText().toString() + "<br/>"
+ "Sexo: " + campoSexo.getSelectedItem().toString() + "<br/>" + "<br/>" + "Dados do fornecedor" + "<br/>" + "Nome do estabelecimento: " +
campoNomeDoEstabelecimento.getText().toString() + "<br/>" + "Bairro estabelecimento: " + campoBairroDoEstabelecimento.getText().toString()
+ "<br/>" + "Ponto de referencia: " + campoPontoDeReferencia.getText().toString() + "<br/>" + "Motivo: " + campoMotivo.getSelectedItem().toString()
+ "<br/>" + "Descrição da denuncia: " + "<br/>" + campoDescricaoDenuncia.getText().toString();
//Toast.makeText(DenunciaActivity.this, "antes do try", Toast.LENGTH_SHORT).show();
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(contaEmail));
message.setSubject("Formulário denuncia App");
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("*****@*****.****.*****.br"));
message.setContent(campos,"text/html; charset=utf-8");
Transport.send(message);
Toast.makeText(DenunciaActivity.this, "Email enviado com sucesso!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
}catch (Exception e){
e.printStackTrace();
}
}
});
}
//se tiver uma camera, ele retorna true
private boolean hasCamera(){
return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
Bundle extras = data.getExtras();
Bitmap photo = (Bitmap) extras.get("data");
}
}
}
答案 0 :(得分:0)
我没有评论您的问题的声誉。有关如何使用javamail在tutorialspoint上放置内嵌图像的好教程:https://www.tutorialspoint.com/javamail_api/javamail_api_send_inlineimage_in_email.htm
您需要将图片保存到文件中才能执行此操作,以便将位图保存到文件中(如果需要https://stackoverflow.com/a/673014/7194993,可以在此处找到该文件)