Android 应用程序发送电子邮件确认没有发件人姓名
Android app send email confirmation no sender name
我尝试向使用 facebook 登录的用户发送确认电子邮件。我已成功发送电子邮件,但在电子邮件应用程序中打开电子邮件时没有发件人姓名。它只是空白。我想要的可能是发件人的名字(我的应用程序名称)或者可能只是电子邮件这样的 noreply@mydomain.com
我用其他方法更改了代码和我的托管电子邮件设置,但在过去 2 天内没有成功。
在 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 中 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) {
}
}
我想显示发件人姓名 (myAppName) 或仅显示电子邮件地址 (noreply@mydomain.com)。任何帮助,将不胜感激。
谢谢。
通常您希望使用setFrom 方法,而不是setSender 方法。而您想使用 InternetAddress constructor that takes an email address and a "personal name".
最后,您可能想要修复其中的任何问题 common JavaMail mistakes。
我尝试向使用 facebook 登录的用户发送确认电子邮件。我已成功发送电子邮件,但在电子邮件应用程序中打开电子邮件时没有发件人姓名。它只是空白。我想要的可能是发件人的名字(我的应用程序名称)或者可能只是电子邮件这样的 noreply@mydomain.com
我用其他方法更改了代码和我的托管电子邮件设置,但在过去 2 天内没有成功。
在 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 中 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) {
}
}
我想显示发件人姓名 (myAppName) 或仅显示电子邮件地址 (noreply@mydomain.com)。任何帮助,将不胜感激。
谢谢。
通常您希望使用setFrom 方法,而不是setSender 方法。而您想使用 InternetAddress constructor that takes an email address and a "personal name".
最后,您可能想要修复其中的任何问题 common JavaMail mistakes。