從我的應用程序中,我發送具有附件的郵件。完整的代碼是在這裏爲具有附件的電子郵件設置多部分
public int sendMail(MailDraft mailDraftInstance, mailInstance, path){ //Send the mail
String mailSubject = mailDraftInstance.subject
String toAddress = mailDraftInstance.toAddress
String ccAddress = mailDraftInstance.ccAddress
String bccAddress = mailDraftInstance.bccAddress
String fromAddress = mailDraftInstance.fromAddress
String body = mailDraftInstance.body
String smtpUsername = 'myusername'
String smtpPassword = 'mypwd'
/*** set the SMTP properties and Authenticate*****/
Properties smtpMailProperties
Session mailSession
smtpMailProperties = mailInstance.getSmtpConnectionProperties()
mailSession = mailInstance.getMailSession(smtpMailProperties, smtpUsername, smtpPassword)
try {
/**** Set the Header *************/
User loggedInUser = User.get(SecurityContextHolder.context.authentication.principal.id)
Address address = new InternetAddress(mailDraftInstance.fromAddress);
Address replyAddress = new InternetAddress(mailDraftInstance.fromAddress);
Message message = new MimeMessage(mailSession);
message.setFrom(address);
//message.addFrom(address); //Set the from address with the logged in user email
message.setReplyTo(replyAddress)
/*** set Recipients*********/
String recipientType = 'TO'
setMailRecipients(message, toAddress, recipientType, toAddressError)
recipientType = 'CC'
setMailRecipients(message, ccAddress, recipientType, ccAddressError)
recipientType = 'BCC'
setMailRecipients(message, bccAddress, recipientType, bccAddressError)
message.setSubject(mailSubject);
Multipart multiPart = new MimeMultipart("alternative"); // Create an "Alternative" Multipart message
// Multipart multiPart = new MimeMultipart("mixed");
MimeBodyPart text = new MimeBodyPart();
MimeBodyPart html = new MimeBodyPart();
text.setText(body.replaceAll("\\<[^>]*>","").replaceAll(" "," ").replaceAll("&","&")); //set the text/plain version
html.setContent(Jsoup.parse(body).html(), "text/html"); //set the text/html version
multiPart.addBodyPart(text);
multiPart.addBodyPart(html);
/* // Set the Body
Multipart multiPart = new MimeMultipart();
MimeBodyPart messageHtml = new MimeBodyPart(); //Create a mime body
messageHtml.setContent(body, "text/html"); //set the content type as HTML
multiPart.addBodyPart(messageHtml);*/
// Add the Attachments
if(!mailDraftInstance.attachments.isEmpty()){
Mail.attachFiles(mailDraftInstance.attachments, multiPart, path)
}
int i=0;
mailDraftInstance.attachments.each{
i++
}
message.setContent(multiPart); //set the content
Transport.send(message); //send the mail
}catch (Exception e) {
if(e instanceof AddressException){
println "Email Recipient Address error" //Error with the TO Or CC Or BCC Adresss
return addressErrorType
}else{
println e //Other errors, may be with the SMTP server
println "Cannot send email as an error occurred"
return addressErrorType
}
}
return mailSentSuccessfully //mail sent successfully
}
public static attachFiles(def attachments, Multipart multiPart, String path){ //Attach files
attachments.each {
def attachmentId = it.id
String newFile= TrainingService.createFile(attachmentId, path)
MimeBodyPart fileAttachmentPart = new MimeBodyPart();
FileDataSource attachmentfile = new FileDataSource(newFile);
fileAttachmentPart.setDataHandler(new DataHandler(attachmentfile));
println "newFile============="+attachmentfile.getName()
fileAttachmentPart.setFileName(attachmentfile.getName());
multiPart.addBodyPart(fileAttachmentPart);
}
}
如果我設置
Multipart multiPart = new MimeMultipart("alternative");
某些客戶端(yahoomail)不接收附件..
所以應該是多部分設置成接收已經電子郵件同時獲得文本,HTML和附件?
上面的代碼是錯誤的。 –
@BillShannon它是怎麼回事? – Jhanvi
看到我上面的答案,再加上[常見錯誤]列表(http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes)。 –