我有一项服务,在该服务中,我一直在通过邮件发送一些数据。为此,我使用的是Gmail帐户。发送邮件。我搜索了很多,但无法得到正确的答案。
我无法通过Intent发送邮件,因为我不想将其显示给用户。我想要的只是在服务中发送邮件而不通知用户。
为此我遵循this教程,但它没有结束邮件。它只是显示吐司" Mail Sent"但实际上它没有发送消息我已经检查了收件箱并发送了物品,但没有什么
我没有任何例外。但它仍然无法正常工作。
请帮忙。如果您知道任何有效的解决方案
答案 0 :(得分:1)
是。您现在可以以编程方式发送电子邮件。您需要使用Gmail API
根据Gmail API,
创建消息:
/**
* Create a MimeMessage using the parameters provided.
*
* @param to Email address of the receiver.
* @param from Email address of the sender, the mailbox account.
* @param subject Subject of the email.
* @param bodyText Body text of the email.
* @return MimeMessage to be used to send email.
* @throws MessagingException
*/
public static MimeMessage createEmail(String to, String from, String subject,
String bodyText) throws MessagingException {
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage email = new MimeMessage(session);
InternetAddress tAddress = new InternetAddress(to);
InternetAddress fAddress = new InternetAddress(from);
email.setFrom(new InternetAddress(from));
email.addRecipient(javax.mail.Message.RecipientType.TO,
new InternetAddress(to));
email.setSubject(subject);
email.setText(bodyText);
return email;
}
发送消息:
/**
* Send an email from the user's mailbox to its recipient.
*
* @param service Authorized Gmail API instance.
* @param userId User's email address. The special value "me"
* can be used to indicate the authenticated user.
* @param email Email to be sent.
* @throws MessagingException
* @throws IOException
*/
public static void sendMessage(Gmail service, String userId, MimeMessage email)
throws MessagingException, IOException {
Message message = createMessageWithEmail(email);
message = service.users().messages().send(userId, message).execute();
System.out.println("Message id: " + message.getId());
System.out.println(message.toPrettyString());
}
检查链接:Sending Email