Alok
Alok

Reputation: 8998

JavaMail API android application cannot be able to show sender's name

I've been working on feedback form. It consists of name,email and feedback EditTexts. I'm making the android application for the user to send the feedback using JavaMail API. Now My android is working fine as the user inputs the details in the fore mentioned space and sends the feedback to the mail which is given as Recipients address(i.e., My gmail account).

Problem : I have tried my level best to get the data(the mail id of the user)from the EditText input for email id of the user and to show it on the gmail account account as sent by USER_NAME but after numerous efforts it is giving me same data i.e., The mail is sent by me only no user id is shown

I'm providing you the screen shot of my mail account

My gmail inbox where I received the mail from the user

As you can see there are two mails right now, the one which is unread it doesn't show the email id of the user rather shows me because the mail comes from the username provided in the recipient's address

I've done researches on it:

  1. Used setFrom(new InternetAddress(email)) here email is the edittext input of the email by the sender.

    1. went to many searches but none of the ideas are working here (couldn't provide the links as I don't have much reputations to post more than two links. I've two photos which is also in a link form as stackoverflow is not allowing me to uplaod it directly.)

    2. Did this also setFrom(new InternetAddress("Sender"+"<"+email+">")) result is showing Sender

What I ended up doing is : setFrom(new InternetAddress(Config.EMAIL,email)); and got succeeded somehow in showing the email in the email address in the section. The picture will give you better idea

Mail when opened

but still result is same the inbox is repeatedly showing me the sender's email as mine.

Here's mycode:

1. SendMail.java

public class SendMail extends AsyncTask<Void,Void,Void> {

//Declaring Variables
private Context context;
private Session session;

//Information to send email
private String name;
private String email;
private String subject;
private String feedback;

private ProgressDialog progressDialog;

public SendMail(Context context,String name, String email,String subject, String feedback) {
    this.context = context;
    this.name = name;
    this.email = email;
    this.subject = subject;
    this.feedback = feedback;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    //Showing progress dialog while sending email
    progressDialog = ProgressDialog.show(context,"Sending feedback","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,"Feedback Sent", Toast.LENGTH_LONG).show();
}

@Override
protected Void doInBackground(Void... voids) {
    //creating properties
    Properties properties = new Properties();

    //Configuring properties for gmail
    //If you are not using gmail you may need to change the values
    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.auth", "true");
    properties.put("mail.smtp.port", "465");


    //creating new session
    session = Session.getDefaultInstance(properties, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(Config.EMAIL, Config.PASSWORD);
        }
    });

    InternetAddress fromAddress = null;

    try {
        fromAddress = new InternetAddress(Config.EMAIL,email);
    }catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    try {
        //Creating MimeMessage object
        MimeMessage mimeMessage = new MimeMessage(session);

        //Setting sender address
       mimeMessage.setFrom(fromAddress);

        //Adding receiver
        mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(Config.EMAIL));
        //Adding subject
        mimeMessage.setSubject(subject);

        //Adding Message
        mimeMessage.setText("Name:"+ " "+ name + "\n" + "Email:" + " " + email + "\n" +
                                "Feedback" + " " + feedback);

        //Sending email
        Transport.send(mimeMessage);

    } catch (MessagingException e) {
        Log.e("SendMail", e.getMessage(), e);
    }
    return null;
}

}

2.Config.java

public class Config {
public static final String EMAIL = "[email protected]";
public static final String PASSWORD ="password";
}

3. Feedback.java

private void sendEmail() {

    //getting content from email
    String subject = string.toString();
    String email = inputEmail.getText().toString().trim();
    String name = inputName.getText().toString().trim();
    String feedback = inputFeedback.getText().toString().trim();

    //Creating SendMail object
    SendMail sendMail = new   SendMail(getContext(),name,email,subject,feedback);

    //Executing sendmail to send email
    sendMail.execute();
}

Please suggest me some way so that I can reach up to my destination. Thanking you in advance.

Upvotes: 0

Views: 551

Answers (1)

Bill Shannon
Bill Shannon

Reputation: 29971

If your application is using your credentials to login to Gmail, Gmail is not going to let you "fake" the From address to be some other user.

Also, you really don't want to embed your Gmail password in the application.

A much better approach is to create a web application that accepts the input for the feedback form and sends the email. The Android application can post the form data to the web application, which will compose the message and send it, adding the user's email address as another header field or as data in the body of the message. That makes sure your Gmail password is safe on your server, not in the application that users can access.

Upvotes: 1

Related Questions