我发送带附件的邮件,但我面临的问题是.....我想向用户显示“现在它正在附加”.....“现在它正在发送邮件”......这样的通知。 我正在使用Transport .send(msg),它的作用是首先附加附件然后发送邮件,所以我无法区分后台发生的事情。
我使用session.debug(true),它会在控制台中向我通知所有内容,但我想要的是一些触发器或回调,它表示现在正在附加,现在就像这样发送....
这是我的代码..
public void send1() {
Properties props = new Properties();
props.put("mail.smtp.host", email.getSmtpServer());
props.put("mail.smtp.port", email.getSmtpPort());
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
try {
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
session.setDebug(true);
emailMessage = new MimeMessage(session);
emailMessage.setSubject(email.getEmailSubject());
if(email.getAttachedFile().toString().trim().equals("")){
emailMessage.setText(email.getEmailBody());
}else{
attachFile(email.getAttachedFile());
}
emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(email.getEmailTo(),"XYZ"));
Transport transport=session.getTransport("smtp");
transport.addConnectionListener(new EmailTransportListener(callBackObject));
transport.addTransportListener(new EmailTransportListener(callBackObject));
transport.connect();
InternetAddress[] a=new InternetAddress[1];
a[0]=new InternetAddress("abc@gmail.com");
transport.sendMessage(emailMessage,a);
} catch (Exception mex) {
mex.printStackTrace();
}
}
我在这里写我的附件代码.....
private int attachFile(File file){
try {
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText(email.getEmailBody());
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("greetings");
multipart.addBodyPart(messageBodyPart);
// Put parts in message
emailMessage.setContent(multipart);
} catch (MessagingException e) {
}
return 1;
}