|
|
|
อยากทราบวิธีการ Attachment ไฟล์รูปภาพโดยใช้ JavaMail API ครับ ไม่ทราบพี่ๆคนไหนเคยทำบ้างครับ |
|
|
|
|
|
|
|
คือตอนนี้ผมสามารถส่งเมล์ผ่านทาง smtp โดยใช้ javamail ได้แล้วครับ
แต่ติดปัญหาคือผม แนบรูปภาพส่งไปด้วยไม่ได้ครับ
ตอนนี้ผมสามารถเลือกรูปภาพจาก gallery ออกมาได้แล้วครับโดยผมมี path ให้แสดง 2 อันคือ
1. URI path : /ducument/image:466
2 Real path : /storage/emulated/0/DCIM/Camera/img141101.jpg
ไม่ทราบว่าต้องใช้ path ไหนหรอครับในการอ้างอิงรูปภาพเพื่อส่ง
และจะสามารถทำอย่างไรให้แนบรุปภาพไปได้ครับ รบกวนพี่ๆด้วยครับ
MainActivity.java
package com.example.rattapongt.androidsmtp;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.FileNotFoundException;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//Declaring EditText
private EditText editTextEmail;
private EditText editTextSubject;
private EditText editTextMessage;
//Send button
private Button buttonSend, btnImageChooser;
private Bitmap bitmap;
private Uri filePath;
private int PICK_IMAGE_REQUEST = 1;
TextView txtSDK;
Button btnSelectImage;
TextView txtUriPath,txtRealPath;
ImageView imgView;
String realPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtSDK = (TextView) findViewById(R.id.txtSDK);
btnSelectImage = (Button) findViewById(R.id.btnSelectImage);
txtUriPath = (TextView) findViewById(R.id.txtUriPath);
txtRealPath = (TextView) findViewById(R.id.txtRealPath);
//Initializing the views
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextSubject = (EditText) findViewById(R.id.editTextSubject);
editTextMessage = (EditText) findViewById(R.id.editTextMessage);
imgView = (ImageView) findViewById(R.id.imgView);
buttonSend = (Button) findViewById(R.id.buttonSend);
btnImageChooser = (Button) findViewById(R.id.btnImageChooser);
//Adding click listener
buttonSend.setOnClickListener(this);
btnImageChooser.setOnClickListener(this);
}
private void sendEmail() {
//Getting content for email
String email = editTextEmail.getText().toString().trim();
String subject = editTextSubject.getText().toString().trim();
String message = editTextMessage.getText().toString().trim();
String realPath1 = txtRealPath.getText().toString();
//Creating SendMail object
SendMail sm = new SendMail(this, email, subject, message, realPath1);
Toast.makeText(MainActivity.this, realPath1, Toast.LENGTH_LONG).show();
//Executing sendmail to send email
sm.execute();
}
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
/*
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
imgView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
*/
@Override
public void onClick(View v) {
if (v == buttonSend) {
sendEmail();
}
if (v == btnImageChooser){
//showFileChooser();
// 1. on Upload click call ACTION_GET_CONTENT intent
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// 2. pick image only
intent.setType("image/*");
// 3. start activity
startActivityForResult(intent, 0);
// define onActivityResult to do something with picked image
}
}
@Override
protected void onActivityResult(int reqCode, int resCode, Intent data) {
if(resCode == Activity.RESULT_OK && data != null){
// SDK < API11
if (Build.VERSION.SDK_INT < 11)
realPath = RealPathUtil.getRealPathFromURI_BelowAPI11(this, data.getData());
// SDK >= 11 && SDK < 19
else if (Build.VERSION.SDK_INT < 19)
realPath = RealPathUtil.getRealPathFromURI_API11to18(this, data.getData());
// SDK > 19 (Android 4.4)
else
realPath = RealPathUtil.getRealPathFromURI_API19(this, data.getData());
setTextViews(Build.VERSION.SDK_INT, data.getData().getPath(),realPath);
}
}
private void setTextViews(int sdk, String uriPath,String realPath){
this.txtSDK.setText("Build.VERSION.SDK_INT: "+sdk);
this.txtUriPath.setText("URI Path: "+uriPath);
//this.txtRealPath.setText("Real Path: "+realPath);
this.txtRealPath.setText(realPath);
Uri uriFromPath = Uri.fromFile(new File(realPath));
// you have two ways to display selected image
// ( 1 ) imageView.setImageURI(uriFromPath);
// ( 2 ) imageView.setImageBitmap(bitmap);
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uriFromPath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
imgView.setImageBitmap(bitmap);
Log.d("HMKCODE", "Build.VERSION.SDK_INT:"+sdk);
Log.d("HMKCODE", "URI Path:"+uriPath);
Log.d("HMKCODE", "Real Path: "+realPath);
}
}
SendMail.java
package com.example.rattapongt.androidsmtp;
/**
* Created by Rattapongt on 2/16/2017.
*/
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
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.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
//Class is extending AsyncTask because this class is going to perform a networking operation
public class SendMail extends AsyncTask<Void, Void, Void> {
//Declaring Variables
private Context context;
private Session session;
//Information to send email
private String email;
private String subject;
private String message;
private String realPath1;
//Progressdialog to show while sending email
private ProgressDialog progressDialog;
//Class Constructor
public SendMail(Context context, String email, String subject, String message, String realPath1) {
//Initializing variables
this.context = context;
this.email = email;
this.subject = subject;
this.message = message;
this.realPath1 = realPath1;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//Showing progress dialog while sending email
progressDialog = ProgressDialog.show(context, "Sending message", "Please wait...", false, false);
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
//Dismissing the progress dialog
progressDialog.dismiss();
//Showing a success message
Toast.makeText(context, "Message Sent", Toast.LENGTH_LONG).show();
}
@Override
protected Void doInBackground(Void... params) {
//Creating properties
Properties props = new Properties();
//Configuring properties for gmail
//If you are not using gmail you may need to change the values
props.put("mail.smtp.host", "XX.XXX.XX.X");
props.put("mail.smtp.socketFactory.port", "110");
//props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "25");
/*
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
*/
//Creating a new session
session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
//Authenticating the password
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(Config.EMAIL, Config.PASSWORD);
}
});
try {
//Creating MimeMessage object
MimeMessage mm = new MimeMessage(session);
//////////
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
BodyPart messageBodyPart = new MimeBodyPart();
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
String filename = "/storage/emulated/0/Pictures/Screenshots/Screenshot_20170202-091809.png";
//String filename = realPath1;
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
//mm.setContent(multipart);
/////////////
//Setting sender address
mm.setFrom(new InternetAddress(Config.EMAIL));
//Adding receiver
mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
//Adding subject
mm.setSubject(subject);
//Adding message
//mm.setText(message);
//mm.setContent(message, "text/html; charset=utf-8");
mm.setContent(multipart);
//Sending email
Transport.send(mm);
} catch (MessagingException e) {
e.printStackTrace();
}
return null;
}
}
Tag : Mobile, Android, Mobile
|
|
|
|
|
|
Date :
2017-04-10 11:47:59 |
By :
rattapongza |
View :
949 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|