2016-03-06 75 views

回答

0

@Clerk這裏是一個相關的SO問題,這將有助於您格式化您的sender email

下面是從文檔的示例代碼:

/** 
* 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; 
} 

下一步是進行編碼MimeMessage,實例化一個Message對象,並將base64url編碼的消息字符串設置爲原始值。

/** 
* Create a Message from an email 
* 
* @param email Email to be set to raw of message 
* @return Message containing base64url encoded email. 
* @throws IOException 
* @throws MessagingException 
*/ 
public static Message createMessageWithEmail(MimeMessage email) 
throws MessagingException, IOException { 
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
email.writeTo(bytes); 
String encodedEmail = Base64.encodeBase64URLSafeString(bytes.toByteArray()); 
Message message = new Message(); 
message.setRaw(encodedEmail); 
return message; 
} 
相關問題