[英]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.