简体   繁体   中英

Android app send email confirmation no sender name

I try to send confirmation email for a user who log in using facebook login. I successfully sent the email but there's no sender name when I open the email in email app. It's just blank. I want may be the name of the sender (my app name) or may be just the email such noreply@mydomain.com

I changed the codes and my hosting email settings with alternatives but no success in the last 2 days.

in LoginActivity on success:

GMailSender sender = new GMailSender("noreply@mydomain.com","thepassword");
sender.sendMail(
name + ", Login into theappname",
"Hi, "+name+". You've just signed in to the app.",
"noreply@mydomain.com",
emailRecipient);

and in GMailSender class:

public GMailSender(String user, String password) {
        this.user = user;
        this.password = password;
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", "mail.mydomain.com");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.quitwait", "false");
        session = Session.getDefaultInstance(props, this);
    }

public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
        try {
            MimeMessage message = new MimeMessage(session);
            DataHandler handler = new DataHandler(new ByteArrayDataSource(
                    body.getBytes(), "text/plain"));
            message.setSender(new InternetAddress(sender));
            message.setSubject(subject);
            message.setDataHandler(handler);
            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText(body);
            _multipart.addBodyPart(messageBodyPart);

            // Put parts in message
            message.setContent(_multipart);
            if (recipients.indexOf(',') > 0)
                message.setRecipients(Message.RecipientType.TO,
                        InternetAddress.parse(recipients));
            else
                message.setRecipient(Message.RecipientType.TO,
                        new InternetAddress(recipients));
            Transport.send(message);
        } catch (Exception e) {
        }
    }

I want to show the sender name (myAppName) or just email address (noreply@mydomain.com). Any help would be appreciated.

Thanks.

Normally you want to use the setFrom method, not the setSender method. And you want to use the InternetAddress constructor that takes an email address and a "personal name" .

Finally, you may want to fix any of these common JavaMail mistakes .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM