簡體   English   中英

Android應用發送電子郵件確認沒有發件人姓名

[英]Android app send email confirmation no sender name

我嘗試為使用Facebook登錄名登錄的用戶發送確認電子郵件。 我已成功發送電子郵件,但在電子郵件應用程序中打開電子郵件時沒有發件人姓名。 它只是空白。 我想要的可能是發件人的姓名(我的應用名稱),也可能只是電子郵件,例如noreply@mydomain.com

我用其他方法更改了代碼和托管電子郵件設置,但最近兩天沒有成功。

在LoginActivity上成功:

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);

並在GMailSender類中:

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) {
        }
    }

我想顯示發件人名稱(myAppName)或僅顯示電子郵件地址(noreply@mydomain.com)。 任何幫助,將不勝感激。

謝謝。

通常,您要使用setFrom方法,而不要使用setSender方法。 而且您想使用InternetAddress構造函數,構造函數使用一個電子郵件地址和一個“個人名稱”

最后,您可能需要修復所有這些常見的JavaMail錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM