81,091
社区成员
发帖
与我相关
我的任务
分享
public boolean send(String[] to, String[] cc, String[] bcc, String subject, String content, String mailType, String[] attachments, String[] attachmentpic) throws MessagingException
{
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", auth);
Session session = Session.getInstance(props);
MimeMessage message = new MimeMessage(session);
InternetAddress from = new InternetAddress(username);
message.setFrom(from);
// Set Mail To
InternetAddress[] Toaddress = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++)
{
Toaddress[i] = new InternetAddress(to[i]);
}
message.setRecipients(Message.RecipientType.TO, Toaddress);
// Set Mail Cc
if (cc != null)
{
InternetAddress[] Ccaddress = new InternetAddress[cc.length];
for (int i = 0; i < cc.length; i++)
{
Ccaddress[i] = new InternetAddress(cc[i]);
}
message.setRecipients(Message.RecipientType.CC, Ccaddress);
}
// Set Mail Bcc
if (bcc != null)
{
InternetAddress[] Bccaddress = new InternetAddress[bcc.length];
for (int i = 0; i < bcc.length; i++)
{
Bccaddress[i] = new InternetAddress(bcc[i]);
}
message.setRecipients(Message.RecipientType.BCC, Bccaddress);
}
// Set Mail Subject
message.setSubject(subject);
// Set Mail Sent Date
message.setSentDate(new Date());
// Text Content Only
if (attachments == null && attachmentpic == null)
{
// Set Mail Content
if (mailType != null && "text".equals(mailType.trim()))
{
// mail_type is "text" -> for send message
message.setText(content);
}
else
{
BodyPart mdp = new MimeBodyPart();
mdp.setContent(content, "text/html;charset=utf-8");
Multipart mm = new MimeMultipart();
mm.addBodyPart(mdp);
message.setContent(mm);
}
}
// With Attachment or Picture Content
else
{
// Set format mixed
MimeMultipart msgMultipart= new MimeMultipart("mixed");
// Create content with pictures
MimeBodyPart contentPart = createContent(content,attachmentpic);
// Set Attachments
if (attachments != null)
{
for(int i=0;i<attachments.length;i++)
msgMultipart.addBodyPart(createAttachment(attachments[i]));
}
msgMultipart.addBodyPart(contentPart);
message.setContent(msgMultipart);
}
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, (null == domainUser) ? username : domainUser, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
return true;
}
//create attachment according to file path
public MimeBodyPart createAttachment(String fileName) throws MessagingException{
MimeBodyPart attachment = new MimeBodyPart();
FileDataSource fds = new FileDataSource(fileName);
attachment.setDataHandler(new DataHandler(fds));
attachment.setFileName(fds.getName());
return attachment;
}
//create content with picture attachment
public MimeBodyPart createContent(String body, String[] fileName) throws MessagingException{
MimeBodyPart contentBody = new MimeBodyPart();
MimeMultipart contentMulti = new MimeMultipart("related");
//content text
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(body, "text/html;charset=utf-8");
contentMulti.addBodyPart(htmlPart);
//content picture
if(fileName != null)
{
MimeBodyPart jpgBody = new MimeBodyPart();
for(int i=0; i<fileName.length;i++){
FileDataSource fds = new FileDataSource(fileName[i]);
jpgBody.setDataHandler(new DataHandler(fds));
//jpgBody.setContentID(Integer.toString(i));
jpgBody.setContentID("<"+"IMG"+new Integer(i).toString()+">");
contentMulti.addBodyPart(jpgBody);
}
}
contentBody.setContent(contentMulti);
return contentBody;
}
}